我正在尝试从Web表单收集输入,然后插入到数据库中。
按下表单的提交按钮时,屏幕会刷新,所有输入框都会被清除。
没有报告错误。但是,当我检查数据库时,尚未存储新条目。
我的HTML代码如下:
<?php
if (isset($_POST['submit'])){
$con = mysqli_connect("localhost","root","","ukhsreturns");
$returnnumber = addslashes($_POST['returnnumber']);
$datereceived = addslashes($_POST['datereceived']);
$customername = addslashes($_POST['customername']);
$customerphone = addslashes($_POST['customerphone']);
$make = addslashes($_POST['make']);
$model = addslashes($_POST['model']);
$fault = addslashes($_POST['fault']);
$assignedto = addslashes($_POST['assignedto']);
$engineersnotes = addslashes($_POST['engineersnotes']);
if (!$con){
die("Can not connect: " . mysqli_error());
}
$sql = "INSERT INTO customers returnnnumber='$returnnumber',datereceived='$datereceived',customername='$customername',customerphone='$customerphone',make='$make',model='$model',fault='$fault',assignedto='$assignedto',engineersnotes='$engineersnotes'";
mysqli_query($con, $sql);
mysqli_close($con);
}
?>
答案 0 :(得分:2)
SQL INSERT语句的编码方式如下,您使用的是UPDATE语法,并且无法编译。
INSERT INTO Table (col1, col2,col3) VALUES (1,'text1','text2')
另外,当您使用mysqli
时,可以使用?
替换参数和bind_param()
See the manual
答案 1 :(得分:1)
Mysql Insert查询格式应该是这样的
INSERT INTO Table (col1, col2,col3) VALUES ('val1','val2','val3')
您的SQL查询应该像
$sql = "INSERT INTO customers (returnnnumber,datereceived,customername,customerphone,make,model,fault,assignedto,engineersnotes) VALUES ('$returnnnumber','$datereceived','$customername','$customerphone','$make','$model','$fault','$assignedto','$engineersnotes')";
只需复制查询并尝试使用