PHP新手,制作基本表单

时间:2014-07-26 13:44:00

标签: php mysql sql forms

我正在使用PHP制作基本提交表单,将数据提交到mysql数据库 但是我遇到了这个错误

Array ( [asset] => test [bunit] => [winkey] => ) INSERT INTO computers (asset, bunit, winkey) VALUES ('test', ', '')Error: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '', '')' at line 2

这是我的示例代码,我目前有一些代码用于错误报告和故障排除

<?php
print_r($_POST);
$con=mysqli_connect("lmydbname","myuser","*******","my table");
// Check connection
if (mysqli_connect_errno()) {
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
}

// escape variables for security
$asset = mysqli_real_escape_string($con, $_POST['asset']);
$bunit= mysqli_real_escape_string($con, $_POST['bunit']);
$winkey = mysqli_real_escape_string($con, $_POST['winkey']);

$sql="INSERT INTO computers (asset, bunit, winkey)
VALUES ('$asset', '$bunit, '$winkey')";
echo $sql;


if (!mysqli_query($con,$sql)) {
  die('Error: ' . mysqli_error($con));
}
header('Location: nextpage.php');


mysqli_close($con);
?>

3 个答案:

答案 0 :(得分:4)

SQL查询中存在语法错误。缺少结束'

INSERT INTO computers (asset, bunit, winkey) VALUES ('$asset', '$bunit', '$winkey')
                                                                      ^

答案 1 :(得分:3)

正如Carl所说,您需要发布HTML,因为它看起来似乎没有将值发送到服务器。

但是你在$ bunit

之后错过了你的sql中的'结束'

VALUES ('$asset', '$bunit, '$winkey')

答案 2 :(得分:2)

VALUES ('$asset', '$bunit, '$winkey')";

应该是

VALUES ('$asset', '$bunit', '$winkey')";