表循环查询错误?

时间:2015-09-25 06:22:36

标签: php jquery ajax

我只是想询问你关于我的表查询的帮助,每次点击按钮时,都会显示一个新表。而不是只有一行将更新到表,后面的整个表显示是旧表。请帮我查一下我的代码。谢谢

demo.insert.php

$dbHost = 'localhost'; // usually localhost
$dbUsername = 'root';
$dbPassword = 'thartpc';
$dbDatabase = 'hospital';
$db = mysql_connect($dbHost, $dbUsername, $dbPassword) or die ("Unable to      connect to Database Server.");
mysql_select_db ($dbDatabase, $db) or die ("Could not select database.");

$sql_check = mysql_query("SELECT * FROM messages order by med_id desc");

if(isSet($_POST['content2'])){
    $content2=$_POST['content2'];
    $content3=$_POST['content3'];
    mysql_query("insert into messages(medname, quantity) values    ('$content2','$content3')");
    $sql_in= mysql_query("SELECT * FROM messages order by med_id desc");
    $r=mysql_fetch_array($sql_in);
}

?>

<?php

require_once 'config.php';
$query = mysql_query("SELECT * FROM messages order by med_id desc") or die   ("could not search");
$count = mysql_num_rows($query);   

?>
<div class="container">
    <div class="panel panel-info">
        <div class="panel-heading" class="panel-title">
            <h7>Name</h7>
        </div>
        <div class="panel-body" style="padding-top:30px">
<?php
echo "
            <table class='table table-hover';>
                <tr>
                    <td style='border:1px solid; color:gray' align='center'> ID</td>
                    <td style='border:1px solid; color:gray' align='center'>Name</td>
                    <td style='border:1px solid; color:gray' align='center'>Quantity</td>
                </tr>";
while($row = mysql_fetch_array($query)){
    echo "
                <tr>
                    <td style='border:1px solid; color:gray' align='center'>".$row['med_id']."     </td>
                    <td style='border:1px solid; color:gray'  align='center'>".$row['medname']." </td>
                    <td style='border:1px solid; color:gray'  align='center'>".$row['quantity']."</td>
                </tr> ";
}
echo "      </table>";  

?>    
        </div>
    </div>
</div>

inserting.php

<?php
session_start();

if(isset($_POST['search'])){
    $globalpid = $_POST['search'];
    $_SESSION['search'] = "$globalpid";
}

