-2

好的,所以我想要构建:您登录的网站(我稍后会这样做)。有一个表单,您可以在其中提交新闻报道,然后将其输入 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(主键,自动增量)标题作者字幕正文图像

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

对不起,长篇大论的家伙。卢克

4

1 回答 1

2

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

$query = "INSERT INTO news_reports (title, author, subtitle, body) " .
    "VALUES ('$title', '$author', '$subtitle', '$body', '$image')"
于 2013-03-29T17:34:40.777 回答