简体   繁体   English

使用PHP在mysql数据库中进行数组搜索

[英]Array search in mysql database with PHP

There's a DB where I stored the products details. 我有一个数据库,用于存储产品详细信息。 (id, name, price, category, unit) (编号,名称,价格,类别,单位)

Firstly i'm listed the products for select in a php file named 'input.php'. 首先,我在名为“ input.php”的php文件中列出了可供选择的产品。 It works like a charm. 它像一种魅力。

// SQL query

    $query = "SELECT    * 
              FROM      products
              ORDER BY  p_name
              ASC";

            $result = mysql_query($query);
            if($result === FALSE) {die(mysql_error());}?>

                <table width="100%">
                    <tr>
                        <td></td>
                        <td width="800px"><h1>Product name</h1></td>
                        <td width="400px"><h1>Product price</h1></td>
                        <td>Category</td>
                    </tr>
                </table>
            <?
            while ($row = mysql_fetch_assoc($result)) {?>
                <table>
                    <tr>
                        <td><input type="checkbox" name="pro_name[]" value="<?=$row['p_name']?>"></td>
                        <td width="800px" align="left"><b><?=$row['p_name']?></b></td>
                        <td width="400px" align="left"><?=$row['p_price']?></td>
                        <td align="center"><?=$row['p_cat']?></td>
                    </tr>
                </table>
                <?}

I submit the data to 'index.php' and handle the variables in there. 我将数据提交到“ index.php”并在那里处理变量。

 if($_SERVER['REQUEST_METHOD']== "POST"){

// Variables

$c_name = $_POST['c_name'];             //  ['company name']
$c_consumption = $_POST['c_consumption'];   //  ['company consumptions']
$f_price = $_POST['f_price'];               //  ['fuel price']
$o_sales = $_POST['o_sales'];               //  ['sales']
$pro_name = $_POST['pro_name'];         //  ['products names']
$pro_id = $_POST['pro_id'];             //  ['product id']  
$pro_price = $_POST['pro_price'];           //  ['product price']

The third step where i'm stucked...I need to list the picked products details. 我陷入困境的第三步...我需要列出挑选的产品详细信息。

My idea is to select the details from the db with search for the product names what i picked earlier. 我的想法是从数据库中选择详细信息,并搜索我之前选择的产品名称。

I tried it with IN, LIKE, implode....etc but it didn't work. 我尝试了IN,LIKE,内爆...等,但没有成功。 :( :(

// SQL query

      $query = "SELECT  * 
                      FROM      products
                      WHERE     p_name IN (".implode(',', $pro_name).")
                      ORDER BY  p_name
                      ASC";

            $result = mysql_query($query);
            if($result === FALSE) {die(mysql_error());}

            while ($row = mysql_fetch_array($result)) {?>
                <table>
                    <tr>                            
                        <td width="800px" align="left"><b><?=$row['p_name']?></b></td>
                        <td width="400px" align="left"><?=$row['p_price']?></td>
                        <td align="center"><?=$row['p_cat']?></td>
                    </tr>
                </table>
                <?}
                }

EDIT: 编辑:

 var_dump($pro_name); result

 array(3) { [0]=> string(14) "12 V szivattyú" [1]=> string(17) "120 l/p szivattyú" [2]=> string(28) "24 hónapra bővített garancia" } 

What is the $pro_name value ? $ pro_name的值是多少? If you are using implode then should be an array like $pro_name=array('Fruit','Chocolate','Mirror'); 如果您使用的是爆破式,则应使用$ pro_name = array('Fruit','Chocolate','Mirror');之类的数组; Then query would be: 然后查询将是:

SELECT  * 
                      FROM      products
                     -> WHERE     p_name IN ("Fruit,Chocolate,Mirror")
                      ORDER BY  p_name
                      ASC

But the problem is that the query is still wrong, because of ", then u should remove and change to 但是问题在于查询仍然是错误的,因为存在“”,那么您应该删除并更改为

WHERE     p_name IN (implode(',', $pro_name))

Then query will be like 然后查询将像

SELECT  * 
                          FROM      products
                         -> WHERE     p_name IN (Fruit,Chocolate,Mirror)
                          ORDER BY  p_name
                          ASC

If the p_name is a string type, I'm pretty sure the $pro_names that you are imploding need to be delimited with ' (single quote delimiter). 如果p_name是字符串类型,我很确定您要插入的$ pro_names需要用'(单引号分隔符)定界。 It looks like what you have in your script would be interpreted by the sql parser as column names. 看起来您脚本中的内容将由sql解析器解释为列名。

Try this SQL 试试这个SQL

 $pr_name = ltrim(implode(',' , '".$pro_name."'), ',');  
 $query = "SELECT  * 
           FROM products
           WHERE p_name IN ($pr_name)
           ORDER BY p_name
           ASC";

Thanks for the replies. 感谢您的答复。 Help me a lot to solve the problem. 帮助我很多,以解决问题。

Here's my solution: 这是我的解决方案:

<input type="checkbox" name="pro_id[]" value="<?=$row['p_id']?>">

It creates an array: 它创建一个数组:

string(24) "6, 7, 10, 20, 21, 49, 54"

Than

$list = implode(', ', $pro_id);

SQL 的SQL

// SQL query
$query = " SELECT      * 
           FROM       products
           WHERE      p_id IN ($list)
           ORDER BY   p_name
           ASC ";

I think the names contains characters what the query can't handle so now the query based on ID. 我认为名称包含查询无法处理的字符,因此现在基于ID的查询。 I don't know why i'm not work with id at the begin... my bad. 我不知道为什么我一开始就不使用id。

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

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