dongzhi2332 2015-02-09 19:35
浏览 86
已采纳

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

No data shows up in my database when I run the code below.

I keep getting the "Can't write to the database" error.

Here is a image of my database setup, with data entered manually through phpmyadmin.

Here is another image with my table struture.

HTML form

  <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 
" . 
  "Number of aliens: $how_many
" . 
  "Alien description: $alien_description
" . 
  "What they did: $what_they_did
" . 
  "Fang spotted: $fang_spotted
" . 
  "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);

I have tried to find the problem, but I can't.

I am using the newest XAMPP install and it all works.

Have also tried the code on my online webserver - still the same issue.

  • 写回答

4条回答 默认 最新

  • doublel83422 2015-02-09 20:07
    关注

    I found the answer. One of my column in my table had a "d" to much in it. Found the error with the help from @Marc B

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥15 为什么使用javacv转封装rtsp为rtmp时出现如下问题:[h264 @ 000000004faf7500]no frame?
  • ¥15 乘性高斯噪声在深度学习网络中的应用
  • ¥15 运筹学排序问题中的在线排序
  • ¥15 关于docker部署flink集成hadoop的yarn,请教个问题 flink启动yarn-session.sh连不上hadoop,这个整了好几天一直不行,求帮忙看一下怎么解决
  • ¥30 求一段fortran代码用IVF编译运行的结果
  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集
  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