我的将 JSON 发送到 mysql 的代码有什么错误?

发布于 2024-10-03 10:16:09 字数 2764 浏览 4 评论 0原文

我的 php 函数从 twitter 获取推文将它们作为 json 编码字符串返回。 JS 脚本捕获该数据并进行处理。使用 ajax 将其 POST 到 php 脚本。接收php脚本解码json并将其插入表中。但是mysql没有数据。错误控制台显示没有错误。这是我的代码:

    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">

<html>
<?php

    function searchResults($q) {

      $host = "http://search.twitter.com/search.atom?q=" . urlencode( $q ) . "&rpp=100";
      $ch = curl_init();
      curl_setopt($ch, CURLOPT_URL, $host);
      curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);

    //Raw xml
      $result = curl_exec($ch);
      curl_close($ch);
      $xml = simplexml_load_string($result);
      return json_encode($xml);
;
      }            //--------------- end of function

?>
<head>
    <script src="http://code.jquery.com/jquery-latest.min.js"></script>
</head>
<body>

<script>
    $(document).ready(function() 
    {
      var msg_top = new Array();
      msg_top = "<"+"?php echo searchResults('windows');"+"?"+">";
      var url = "msg2_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); //
                     }
                    }
                   }
      request.send("msg_top=" + encodeURIComponent(msg_top).replace(/%20/g, '+'));
    });
    </script>
</body>
</html>

这是我用来将数据插入表的脚本

<?php  
      $username = "******";  
      $password = "********";  
      $hostname = "localhost";  
      $dbh = mysql_connect($hostname, $username, $password) or die("Unable to connect   
      to MySQL");  
      $selected = mysql_select_db("spec",$dbh) or die("Could not select first_test");
      if($_POST['msg_top']!="")
      {
          $json = $_POST['msg_top'];
          $msg = strtoupper(json_decode($json));
          $query = "INSERT INTO msg2 (id,msg,msg_id,depth) VALUES ('','$msg','ID','3')";
          mysql_query($query);
          if(!mysql_query($query, $dbh))
          {die('error:' .mysql_error());} echo'success';
        }
     else echo('no value!');
   ?> 

My php function fetches tweets from twitter & return them as json encoded string. A JS script catches that data & POST it to a php script using ajax. The receiving php script decodes json and insert it into table. But there is no data into mysql. Error console shows no error. Here is my code:

    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">

<html>
<?php

    function searchResults($q) {

      $host = "http://search.twitter.com/search.atom?q=" . urlencode( $q ) . "&rpp=100";
      $ch = curl_init();
      curl_setopt($ch, CURLOPT_URL, $host);
      curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);

    //Raw xml
      $result = curl_exec($ch);
      curl_close($ch);
      $xml = simplexml_load_string($result);
      return json_encode($xml);
;
      }            //--------------- end of function

?>
<head>
    <script src="http://code.jquery.com/jquery-latest.min.js"></script>
</head>
<body>

<script>
    $(document).ready(function() 
    {
      var msg_top = new Array();
      msg_top = "<"+"?php echo searchResults('windows');"+"?"+">";
      var url = "msg2_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); //
                     }
                    }
                   }
      request.send("msg_top=" + encodeURIComponent(msg_top).replace(/%20/g, '+'));
    });
    </script>
</body>
</html>

Here is script I am using to insert data into table

<?php  
      $username = "******";  
      $password = "********";  
      $hostname = "localhost";  
      $dbh = mysql_connect($hostname, $username, $password) or die("Unable to connect   
      to MySQL");  
      $selected = mysql_select_db("spec",$dbh) or die("Could not select first_test");
      if($_POST['msg_top']!="")
      {
          $json = $_POST['msg_top'];
          $msg = strtoupper(json_decode($json));
          $query = "INSERT INTO msg2 (id,msg,msg_id,depth) VALUES ('','$msg','ID','3')";
          mysql_query($query);
          if(!mysql_query($query, $dbh))
          {die('error:' .mysql_error());} echo'success';
        }
     else echo('no value!');
   ?> 

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

晨光如昨 2024-10-10 10:16:09

这些行是错误的:

 var msg_top = new array(); // Here error 
 msg_top[] = "<"+"?php echo searchResults('windows');"+"?"+">";

首先,它是“Array”,而不是“array”。实际上您需要的是:

 var msg_top = whatever;

问题是如何将 php 域中的数组编码为 Javascript 数组常量。我怀疑使用 JSON 编码器是最简单的事情。

编辑 - 我会重复警告,我不懂 php,但你的代码可能如下所示:

<?php 
  echo "var msg_top = ", json_encode(searchResults('windows')), ";"
?>

These lines are wrong:

 var msg_top = new array(); // Here error 
 msg_top[] = "<"+"?php echo searchResults('windows');"+"?"+">";

First, it's "Array", not "array". Really all you need is:

 var msg_top = whatever;

The problem is going to be how to encode the array from the php domain into a Javascript array constant. I suspect that using a JSON encoder would be the simplest thing to do.

edit — I'll repeat the caveat that I don't know php, but your code might look like this:

<?php 
  echo "var msg_top = ", json_encode(searchResults('windows')), ";"
?>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文