我正在尝试插入这样的数据
function submitData() {
//Posting to ContactDB with JSON format
$.post("contactDB.php",
JSON.stringify({
name: $("#name").val(),
email: $("#email").val(),
phone: $("#phone").val(),
message: $("#message").val()
}),
function(usrava){
// if data is inserted successfully than show insert success message in Result div
if(usrava=='Data Inserted')
{
$("#result").fadeTo(200,0.1,function(){
$(this).html('Your message is successfully saved with us. We will get back to you soon.').fadeTo(900,1);
});
}
//else show the error
else
{
$("#result").fadeTo(200,0.1,function(){
$(this).html(usrava).fadeTo(900,1);
});
}
});
}
<?php
$mysqli = new mysqli("localhost", "root", "", "contactDB"); //Connection to the Database
//If Error than die
if (mysqli_connect_errno()) {
die('Connect Error (' . mysqli_connect_errno() . ') '. mysqli_connect_error());
//Echo for the response
echo "Data base connection NOT Successful. Please get the assistance from your Administrator.";
//Should not go out if not connected
exit();
}
//data received in json. decoded it to an array
$data = json_decode(file_get_contents('php://input'), true);
//Create a insert Command using implode as the data is already in the array
$insert = "INSERT INTO contact(Name,Email,Phone,Message) VALUES ('" .implode(",",$data)."')";
$mysqli->query($insert);
//Close the connection
$mysqli->close();
// Echo for the response
echo "Data Inserted";
?>
问题是我$mysqli->query($insert);
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 Rajesh, at line 1
发现错误<form method="post">
<input type="text" name="Name" id="Name">
<input type="submit" value="Submit">
</form>
我该怎么做让它运行。我已经尝试了一切,但找不到任何有效的解决方案。请帮忙!!提前致谢
答案 0 :(得分:2)
有一些env env env
值。您需要正确使用make -p
。
string
<强>解释强>
您正在使用的代码生成类似于 - '
的语法错误。它们都是VALUES ('" .implode("','",$data)."')"
,需要包含在VALUES ('name, email, phone, message')
中。
string
- 将正确包装它们。它会生成 - '
答案 1 :(得分:1)
函数implode将使用字符串连接数组元素。现在你的函数是implode(",",$data)
,这将使字符串像这样
NameData,EmailData,PhoneData,MessageData
然而,你会想要像这样做
'NameData','EmailData','PhoneData','MessageData'
要做到这一点,你需要使你的内爆函数看起来像这个implode("','",$data)
,并且第一个和最后一个'
已经从你编写的代码中进入。
答案 2 :(得分:0)
试试这个:
$postdata = implode("','", $data) ;
$insert = "INSERT INTO contact(Name,Email,Phone,Message) VALUES ('$postdata')";