简体   繁体   中英

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'. 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.

 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. :(

// 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 ? If you are using implode then should be an array like $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). It looks like what you have in your script would be interpreted by the sql parser as column names.

Try this 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 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. I don't know why i'm not work with id at the begin... my bad.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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