编辑:(答案如下)
特别说明:我注意到您没有接受对您的任何问题提供的任何答案,但确实找到了解决方案。
StackOverflow 系统的工作方式是,一旦对问题给出了答案,您就可以勾选白色复选标记直到它变为绿色。
请查看以下有关“接受答案的工作原理”的文章。
StackOverflow 建立在积分/声誉系统之上,旨在鼓励每个人继续取得成功。
阅读“关于”部分:
回答
将error reporting 添加到您的文件中会表示错误。
error_reporting(E_ALL);
ini_set('display_errors', 1);
mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
如果表名是 MySQL 保留字,则不要在表名周围使用引号或使用反引号;在这种情况下,不只是删除表名周围的引号。
$res = mysqli_query($con,"INSERT INTO boatviewing (clientNo,boatNo,viewDate,comment)
(如果你真的想转义你的表名)
$res = mysqli_query($con,"INSERT INTO `boatviewing` (clientNo,boatNo,viewDate,comment)
你似乎还有一个额外的<form> 标签;删除它,并删除 die(mysqli_error());; 末尾多余的分号。
另外,去掉header("Location:client.php");上面的echo "success";,你在header之前输出,会报错。
改变
$con = mysqli_connect("localhost","root","","boats4u")or die(mysqli_error());;
到:
$con = mysqli_connect("localhost","root","","boats4u")
or die("Error " . mysqli_error($con));
按照说明书上的方法:
旁注:您当前的代码对SQL injection 开放。使用prepared statements,或PDO。
这是一个准备好的语句方法:
<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);
mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
$mysqli = new mysqli("localhost","root","","boats4u");
/* check connection */
if (mysqli_connect_errno()) {
printf("Connect failed: %s\n", mysqli_connect_error());
exit();
}
/* Set the params */
$client = $_POST['clientNo'];
$boat = $_POST['boatNo'];
$date = $_POST['viewDate'];
$comment = $_POST['comment'];
/* Create the prepared statement */
if ($stmt = $mysqli->prepare("INSERT INTO boatviewing (clientNo,boatNo,viewDate,comment) VALUES (?, ?, ?, ?)")) {
/* Bind the params */
$stmt->bind_param('ssss', $client, $boat, $date, $comment);
/* Execute the prepared statement */
$stmt->execute();
/* Echo results */
echo "Inserted {$client}, {$boat}, {$date}, {$comment} into database.\n";
/* Close the statement */
$stmt->close();
}
else {
/* Error */
printf("Prepared Statement Error: %s\n", $mysqli->error);
}
?>
如果您不想使用准备好的语句(您确实应该这样做)并且处于学习状态,至少使用mysqli_real_escape_string() 进行一些保护
$client = mysqli_real_escape_string($con,$_POST['clientNo']);
$boat = mysqli_real_escape_string($con,$_POST['boatNo']);
$date = mysqli_real_escape_string($con,$_POST['viewDate']);
$comment = mysqli_real_escape_string($con,$_POST['comment']);