繁体   English   中英

PHP查询正确,但结果不匹配?

[英]PHP Query is correct but results don't match?

我只有两个数据库,它们都通过相同的选择脚本运行。 该脚本非常适合其中一个脚本,但是另一个脚本仅显示数据库的前三行,并且会重复显示它们。

由于我是PHP新手,所以我无法想象为什么它适用于一个数据库而不适用于另一个数据库。 我将所有拼写检查了四倍,并且为了确定我回显了查询脚本。 更糟糕的是,浏览器没有给出错误提示。 它只是显示了错误的信息。

请帮忙。 。谢谢!

index.html(这是表格...我不认为问题在这里...但是我加入其中是为了以防万一...我认为问题必须在product_list.php中解决)

<!DOCTYPE html>
<html>
        <head><title>Databases</title></head>
        <body>
                <h1>Music Store Database</h1>
                <form method='POST' action='display.php'
                        <label>Select a table:</label>
                        <select name="tableName">
                                <option value="products">Products</option>
                                <option value="categories">Categories</option>
                        </select>
                        <p>To ADD: Enter the field values(s)  below for the record(s) you want to add.  NOTE:  Adding a record requires all appropriate field for the selected table.<p>
                        <p>To DELETE:  Enter the value for the field you are going to use to identify the desired record(s).  Then select that field from "delete record(s)" section below.</$
                                <label>ProductID</label>
                                <input type="text" name="productIDtx" value=""/></br>
                                <label>CategoryID</label>
                                <input type="text" name="categoryIDtx" value=""/></br>
                                <label>Product Code</label>
                                <input type="text" name="productCodetx" value=""/></br>
                                <label>Product Name</label>
                                <input type="text" name="productNametx" value=""/></br>
                                <label>List Price</label>
                                <input type="text" name="listPricetx" value=""/></br>
                                <label>Category Name</label>
                                <input type="text" name="categoryNametx" value=""/></br>
                        <p>Delete Record(s):  Select desired field below.  Don't forget to complete the information for the record in question above:</p>
                                <input type="radio" name="remove" value="productID"/>
                                <label>ProductID</label><br />
                                <input type="radio" name="remove" value="categoryID"/>
                                <label>CategoryID</label><br />
                                <input type="radio" name="remove" value="productCode"/>
                                <label>Product Code</label><br />
                                <input type="radio" name="remove" value="productName"/>
                                <label>Product Name</label><br />
                                <input type="radio" name="remove" value="listPrice"/>
                                <label>List Price</label><br />
                                <input type="radio" name="remove" value="categoryName"/>
                                <label>Category Name</label><br />
                        <p>To Retrieve Record(s): Select field(s) below you want to see from the list below.</p>
                                <input type="checkbox" name="productIDcb"/>
                                <label>ProductID</label><br />
                                <input type="checkbox" name="categoryIDcb"/>
                                <label>CategoryID</label><br />
                                <input type="checkbox" name="productCodecb"/>
                                <label>Product Code</label><br />
                                <input type="checkbox" name="productNamecb"/>
                                <label>Product Name</label><br />
                                <input type="checkbox" name="listPricecb"/>
                                <label>List Price</label><br />
                                <input type="checkbox" name="categoryNamecb"/>
                                <label>Category Name</label><br />
                        <p>Select the appropriate action based on your selection from above:</p>
                                <input type="radio" name="operation" value="retrieve"/>
                                <label>Retrieve Information</label><br />
                                <input type="radio" name="operation" value="addition"/>
                                <label>Add Information</label><br />
                                <input type="radio" name="operation" value="delete"/>
                                <label>Delete Information</label><br />
                        <p><input type="submit" value="Submit Request"/></p>
                </form>
       </body>
</html>

display.php(这会从我的表单中捕获信息并调用函数...仍然包括在内,以防万一,但我想问题肯定出在下一个文件上。)

<?php
  require('database.php');

  $productIDtx = $_POST['productIDtx'];
  $categoryIDtx = $_POST['categoryIDtx'];
  $productCodetx = $_POST['productCodetx'];
  $productNametx = $_POST['productNametx'];
  $listPricetx = $_POST['listPricetx'];
  $categoryNametx = $_POST['categoryNametx'];


  if(isset($_POST['tableName']))
   {
        $table = $_POST['tableName'];
   }
  else
   {
        echo("Must select a table.<br>");
   }//endif

  if(isset($_POST['operation']))
   {
        $operation = $_POST['operation'];
   }
  else
   {
        echo("Must select an action.<br>");
        exit();
   }//endif

   if(isset($_POST['remove']))
   {
        $remove = $_POST['remove'];
   }
   else
   {
        $remove = "";
   }//endif

   if(isset($_POST['productIDcb']))
   {
        $productIDcb = $_POST['productIDcb'];
   }
   else
   {
        $productIDcb = "";
   }//endif

   if(isset($_POST['categoryIDcb']))
   {
        $categoryIDcb = $_POST['categoryIDcb'];
   }
   else
   {
        $categoryIDcb = "";
   }//endif

   if(isset($_POST['productCodecb']))
   {
        $productCodecb = $_POST['productCodecb'];
   }
 else
   {
        $productCodecb = "";
   }//endif

   if(isset($_POST['productNamecb']))
   {
        $productNamecb = $_POST['productNamecb'];
   }
   else
   {
        $productNamecb = "";
   }//endif

   if(isset($_POST['listPricecb']))
   {
        $listPricecb = $_POST['listPricecb'];
   }
   else
   {
        $listPricecb = "";
   }//endif

   if(isset($_POST['categoryNamecb']))
   {
        $categoryNamecb = $_POST['categoryNamecb'];
   }
   else
   {
        $categoryNamecb = "";
   }//endif


  if($operation == 'retrieve')
        {
                include_once('product_list.php');
                show_products($productIDcb, $categoryIDcb, $productCodecb, $productNamecb, $listPricecb, $categoryNamecb);
        }
  elseif($operation == 'addition')
        {
                include_once('addprod.php');
                add($table, $productIDtx, $categoryIDtx, $productCodetx, $productNametx, $listPricetx, $categoryNametx);
        }
  elseif($operation == 'delete')
        {
                include_once('deleteprod.php');
                delete($table, $remove, $productIDtx, $categoryIDtx, $productCodetx, $productNametx, $listPricetx, $categoryNametx);
        }
  else
        {
                echo('<p>Select an action:  Retrieve, Add, or Delete. </p>');  //this code shouldn't ever happen because $operation is tested above but I put it in here in case an errors at this point in the code.
                exit();
        }//endif

