-1

我无法从表中获取现有数据并将新数据附加到表中,类似于评论系统。我只能看到由于.click.

我想知道我是否仍然需要创建一个新查询来从表中获取现有数据然后附加新的,或者是否有更简单的方法来做到这一点。这是我的代码:

<!doctype html>
<?php
     require_once('get.php');
 ?>
<html class="no-js" lang="en">
   <head>
   <meta charset="utf-8">
   <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
   <title></title>
   <meta name="description" content=""> 
   <meta name="viewport" content="width=device-width">
 </head>
    <body>
       <div id="wrap-body">
        <form action="" method="post">
           <input type="text" name="username" id="username">
           <input type="text" name="msg" id="msg">
           <input type="button" id="submit" value="Send">
        </form>
       <div id="info">
       </div>
        </div> 
    </body>
    <script>
         $(document).ready(function (){
            var username ;
            var msg ;
        $('#submit').click(function (){
             username = $('#username').val();
                msg = $('#msg').val();

            $.ajax({
                      type: 'POST',
                      url: 'get.php',
                      dataType: 'json',
                     data:{'username': username, 'msg':msg},
                      success: function (data){
                     $.each(data, function(i,item) {
                          $('#info').append("<p> you are:"+data[i].username+"</p> <p> your  message  is:"+data[i].mesg);
                          })
                       }
                });
            });
        });
    </script>
    </html>

获取.php:

       <?php
          $host='localhost';
          $username='root';
            $password='12345';
          $db = 'feeds';
          $connect = mysql_connect($host,$username,$password) or die("cant connect");
               mysql_select_db($db) or die("cant select the".$db);
               $username = $_POST['username'];
               $msg = $_POST['msg'];
               $insert = "INSERT INTO info(user_name,message) VALUES('$username','$msg')";
            if(@!mysql_query($insert)){
                  die('error insertion'.mysql_error());
                   }
               $get = "SELECT * FROM info ORDER BY id desc LIMIT 1";
               $result=mysql_query($get)or die(mysql_error());  
               $inside_counter =   mysql_num_rows($result);
                  $data=array();
                    while ($row = mysql_fetch_array($result))
                     {
                      $data[] = array(
                      'username'=>$row['user_name'],
                      'mesg'=>$row['message'],
                      'counter'=>$inside_counter
                    );
                  }
                echo json_encode($data);
           ?>
4

2 回答 2

0

看来您将第二个查询限制为 1 个返回,因此您只能返回一行

$get = "SELECT * FROM info ORDER BY id desc LIMIT 1";

应该

$get = "SELECT * FROM info ORDER BY id desc";
于 2012-09-25T09:13:48.073 回答
0

尝试像这样编辑

$('#info').append("<p> you are:"+data[i].username+"</p> Hello<p>"+data[i].msg);
于 2012-09-25T09:14:18.760 回答