SELECT ... FROM ... WHERE ...在选择输出时将多个输入与多个列组合时出现问题

时间:2018-01-11 06:34:40

标签: php mysqli

我是PHP编程的新手,我正在开发我的第一个程序..这是仓库管理系统的开始,所以我需要能够查询零件号,EAN,项目名称等。有时,所有信息都是已知的,或者在用户输入时完全匹配,但有时用户只知道EAN的一部分,或者想要检查具有相似名称的所有项目,因此结果需要像输入一样由用户提供。

当用户仅在表单中的一个输入中输入信息时,一切正常(例如,只有部件号的全部或部分被添加到'partnumber'输入中,并且它正确返回带有info查询的相关行' d),但是当用户向查询表添加多个输入时(例如用户将数据输入'partnumber'和'EAN'输入),结果最终成为表中的每个项目..与'SELECT相同* FROM table;'。

有没有办法在多列上查询和输出多个输入的数据?我到处搜索过这个,但还没有找到与我的需求相关的答案......(或者至少有一个我的理解水平)。任何正确方向的帮助都会很棒!

到目前为止我用过的SQL查询:

$query = "
SELECT partNumber
     , EAN
     , UPC
     , itemDescription
     , SNFlag
     , idClass
     , idType 
  FROM productinfo_table 
 WHERE partNumber LIKE '$partNumber' 
    OR EAN LIKE '$EAN' 
    OR itemDescription LIKE '$itemDescription' 
    OR SNFlag LIKE '$SNFlag'
";

$query = "
SELECT partNumber
     , EAN
     , UPC 
     , itemDescription
     , SNFlag
     , idClass
     , idType 
  FROM productinfo_table 
 WHERE (partNumber,EAN,itemDescription,SNFlag) IN LIKE ('$partNumber','$EAN','$itemDescription','$SNFlag')";

其他一些......

testissue.php

<?php //testissue.php
//establish connection
require_once "login.php";
$db_server = mysqli_connect($db_hostname,$db_username,$db_password,$db_database);
if(!$db_server) printf('Error connecting to database: %s',mysqli_error($db_server));

//if loop to acquire variables
//if all post array elements are NOT empty
if(!empty($_POST['partNumber']) ||
   !empty($_POST['EAN']) ||
   !empty($_POST['itemDescription']) ||
   !empty($_POST['SNFlag'])) {
    //if partNumber is not empty
    if(!empty($_POST['partNumber'])) {
        $partNumber = '%';
        $partNumber .= $_POST['partNumber'];
        $partNumber .= '%';
    } else {
        $partNumber = '';
    }
    //if EAN is not empty
    if(!empty($_POST['EAN'])) {
        $EAN = '%';
        $EAN .= $_POST['EAN'];
        $EAN .= '%';
    } else {
        $EAN = '';
    }
    // if itemDescription is not empty
    if(!empty($_POST['itemDescription'])) {
        $itemDescription = '%';
        $itemDescription .= $_POST['itemDescription'];
        $itemDescription .= '%';
    } else {
        $itemDescription = '';
    }
    //if SNFlag is not empty
    if(!empty($_POST['SNFlag'])) {
        $SNFlag = '%';
        $SNFlag .= $_POST['SNFlag'];
        $SNFlag .= '%';
    } else {
        $SNFlag = '';
    }
    //echo variables to confirm set, for testing
    echo "$partNumber<br/>";
    echo "$EAN<br/>";
    echo "$itemDescription<br/>";
    echo "$SNFlag<br/>";

    //query to pull data to insert into table rows
    //$query = "SELECT partNumber,EAN,UPC,itemDescription,SNFlag,idClass,idType FROM productinfo_table WHERE partNumber LIKE '$partNumber' OR EAN LIKE '$EAN' OR itemDescription LIKE '$itemDescription' OR SNFlag LIKE '$SNFlag'";
    $query = "SELECT partNumber,EAN,UPC,itemDescription,SNFlag,idClass,idType FROM productinfo_table WHERE (partNumber,EAN,itemDescription,SNFlag) IN LIKE ('$partNumber','$EAN','$itemDescription','$SNFlag')";
    $result = mysqli_query($db_server,$query);
    if(!$result) printf("Error querying database: %s",mysqli_error($db_server));
    $rows = mysqli_num_rows($result);   
} 
//if all post array elements ARE empty
else {
    echo "empty post array";
    $rows = '';
}
//echo form input
echo <<<HERE
<pre>
<form action='testissue.php' method='post'>
  Part No. <input type='text' name='partNumber' />
       EAN <input type='text' name='EAN' />
Item Desc. <input type='text' name='itemDescription' />
   SN Flag <input type='text' name='SNFlag' />
           <input type='submit' value='Search' />
</form>
</pre>
HERE;
//print post array to confirm set values, for testing
echo "<br/>";
print_r($_POST);
echo "<br/><br/>";
//echo table for output
echo <<<HERE
<table>
<tr><th>Part No.</th> <th>EAN</th> <th>UPC</th> <th>Item Desc.</th> <th>SN Flag</th> <th>Class ID</th> <th>Type ID</th></tr>
HERE;
// for loop function to populate items in table
for($a=0;$a<$rows;++$a){
    echo "<tr>";
    $col = mysqli_fetch_row($result);
    for($b=0;$b<7;++$b) echo "<td>$col[$b]</td>";
    echo "</tr>";
}
echo "</table>";
//close connection
mysqli_close($db_server);

?>

如果您需要其他任何帮助或提供任何改进,请告诉我。

非常感谢!

2 个答案:

答案 0 :(得分:0)

$sql="SELECT *FROM table where partNumber = '$partnumber'order by id ASC";

答案 1 :(得分:0)

您需要使用AND子句而不是OR

AND会为您提供符合WHERE子句

中所有条件的行

OR将返回符合WHERE子句

中任何条件的行
$query = "
SELECT partNumber
    , EAN
    , UPC
    , itemDescription
    , SNFlag
    , idClass
    , idType 
FROM productinfo_table 
WHERE partNumber LIKE '$partNumber' 
    AND EAN LIKE '$EAN' 
    AND itemDescription LIKE '$itemDescription' 
    AND SNFlag LIKE '$SNFlag'
";

注意:请注意注释中提供的建议,以防止SQL注入