?>

product_list.php(我认为问题一定出在$ rSET或之后的某个地方,因为$ theQuery的回显显示正确。但这对其他数据库也有效。所以我很茫然。)

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


  function show_products($productIDcb, $categoryIDcb, $productCodecb, $productNamecb, $listPricecb, $categoryNamecb)
  {
        global $db;
        $theQuery = 'select ';
        $list = "";
        if($productIDcb == "")
         {
                $theQuery == $theQuery;
         }
        else
         {
                $theQuery .= 'p.productID, ';
         }//endif

        if($categoryIDcb == "")
         {
                $theQuery == $theQuery;
         }//endif

        else
         {
                $theQuery .= 'c.categoryID, ';

         }//endif

        if($productCodecb == "")
         {
                $theQuery == $theQuery;
         }
        else
         {
                $theQuery .= 'p.productCode, ';

         }//endif

        if($productNamecb == "")
         {
                $theQuery == $theQuery;
         }
        else
         {
                 $theQuery .= 'p.productName, ';
         }//endif

        if($listPricecb == "")
         {
                $theQuery == $theQuery;
         }
        else
         {
                $theQuery .= 'p.listPrice, ';
         }//endif

        if($categoryNamecb == "")
         {
                $theQuery == $theQuery;
         }
        else
         {
                $theQuery .= 'c.categoryName, ';
         }//endif

        $theQuery .=" '' from (categories c, products p) where (c.categoryID = p.categoryID);";
        echo($theQuery);
        echo('<br>');

        //***I THINK THE ISSUE MUST BE SOMEWHERE AFTER THIS***

        $rSet = $db -> query($theQuery);
        foreach($rSet AS $results)
          {
              $list .=' '.$results[0];
                if(isset($results[1]))
                {
                   $list .=' '.$results[1];
                }
                if(isset($results[2]))
                {
                   $list .=' '.$results[2];
                }
                if(isset($results[3]))
                {
                   $list .=' '.$results[3];
                }
                if(isset($products[4]))
                {
                   $list .=' '.$results[4];
                }
              $list .="<br>";
          }//end foreach
        echo($list);
        echo('<br>');
        echo('<a href="index.html">Music Store Database</a>');

}//end function
?>

产品数据库查询(这一项有效)

从(类别c,产品p)中选择p.productID,c.categoryID,p.productCode,p.productName,p.listPrice,'',其中(c.categoryID = p.categoryID);

1 1层护舷Stratocaster

2 1 les_paul Gibson Les Paul

3 1 SG吉布森SG

4 1 fg700s雅马哈FG700S

5 1沃什伯恩沃什伯恩D10S

6 1罗德里格斯·罗德里格斯·卡瓦列罗11

7 2精度挡泥板精度

8 2 hofner Hofner图标

9 3路德维希路德维希5件套Cy片

10 3多摩多摩5片装Set鼓


类别数据库查询(查询看起来正确但数据错误)

从(类别c,产品p)中选择c.categoryID,c.categoryName,'',其中(c.categoryID = p.categoryID);

1把吉他

1把吉他

1把吉他

1把吉他

1把吉他

1把吉他

2贝司

2贝司

3鼓

3鼓

它应该列出以下内容

1把吉他

2贝司

3鼓

11测试

15测试

20测试

第33章

40测试

(注意:这些测试来自于我测试add函数的时间-如果您想知道的话)。

您的查询将导致笛卡尔积,该笛卡尔积将返回重复的结果。请尝试使用INNER JOINCROSS JOIN

SELECT categoryID, categoryName
FROM categories c
CROSS JOIN products p
WHERE (c.categoryID = p.categoryID)

谢谢以上评论的所有人,让我明白这一点。 正如andrewsi指出的那样,SELECT DISTINCT代码确实解决了重复问题,但由于(c.categoryID = p.categoryID),因此无法显示整个类别表。 因此,我添加了if语句来处理这两种情况,这也消除了对DISTINCT的需要。 这是修复它的代码。

if($theQuery == 'select c.categoryID, c.categoryName, ')
        {
                $theQuery = "";
                $theQuery = "select * from categories;";
        }
        else
        {
                $theQuery .=" '' from (categories c, products p) WHERE (c.categoryID = p.categoryID);";
        }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM