gpt4 book ai didi

php - mysqli_query() 生成查询错误

转载 作者:行者123 更新时间:2023-11-29 18:28:51 25 4
gpt4 key购买 nike

我正在尝试使用 mysqli 将表单数据存储到数据库中,但它生成查询错误,我的代码如下......当我尝试提交数据库连接时,正在生成.. $_POST 工作正常.. 错误仅由 mysqli_query 生成..

<?php 
$name = $_POST["firstname"] . " " . $_POST["lastname"];
$email = $_POST["email"];
$happen = $_POST["whendidhappen"];
$howlong = $_POST["howlong"];
$howmany = $_POST["howmany"];
$describe = $_POST["describe"];
$whattheydid= $_POST["whattheydid"];
$seenmycat = $_POST["seenmycat"];
$anythingelse = $_POST["anythingelse"];


$dbc = mysqli_connect('localhost','root','','abductionreport')
or die('Database connection error');


$query = "INSERT INTO abductionform (firstname, lastname, email,whendidhappen, howlong, describe, whattheydid, seenmycat,anythingelse)VALUES('$name','$name','$email','$happen','$howlong', '$howmany','$describe','$whattheydid', '$seenmycat','$anythingelse')";



$result = mysqli_query($dbc,$query) or die ("Query Error");

mysqli_close($dbc);



?>
<h3>Aliens Abducted Me - Report an Abduction</h3>
<p>Thanks for Submiting the form.</p>
<?php
echo "$name it happend to you on $happen it take $howlong <br>";
echo "Number of aliens: $howmany<br>";
echo "Describe: $describe<br>";
echo "What they did to you: $whattheydid<br>";
echo "Have you seen my cat: $seenmycat<br>";
echo "Anything else : $anythingelse<br>";
echo "Your Email Address is : $email<br>";

?>

最佳答案

DESCRIBE 是 mysql 关键字。将列名称用反引号括起来。为此,请用反引号将表格和所有列括起来。在尝试访问 $_POST 元素之前,请务必使用 isset () 检查它们是否存在。使用带有占位符的 mysqli 准备语句以提高安全性。在发布到 SO 之前始终执行错误检查。

您的查询中还有 9 列和 10 个值 - 这每次都会导致失败。

关于php - mysqli_query() 生成查询错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45902759/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com