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

时间:2013-11-24 03:12:27

标签: php mysql

我只有两个数据库,它们都运行在同一个选择脚本中。该脚本适用于一个,但另一个只显示数据库的前三行,并重复显示它们。

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

请帮忙。 。谢谢你!

index.html(这是形式......我不认为这个问题在这里......但我包括它以防万一...我认为问题必须在product_list.php中删除)< / p>

<!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
?>

产品数据库查询(这个工作)

选择p.productID,c.categoryID,p.productCode,p.productName,p.listPrice,''from(categories c,products p)where(c.categoryID = p.categoryID);

<1> 1 strat Fender Stratocaster

2 1 les_paul Gibson Les Paul

3 1 sg Gibson SG

4 1 fg700s雅马哈FG700S

5 p Washburn Washburn D10S

6 1 rodriguez Rodriguez Caballero 11

7 2精密挡泥板精度

8 2 hofner Hofner Icon

9 3路德维希路德维希五件套鼓镲套装

10 3 tama Tama带镲片的5件套鼓


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

选择c.categoryID,c.categoryName,''from(categories c,products p)where(c.categoryID = p.categoryID);

1吉他

1吉他

1吉他

1吉他

1吉他

1吉他

2 Basses

2 Basses

3鼓

3鼓

它应列出以下内容

1吉他

2 Basses

3鼓

11测试

15测试

20测试

33 test33

40测试

(注意:测试来自我测试添加功能的时候 - 如果你想知道的话)。

2 个答案:

答案 0 :(得分:0)

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

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

答案 1 :(得分:0)

感谢上面评论过的所有人,让我明白这一点。正如andrewsi指出的那样,SELECT DISTINCT代码确实修复了重复的问题,但由于(c.categoryID = p.categoryID),整个类别表都不会显示。所以我添加了if 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);";
        }