php - 使用ajax从mysql数据库中列出记录

标签 php javascript 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>

这是调用查询所在的 php 文件的 javascript 文件:

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 个字母的单词 ('Tos'),它只输出一条记录。它应该输出 2 条记录。因为在我的数据库中有 2 条以“tos”一词开头的记录。它只输出我添加的最新记录。 当我这样做时:

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

情况变得更糟了。请帮忙。

最佳答案

看起来问题在于,当您检查下一行中的结果时,您将光标移动 1 并丢弃结果集中的第一条记录:

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

您可能想改用 mysql_num_rows 函数,以避免丢失结果集中的第一条记录。

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

关于php - 使用ajax从mysql数据库中列出记录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4116312/

相关文章:

java - 使用可序列化隔离的同一事务的 2 个实例出现死锁

mysql - 如何使用 Spring Boot 回滚 MySQL 数据库中的所有更改

php - 使用 php 运行 c 程序

php - 如何使用 CodeIgniter 检索 HTTP $_GET 值

php - 将 PHP 数组转换为对象的 JSON 数组

javascript - JS/Jquery,匹配未找到 PNG = 匹配 ('/gif|jpg|jpeg|png/' )

php - 'C :\wmic' is not recognized as an internal or external command, 可运行的程序或批处理文件

javascript - 自定义 chop 函数删除所有初始文本

javascript - 无法在 Angular UI Grid 的可扩展行中保存行

mysql - 错误 : connect ECONNREFUSED