php – 使用JSON插入时出现问题
发布时间:2020-12-13 16:57:47 所属栏目:PHP教程 来源:网络整理
导读:我试图插入这样的数据 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(us
|
我试图插入这样的数据
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,function(){
$(this).html(usrava).fadeTo(900,1);
});
}
});
}
ContactDB.php <?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->查询($insert)上遇到错误;你的SQL语法有错误;查看与您的MySQL服务器版本相对应的手册,以便在Rajesh附近的第1行使用正确的语法 我应该怎么做才能让它运行.我已经尝试了一切,但找不到任何有效的解决方案.请帮忙!!提前致谢 解决方法
函数implode将使用字符串连接数组元素.现在你的函数是implode(“,”,$data),它将使字符串像这样
NameData,EmailData,PhoneData,MessageData 而你会希望这样做 'NameData','EmailData','PhoneData','MessageData' 所以要做到这一点,你需要使你的内爆函数看起来像这个implode(“’,’”,并且第一个和最后一个已经从你编写的代码中进入. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