?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"       "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />

        <link rel="stylesheet"    href="https://maxcdn.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap.min.css">
        <link rel="stylesheet" type="text/css" href="css/custom.css">
        <!-- Optional theme -->
        <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap- theme.min.css">
        <title>Insert Record with jQuery and Ajax</title>
        <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.0/jquery.min.js"></script>

        <script type="text/javascript">
        $(function(){
            $("#v").click(function(){
                var element = $(this);
                var test2 = $("#content2").val();
                var test3 = $("#content3").val();
                var dataString = 'content2='+ test2+'&content3='+ test3;
                if(test2==''){
                    alert("Please Enter Some Text");
                }else{
                    $("#flash").show();
                    $("#flash").fadeIn(400).html('<img src="http://tiggin.com/ajax-loader.gif"       align="absmiddle">&nbsp;<span class="loading">Loading Comment...</span>');
                    $.ajax({
                        type: "POST",
                        url: "demo_insert.php",
                        data: dataString,
                        cache: false,
                        success: function(html){
                            $("#display").after(html);
                            document.getElementById('content2').value='';
                            $("#flash").hide();
                        }
                    });
                }
                return false;
            });
        });
        </script>
    </head>
    <body>
    <?php
        require_once 'pharmacyheader.php';
        require_once 'pharmacysearch.php';
        require_once 'patientquery.php';
    ?>
        <div class="container">
            <form  method="post" name="form" action="">
                <div class="col-md-6">
                    <div class="panel panel-info">
                        <div class="panel-heading"><h7>Name A - F</h7></div>
                        <div class="panel-body">
                            <select class="form-control" id="content2" name="content2">
                                <option id="0" style="width:100px"></option>
    <?php
        require_once 'config.php';
        $medicine = mysql_query("SELECT * FROM medicine");
        while ($row = mysql_fetch_array($medicine)) {
            echo '<option id="' . $row['medicinename']  . '"';
            echo ' value="' . $row['medicinename'] . '"';
            if($row['brandid'] == $row['medicinename']) {
                echo ' selected="selected"'; 
            }
            echo '>';
            echo $row['medicinename'];
            echo '</option>';               
        }
    ?>
                            </select>
                        </div>
                    </div>
                </div>                      
                <div class="col-md-6">
                    <div class="panel panel-info">
                        <div class="panel-heading"><h7>Quantity</h7></div>
                        <div class="panel-body">
                            <select class="form-control" id="content3" name="content3">
                                <option id="0" style="width:100px"></option>
    <?php
    require_once 'config.php';
    $quantity = mysql_query("SELECT * FROM quantity");
    while ($row = mysql_fetch_array($quantity)) {
        echo '<option id="' . $row['quantityname']  . '"';
        echo ' value="' . $row['quantityname'] . '"';
        if($row['quantityid'] == $row['quantityname']) {
            echo ' selected="selected"'; 
        }
        echo '>';
        echo $row['quantityname'];
        echo '</option>';               
    }
    ?>                                  
                            </select>
                        </div>
                    </div>
                </div>
                <div class="row"><br />
                    <div class="col-md-1">
                        <input type="submit" value="Add"  id="v" name="submit" class="form-control"      />
                    </div>
                </div>
            </form>
        </div>
        <div style="height:7px"></div>
        <div id="flash" align="left"  ></div>
        <div id="display" align="left"></div>
        <script     src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
        <script   src="https://maxcdn.bootstrapcdn.com/bootstrap/3.2.0/js/bootstrap.min.js">       </script>
        <script src="typeahead.min.js"></script>
        <script src="http://code.jquery.com/jquery-2.1.1.min.js"></script>
        <script   src="https://maxcdn.bootstrapcdn.com/bootstrap/3.2.0/js/bootstrap.min.js">         </script>
        <script src="js/bootstrap.min.js"></script>
        <script src="js/jquery.min.js"></script>
        <script type="text/javascript" src="jquery.min.js"></script>
    </body>
</html>

2 个答案:

答案 0 :(得分:0)

请看下面的代码,

&#13;
&#13;
echo "<table class='table table-hover';>
<tr>
<td style='border:1px solid; color:gray' align='center'> ID</td>
<td style='border:1px solid; color:gray' align='center'>Name</td>
<td style='border:1px solid; color:gray' align='center'>Quantity</td>

</tr>";

while($row = mysql_fetch_array($query)) 

{
echo "
<tr>
<td style='border:1px solid; color:gray' align='center'>".$row['med_id']."     </td>
<td style='border:1px solid; color:gray'  align='center'>".$row['medname']." </td>
<td style='border:1px solid; color:gray'  align='center'>".$row['quantity']."</td>

</tr> ";

}
echo "</table>";
&#13;
&#13;
&#13;

我认为每次调用ajax时都会执行此代码。

黄油解决方案是放在&#34; table&#34;标记你进行ajax调用的地方,只附加来自ajax响应的tr内容。

这可能会帮助你。

如果您有任何疑问,请告诉我

由于

答案 1 :(得分:0)

好的,所以在查看代码时我可以看到:

  • 您使用ajax检索包含内容的表

  • 你有一个.display元素应该接收ajax数据

因此,每次调用.ajax时,都会收到一个新的完整表格(请查看demo_insert.php)。将新内容放入&#34; .display&#34; item,你使用jquery&#34; .after&#34; (你应该检查它是如何工作的[{3}})

.after应该在其他东西之后添加一些东西。因此,合乎逻辑的是,你的ajax返回(完整的表格)是在&#34; .display&#34;之后添加的。因为你要求jquery这样做。

你应该做的只是更换你的&#34; .display&#34;内容与.ajax响应...

$('#display').html(html);
例如

,或类似的东西......