无法从数据库PHP,AJAX中检索数据

时间:2013-12-06 19:43:24

标签: php jquery mysql ajax

这是我在这里的第一篇文章。 我正在创建一个简单的网站,用户可以在其中发表评论,其评论将保存在数据库中。 我在数据库中创建了一个表,其中包含三列“id”,“name”,“comment”(名称不是imp。)。 现在我想制作一个“查看更多”按钮。我在根目录“config.php”,“index.php”,“ajax_more.php”中有三个文件。

这是我的ajax_more.php

     <?php
     include("config.php");
     if(isSet($_POST['lastmsg']))
      {
     $lastmsg=$_POST['lastmsg'];
     $result=mysql_query("select * from commenttable where id<'$lastmsg' order by id desc limit 5");
     $count=mysql_num_rows($result);
     while($row=mysql_fetch_array($result))
     {
      $id=$rows['id'];
      $name=$rows['name'];
     $comment=$rows['comment'];
     ?>
      <li>
      <?php echo "#" . $id . '<br/>' . '<br/>'  .  $comment . '<br/>' . '<br/>' . '<hr size="0"/>'; ?>
       </li>
        <?php
         }
         ?>
         <div id="more<?php echo $id; ?>" class="morebox">
         <a href="#" id="<?php echo $id; ?>" class="more">more</a>
         </div>
          <?php
          }
            ?>

这是index.php

<?php
include('config.php');

    ?>
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <div id="fb-root"></div>

    <script type="text/javascript" src="http://ajax.googleapis.com/ajax/
    libs/jquery/1.3.0/jquery.min.js"></script>

    <script type="text/javascript">
    $(function() {
    //More Button
    $('.more').live("click",function() 
    {
    var ID = $(this).attr("id");
    if(ID)
    {
    $("#more"+ID).html('<img src="moreajax.gif" />');

    $.ajax({
    type: "POST",
    url: "ajax_more.php",
    data: "lastmsg="+ ID, 
    cache: false,
    success: function(html){
    $("ol#updates").append(html);
    $("#more"+ID).remove();
    }
    });
    }
    else
    {
    $(".morebox").html('The End');

    }


    return false;


    });
    });

    </script>


    <style>
    body{font-family:Arial,Helvetica,sans-serif;
    width:50%;
    position:relative;
    margin-left:auto;
    margin-right:auto;
    }

    a { text-decoration:none; color:#0066CC}
    a:hover { text-decoration:underline; color:#0066cc }
    *
    { margin:0px; padding:0px }
    ol.timeline
{ list-style:none}ol.timeline li{ position:relative;border-bottom:1px #dedede dashed;                       padding:8px; }ol.timeline li:first-child{}
.morebox
{
font-weight:bold;
color:#333333;
text-align:center;
border:solid 1px #333333;
padding:8px;
margin-top:8px;
margin-bottom:8px;
-moz-border-radius: 6px;-webkit-border-radius: 6px;
}
.morebox a{ color:#333333; text-decoration:none}
.morebox a:hover{ color:#333333; text-decoration:none}
#container{margin-left:60px; width:580px }

    </style>
    <title>Comment box</title>
    </head>

    <body>
    <center>
    <form action="index.php" method="POST">
    <table font-family="inherit">

    <tr><td colspan="2">Comment: </td></tr>
    <tr><td colspan="5"><textarea name="comment" rows="10" cols="50"></textarea></td></tr>
    <tr><td colspan="2"><input type="submit" name="submit" value="Comment"></td></tr>
    </table>
    </form>


    <div id='container'>
    <ol class="timeline" id="updates">
    <?php
    $dbLink = mysql_connect("mysql17.000webhost.com", "a1360777_tester", "9868364058?");
        mysql_query("SET character_set_results=utf8", $dbLink);
        mb_language('uni');
        mb_internal_encoding('UTF-8');

    $getquery=mysql_query("SELECT * FROM commenttable ORDER BY id DESC LIMIT 5");
    while($rows=mysql_fetch_array($getquery))
    {
    $id=$rows['id'];
    $name=$rows['name'];
    $comment=$rows['comment'];
    ?>
    <li>
    <?php echo "#" . $id . '<br/>' . '<br/>'  .  $comment . '<br/>' . '<br/>' . '<hr size="0"/>'; ?>
    </li>
    <?php } ?>
    </ol>
    <div id="more<?php echo $id; ?>" class="morebox">
    <a href="#" class="more" id="<?php echo $id; ?>">more</a>
    </div>
    </div>

    </body>
    </html>

这是我的config.php

    <?php
    mysql_connect("mysql17.000webhost.com","a1360777_tester","9868364058?");
    mysql_select_db("a1360777_test");
    $name=$_POST['name'];
    $comment=$_POST['comment'];
    $submit=$_POST['submit'];

    $dbLink = mysql_connect("mysql17.000webhost.com", "a1360777_tester", "9868364058?");
        mysql_query("SET character_set_client=utf8", $dbLink);
        mysql_query("SET character_set_connection=utf8", $dbLink);

            if($submit)
    {
    if($comment)
    {
    $insert=mysql_query("INSERT INTO commenttable (name,comment) VALUES ('$name','$comment') ");  
        ?>
    <script type="text/javascript">
    window.location = "http://sonymobile.comule.com/";
    </script>      
        <?php
    }
    else
    {
    echo "Please Fill out all Fields";
    }
    }
    ?>

点击“更多”按钮后,它仅从ajax中发布(回声)“#”,而不是评论。 请帮忙,不要做什么。

1 个答案:

答案 0 :(得分:0)

将您的while声明更改为foreach

$row=mysql_fetch_array($result);
foreach($row as $key)
 {
  $id=$key['id'];
  $name=$key['name'];
 $comment=$key['comment'];
 ?>
  <li>
  <?php echo "#" . $id . '<br/>' . '<br/>'  .  $comment . '<br/>' . '<br/>' . '<hr size="0"/>'; ?>
   </li>
    <?php
     }
     ?>
     <div id="more<?php echo $id; ?>" class="morebox">
     <a href="#" id="<?php echo $id; ?>" class="more">more</a>
     </div>
      <?php
      }
        ?>

如果有效,请告诉我