无法使用$ _POST将数据插入MySQL数据库

时间:2015-02-09 19:35:25

标签: php mysql mysqli

运行以下代码时,我的数据库中没有数据显示。

我一直收到“无法写入数据库”错误。

Here是我的数据库设置的图像,数据通过phpmyadmin手动输入。

Here是我的桌面结构的另一张图片。

HTML表单

  <form method="post" action="report.php">
    <label for="firstname">First name:</label>
    <input type="text" id="firstname" name="firstname" placeholder="test"/><br />
    <label for="lastname">Last name:</label>
    <input type="text" id="lastname" name="lastname" /><br />
    <label for="email">What is your email address?</label>
    <input type="text" id="email" name="email" /><br />
    <label for="whenithappened">When did it happen?</label>
    <input type="text" id="whenithappened" name="whenithappened" /><br />
    <label for="howlong">How long were you gone?</label>
    <input type="text" id="howlong" name="howlong" /><br />
    <label for="howmany">How many did you see?</label>
    <input type="text" id="howmany" name="howmany" /><br />
    <label for="aliendescription">Describe them:</label>
    <input type="text" id="aliendescription" name="aliendescription" size="32" /><br />
    <label for="whattheydid">What did they do to you?</label>
    <input type="text" id="whattheydid" name="whattheydid" size="32" /><br />
    <label for="fangspotted">Have you seen my dog Fang?</label>
    Yes <input id="fangspotted" name="fangspotted" type="radio" value="yes" />
    No <input id="fangspotted" name="fangspotted" type="radio" value="no" /><br />
    <img src="fang.jpg" width="100" height="175"
      alt="My abducted dog Fang." /><br />
    <label for="other">Anything else you want to add?</label>
    <textarea id="other" name="other"></textarea><br />
    <input type="submit" value="Report Abduction" name="submit" />
  </form>
</body>

report.php

<?php
$name = $_POST['firstname'] . ' ' . $_POST['lastname'];
$first_name = $_POST['firstname'];
$last_name = $_POST['lastname'];
$when_it_happened = $_POST['whenithappened'];
$how_long = $_POST['howlong'];
$how_many = $_POST['howmany'];
$what_they_did = $_POST['whattheydid'];
$alien_description = $_POST['aliendescription'];
$fang_spotted = $_POST['fangspotted'];
$other = $_POST['other'];
$email = $_POST['email'];
$to = 'test@gmail.com';
$subject = 'Aliens abducted med - Abduction report';
$msg = "$name was abducted $when_it_happened and was gone for $how_long \n" . 
  "Number of aliens: $how_many\n" . 
  "Alien description: $alien_description\n" . 
  "What they did: $what_they_did\n" . 
  "Fang spotted: $fang_spotted\n" . 
  "Other comments: $other";

$dbc = mysqli_connect('localhost', 'root', '', 'phpheadfirst')
  or die ('Error. Can\'t connect to the databse');

  $query = "INSERT INTO aliens_abduction (first_name, last_name, when_it_happened, how_long, " .
   "how_many, alien_description, what_they_did, fang_spotted, other, email) " .
   "VALUES ('$first_name', '$last_name', '$when_it_happened', '$how_long', '$how_many', " .
   "'$alien_description', '$what_they_did', '$fang_spotted', '$other', '$email')";

  $result = mysqli_query($dbc, $query)
    or die('Error. Can\'t write to the database');

mysqli_close($dbc);

echo 'Hello ' . $name . ', and thanks for submitting the form.' . '<br />';
echo 'You were abducted ' . $when_it_happened;
echo ' by ' . $how_many . ' aliens';
echo ' and were gone for ' . $how_long . '<br />';
echo 'What did they do to you? ' . $what_they_did . '<br />';
echo 'Describe them: ' . $alien_description . '<br />';
echo 'Was fang there? ' . $fang_spotted . '<br />';
echo 'Do you have more to add?' . $other . '<br />';
echo 'Your email adress is ' . $email;

mail($to, $subject, $msg, 'From:' . $email);

我试图找到问题,但我不能。

我正在使用最新的XAMPP安装,一切正常。

还尝试了我的在线网络服务器上的代码 - 仍然是同样的问题。

4 个答案:

答案 0 :(得分:0)

而不是一个固定的(并且完全无用的)错误消息,让MySQL TELL 你出了什么问题:

$result = mysqli_query($dbc, $query) or die(mysqli_error($dbc));
                                            ^^^^^^^^^^^^^^^^^^

您可能会因语法错误而找到它,这是由于您的大量开放sql injection attack漏洞造成的。

答案 1 :(得分:0)

我找到了答案。我桌子上的一个栏目中有一个“d”。在@Marc B

的帮助下发现错误

答案 2 :(得分:-1)

"查询中INSERT太多了......应该是这样的..

"INSERT INTO aliens_abduction (first_name, last_name, when_it_happened, how_long, how_many, alien_description, what_they_did, fang_spotted, other, email) VALUES ('$first_name', '$last_name', '$when_it_happened', '$how_long', '$how_many','$alien_description', '$what_they_did', '$fang_spotted', '$other', '$email')";

答案 3 :(得分:-1)

$query = "INSERT INTO aliens_abduction (first_name, last_name, when_it_happened, how_long, " . "how_many, alien_description, what_they_did, fang_spotted, other, email) " . "VALUES ('$first_name', '$last_name', $when_it_happened', '$how_long', '$how_many', " .   "'$alien_description', '$what_they_did', '$fang_spotted', '$other', '$email')";

应该成为

$query = "INSERT INTO aliens_abduction (first_name, last_name, when_it_happened, how_long,how_many, alien_description, what_they_did, fang_spotted, other, email) VALUES ('" . $first_name . "', '" . $last_name . "', '" . $when_it_happened . "', '" . $how_long . "', '" . $how_many . "', "'" . $alien_description . "', '" . $what_they_did . "', '" . $fang_spotted . "', '" . $other . "', '" . $email . "')";
相关问题