从表中获取旧数据并附加新数据

时间:2012-09-25 09:10:01

标签: php javascript jquery ajax

我无法从表中获取现有数据并向其添加新数据,类似于评论系统。由于.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>

get.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);
           ?>

2 个答案:

答案 0 :(得分:0)

您似乎将第二个查询限制为1返回,因此您只返回一行

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

应该是

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

答案 1 :(得分:0)

尝试像这样编辑

$('#info').append("<p> you are:"+data[i].username+"</p> Hello<p>"+data[i].msg);