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

标签 php mysql

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

由于我是 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 strat Fender Stratocaster

2 1 les_paul 吉布森·莱斯·保罗

3 1 SG 吉布森 SG

4 1 fg700s 雅马哈 FG700S

5 1 Washburn Washburn D10S

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

7 2精度挡泥板精度

8 2 霍夫纳霍夫纳图标

9 3 ludwig Ludwig 5 件套鼓带镲片

10 3 tama Tama 5 件套鼓带镲片

<小时/>

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

从(类别 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测试33

40 次测试

(注意:测试来 self 测试 add 函数时 - 如果您想知道的话)。

最佳答案

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

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

关于PHP 查询正确但结果不匹配?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20170655/

相关文章:

javascript - 多行写入数据库,不添加

php - undefined variable : list in C:\xampp\htdocs\shed\select. php 第 115 行

php - 加载 Laravel View 时使用 MySQL 结果的缓存版本

mysql - 如何为Django(MySQL后端)中的每个表设置不同的存储引擎?

php - MySQL 文本搜索示例

php - 正则表达式获取前后N个词

php - 存在类时获取 X-Cart 错误代码 2(缺少类)

c# - MySQL 连接器网络 6.9.9 设置向导提前结束

php - Laravel 迁移 : Remove onDelete ('cascade' ) from existing foreign key

python - 为什么我的数据库不能在此 Python/Django 应用程序中运行?