json函数中的错误将数据保存到数据库中

时间:2010-11-30 15:37:24

标签: php mysql json

我已经尝试了很多但是我无法找出这个函数的问题是将两个值保存到数据库中。它一直很好地用于保存一个值的另一个函数。这里表现得很奇怪。有时发送'父母'的价值&有时停止发送它但从不发送msg值。这是功能。它适用于一个输入即父级,但问题始于添加第二个输入。

<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 =“localhost”;
      $ 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())
      ;}

&GT?;

1 个答案:

答案 0 :(得分:2)

阿尔特

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检索这些值。