user2773365
user2773365

Reputation: 23

Mysql IN() not working

I have a checkbox form which passes an array of id values. I then implode the array like this:

$ship = $_POST['result'];
$array=implode(",", $ship);
$shipping=ship_update($array);

The Mysql query function looks like this:

function ship_update($array){
    global $MEMS;
    echo $array;
    $query="SELECT * FROM Inventory
        WHERE MEMS_ID IN ('$array')
            ORDER BY WAFER ASC, RC ASC";
    $shipping=$MEMS -> exec($query);
    return $shipping;
}

When I run this code, $shipping returns an empty query. What am I doing wrong?

Upvotes: 0

Views: 1197

Answers (3)

M Khalid Junaid
M Khalid Junaid

Reputation: 64466

Try your query without ' single quotes

SELECT * FROM Inventory
        WHERE MEMS_ID IN ($array)
            ORDER BY WAFER ASC, RC ASC

Upvotes: 1

John Woo
John Woo

Reputation: 263683

This should be

$array = "'" . implode("','", $ship) . "'";

And in your query,

$query="SELECT * FROM Inventory
        WHERE MEMS_ID IN ($array)    // remove the single quotes to
        ORDER BY WAFER ASC, RC ASC"; // avoid syntax error

warning, this is still vulnerable with sql injection.

Upvotes: 2

aynber
aynber

Reputation: 23001

There aren't any single quotes in the middle of your array. Change

$array=implode(",", $ship);

to

$array=implode("','", $ship);

Upvotes: 0

Related Questions