Php插入数据库似乎无法正常工作

时间:2015-12-11 12:49:41

标签: php html mysqli

我正在尝试从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);
}
?>

2 个答案:

答案 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')";

只需复制查询并尝试使用