我已经尝试了很多,但是我无法找出该函数将两个值保存到数据库中出了什么问题。另一个功能保存一个值一直很好。这里的表现很奇怪。有时发送“父”值,有时停止发送,但从不发送味精值。这是功能。它对于一个输入(即父级)工作正常,但问题从添加第二个输入开始。
<script>
function ADDLISITEM(form)
{
var parent = form.txtInput.value;
var msg = form.msgInput.value;
form.txtInput.value = "";
form.msgInput.value = "";
var url = "send_mysql.php"
var request = null;
if (window.XMLHttpRequest)
{// code for IE7+, Firefox, Chrome, Opera, Safari
request=new XMLHttpRequest();
}
else
{// code for IE6, IE5
request=new ActiveXObject("Microsoft.XMLHTTP");
}
request.open("POST", url, true);
request.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
request.setRequestHeader("Connection", "close");
request.onreadystatechange = function(){
if (request.readyState == 4) {
if (request.status == 200) {
//alert('POST');
} else {
alert(request.status); // fails here
}
}
}
request.send("parent=" + encodeURIComponent(parent).replace(/%20/g, '+')+"&msg=" +
encodeURIComponent(msg).replace(/%20/g, '+'));
}
</script>
这是send.php
$ username =“ babar”;
$ password =“ k4541616”;
$ hostname =“本地主机”;
$ dbh = mysql_connect($ hostname,$ username,$ password)或die(“无法连接
到MySQL”);
$ selected = mysql_select_db(“ spec”,$ dbh)或die(“无法选择first_test”);
//die(var_export($_POST,TRUE));
$parent = $_POST['parent'];
$msg = $_POST['msg'];
$name = 'Akhtar Nutt';
//$parent2 = json_decode($parent);
$msg_ID = '2q7b2sfwwe';
//$msg2 = json_decode($msg);
$query = "INSERT INTO msg2_Qualities(id,name,msg,msg_id,parent) VALUES
('','$name','$msg','$msg_ID','$parent')";
if(!mysql_query($query, $dbh))
{die('error:' .mysql_error())
;}
?>
最佳答案
改变
request.send("parent=" + encodeURIComponent(parent).replace(/%20/g, '+')+"msg=" + encodeURIComponent(msg).replace(/%20/g, '+'));
至:
request.send("parent=" + encodeURIComponent(parent).replace(/%20/g, '+')+"&msg=" + encodeURIComponent(msg).replace(/%20/g, '+'));
您在查询字符串中缺少参数分隔符
&
...您可能还想避免使用
$_REQUEST
中的值,因为它们不可靠。如果您的脚本需要来自POST
的数据,请从$_POST
检索这些值。关于php - json函数将数据保存到数据库中时出错,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/4315474/