2010-11-07 20 views
0

我无法从我的代码生成正确的输出,从MySQL数据库动态列出记录时遇到问题。 这是形式:使用ajax从mysql数据库中列出记录

<form action="lister.php" method="get" id="info" name="weboptions" class="lesser"> 
    <h2>List Products</h2> 
    <input type="radio" name="xo" value="n" class="styled">Name<br/> 
    <input type="radio" name="xo" value="c" class="styled">Category<br/> 
    <input type="radio" name="xo" value="d" class="styled">Description 
     <div id="dta-wrap" class="slider"> 
     <input type="text" id="neym" name="neym" onKeyUp="bypname(this.value)" class="DEPENDS ON xo BEING n AND CONFLICTS WITH XO BEING d OR XO BEING c"/></span> 
     </div><!--/#dta-wrap--> 
    <div id="dta2-wrap" class="slider"> 
     <input type="text" id="c" name="c" onKeyUp="bycat(this.value)" class="DEPENDS ON xo BEING c AND CONFLICTS WITH XO BEING d OR XO BEING n"/></span> 
    </div><!--/#dta-wrap--> 

    <div id="dta3-wrap" class="slider"> 
     <input type="text" id="dta3" name="dta3" onKeyUp="bydesc(this.value)" class="DEPENDS ON xo BEING d AND CONFLICTS WITH XO BEING n OR XO BEING c"/></span> 
    </div> 
<a href="adminpage.php"><input type="button" id="btn" name="back" value="back"></a> 
</form> 
<div id="resultcat"></div> 

这是JavaScript文件,该文件调用PHP文件,其中查询所在位置:

function bydesc(str) 
{ 
if (str=="") 
    { 
    document.getElementById("resultcat").innerHTML=""; 
    return; 
    } 
if (window.XMLHttpRequest) 
    { 
    xmlhttp=new XMLHttpRequest(); 
    } 
else 
    { 
    xmlhttp=new ActiveXObject("Microsoft.XMLHTTP"); 
    } 
xmlhttp.onreadystatechange=function() 
    { 
    if (xmlhttp.readyState==4 && xmlhttp.status==200) 
    { 
    document.getElementById("resultcat").innerHTML=xmlhttp.responseText; 
    } 
    } 
xmlhttp.open("GET","listerdesc.php?dta3="+str,true); 
xmlhttp.send(); 
} 

function bypname(str) 
{ 
if (str=="") 
    { 
    document.getElementById("resultcat").innerHTML=""; 
    return; 
    } 
if (window.XMLHttpRequest) 
    { 
    xmlhttp=new XMLHttpRequest(); 
    } 
else 
    { 
    xmlhttp=new ActiveXObject("Microsoft.XMLHTTP"); 
    } 
xmlhttp.onreadystatechange=function() 
    { 
    if (xmlhttp.readyState==4 && xmlhttp.status==200) 
    { 
    document.getElementById("resultcat").innerHTML=xmlhttp.responseText; 
    } 
    } 
xmlhttp.open("GET","listerpname.php?dta="+str,true); 
xmlhttp.send(); 
} 

function bycat(str) 
{ 
if (str=="") 
    { 
    document.getElementById("resultcat").innerHTML=""; 
    return; 
    } 
if (window.XMLHttpRequest) 
    { 
    xmlhttp=new XMLHttpRequest(); 
    } 
else 
    { 
    xmlhttp=new ActiveXObject("Microsoft.XMLHTTP"); 
    } 
xmlhttp.onreadystatechange=function() 
    { 
    if (xmlhttp.readyState==4 && xmlhttp.status==200) 
    { 
    document.getElementById("resultcat").innerHTML=xmlhttp.responseText; 
    } 
    } 
xmlhttp.open("GET","listercat.php?dta2="+str,true); 
xmlhttp.send(); 
} 

而这正是该查询(listerpname.php):

<?php 
if(!empty($_GET['dta'])){ 
$pname=$_GET['dta']; 
$result1=query_database("SELECT * FROM prod_table WHERE PRODUCT LIKE'%$pname%'", "onstor", $link); 
?> 

<?php 
if(mysql_fetch_assoc($result1)==0){ 
    echo "<center><h3 id='wyt'>Your query produced no results..</h3></center>"; 
}else{ 
?>   

<center>   
<table border="1"> 
<thead> 
    <tr> 
    <th>PID</th> 
    <th>PRODUCT</th> 
    <th>CATEGORY</th> 
    <th>DESCRIPTION</th> 

    <th>QTY_ON_HAND</th> 
    <th>REORDER_QTY</th> 
<th>DEALER PRICE</th> 
    <th>SELL PRICE</th> 
    </tr> 
</thead> 

<?php 
while($row=mysql_fetch_assoc($result1)) { 

?> 
    <tbody> 
      <tr> 
     <td><a href="delprod.php?prodid=<?php echo $row['PID']; ?>"><?php echo $row['PID']; ?></td> 
     <td><a href="updateprod.php?prodname=<?php echo $row['PRODUCT']; ?>" class="plain"><?php echo $row['PRODUCT']; ?></a></td> 

     <td><?php echo $row['CATEGORY']; ?></td> 
     <td><?php echo $row['P_DESC']; ?></td> 

     <td><?php echo $row['QTYHAND']; ?></td> 
     <td><?php echo $row['REORDER_LVL']; ?></td> 
     <td><?php echo $row['B_PRICE']; ?></td> 
     <td><?php echo $row['S_PRICE']; ?></td> 
      </tr> 
     </tbody> 

<?php } ?> 
<?php } ?> 
<?php } ?> 

的问题是,如果我在3字母词(“甲苯磺酰”)类型的输出只有一个记录。它应该输出2条记录。因为在我的数据库中有2个以'tos'开头的记录。它只输出我添加的最新记录。 当我这样做:

SELECT * FROM prod_table WHERE PRODUCT LIKE'$pname%' 

它使情况恶化。请帮忙。

+0

这就是你的'SELECT'查询真的被调用了吗('LIKE'和'$ pname'之间没有空格)?或者在粘贴问题时出现错字? – 2010-11-07 03:03:59

回答

4

它看起来像问题是,当你在下面一行检查结果,要移动光标1和ResultSet中被丢弃的第一个记录:

if(mysql_fetch_assoc($result1)==0){ 

您可能需要使用代替mysql_num_rows函数,以避免丢失结果集中的第一条记录。

if(mysql_num_rows($result1)==0){ 
+0

我刚刚做出同样的评论,但你已经拥有了它:)小小的增加:你也可以先将数据提取到'$ row'中,如果包含错误,则停止。如果不是,则输入已经填充了“$ row”的while循环,并在循环结尾处填充$ row。保存你对'mysql_num_rows()'的调用。呃,好吧。 – thomaspaulb 2010-11-07 04:44:08

相关问题