【问题标题】:MySQL querying error, I don't know whyMySQL查询错误,我不知道为什么
【发布时间】:2013-03-29 17:28:59
【问题描述】:

好的,我要构建的内容: 您登录的网站(我稍后会这样做)。有一个表单,您可以在其中提交新闻报道,然后将其输入 MySQL 数据库。然后它会显示在 iPhone 上的表格视图中(稍后会出现)。 正如我所提到的,我得到一个“查询数据库时出错”。我试图修复它,但我是 MySQL 和 PHP 的新手,所以我不知道还能做什么。 我在家庭服务器 (WAMP) 上设置了这个。

我的报告.html:

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
  <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  <title>Football Central News Report Submission Page</title>
  <link rel="stylesheet" type="text/css" href="style.css" />
</head>
<body>
  <h1>Football Central News Report Submission Page</h1>
  <h2>Sumbit your football news report here</h2>
  <h3>CHECK FOR MISTAKES !!!</h3>
  <form method="post" action="report.php">
    <label for="title">Title:</label>
    <input type="text" name="title" />
    <br />
    <label for="author">Author:</label>
    <input type="text" name="author" />
    <br />
    <label for="subtitle">Subtitle:</label>
    <input type="text" name="subtitle" />
    <br />
    <label for="body">Body:</label>
    <textarea name="body"></textarea>
    <br />
    <label for="image">Image:</label>
    <input type="file" id="image" name="image" />
    <br />
    <input type="submit" value="Submit your news report" name="submit" />
  </form>
</body>
</html>

我的报告.php:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
  <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  <title>Football Central News Report Submission Page</title>
  <link rel="stylesheet" type="text/css" href="style.css" />
</head>
<body>
  <h1>Football Central News Report Submission Page (Confirmation)</h1>

<?php
  $title = $_POST['title'];
  $author = $_POST['author'];
  $subtitle = $_POST['subtitle'];
  $body = $_POST['body'];
  $image = $_POST['image'];

  $dbc = mysqli_connect('localhost', 'root', 'xxxxxx', 'news_reports')
    or die('Error connecting to database server.');

  $query = "INSERT INTO news_reports (title, author, subtitle, body) " .
    "VALUES ('$title', '$author', '$subtitle', '$body', '$image')";

  $result = mysqli_query($dbc, $query)
    or die('Error querying database.');

  mysqli_close($dbc);
?>

<p>
-Thanks for submitting the form.<br />
-Your news report has been submitted to the database and should appear in the app shorty.
</p>

</body>
</html>

最后——我的数据库结构(我无法发布屏幕截图) 领域: report_id(主键,自增) 标题 作者 字幕 身体 图片

附言 对于图像/\我关注的是“Head First PHP and MySQL”,所以它是存储在数据库中的图像名称,而不是图像(我没有把它放入表格中,但我不认为这是问题)。

对不起,长篇大论。 卢克

【问题讨论】:

  • 你有问题吗?
  • 只需echo mysqli_error($dbc);,您就会知道发生了什么
  • +1 用于使用 mysqli 扩展而不是旧的 mysql 函数。但是 -100 表示根本没有对输入变量进行任何转义。您的代码很容易受到 SQL 注入攻击,如果任何输入包含引号或其他特殊字符,则会失败。
  • 您没有在 SQL 查询的列列表中提及 image 字段。

标签: php html mysql phpmyadmin dreamweaver


【解决方案1】:

你有一个不匹配。您命名了 4 个要插入的列,但您定义了 5 个值。添加缺少的列。

$query = "INSERT INTO news_reports (title, author, subtitle, body) " .
    "VALUES ('$title', '$author', '$subtitle', '$body', '$image')"

【讨论】:

  • 大家好,谢谢。我知道这会很简单,哈哈,感谢您提供的注射技巧,我还没有达到书中的那部分。但是感谢您的提醒!
  • 好的,所以我可能需要更多帮助。我已经(尝试)改进了很多脚本,但是(使用 DreamWeaver)我遇到了语法错误,但我看不出有什么问题。我应该在哪里发布代码?它是 249 字。
  • 在查询后输入echo $query; exit; 并在此处发布。
  • 好吧,现在我有 2 个新问题。我知道问题是什么,但我不知道如何解决它们。一个是文件上传,另一个是联系表格。我现在应该在哪里发帖?对不起,伙计们。
  • 如果您有新问题,请提出新问题。
猜你喜欢
  • 2014-09-22
  • 2012-04-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-01-24
  • 1970-01-01
  • 2021-06-15
  • 2017-11-23
相关资源
最近更新 更多