Jason
Jason

Reputation: 265

Updating Multiple SQL Rows

My setup is like this:

Admin search for orders depending on say address, or client name. Once all they orders are displayed for the search criteria they entered, they can select the orders using check-boxes. The code below handles that:

<form action="noJavaScript.php" name="theForm" method="post">
<table style="border: 1px solid black">
    <?php
        while ($row = mysql_fetch_assoc($result))
        {
            echo '<tr><td>';
            echo '<input type="checkbox" name="selected[]"  value="'.$row['order_number'].'"/>';
            echo '</td>';
            foreach ($row as $key => $value)
                echo '<td>'.htmlspecialchars($value).'</td>';
            echo '</tr>';
        }
    ?>
</table>
<input type="submit" name="submit" value="Edit/Modify Order" onClick="document.theForm.action='modify.php'">
<input type="submit" name="submit" value="Clone Order" onClick="document.theForm.action='clone.php'">
<input type="submit" name="submit" value="Delete Order" onClick="document.theForm.action='deleteorder.php'">
</form>

What I am trying to accomplish is depending on how many orders they select, 1, 5, 10, All of them, When they click on the Delete Order button, I want all those orders they selected to be updated in the database. I'm not sure how to do that. I have tried the following: This is the code on my deleteorder.php page:

$id = array();
$id = $_POST['selected'];

//Include database connection details
require_once('config_order.php');

//Connect to mysql server
$link = mysql_connect(DB_HOST, DB_USER, DB_PASSWORD);
if(!$link) {
    die('Failed to connect to server: ' . mysql_error());
}

//Select database
$db = mysql_select_db(DB_DATABASE);
if(!$db) {
    die("Unable to select database");
}


//Create query
$query = mysql_query("UPDATE PropertyInfo SET order_status = 'In-Active' WHERE  `order_number` LIKE $id[0]");

That doesn't work, as it only updates the value assigned to $id[0] and not all of the orders they might have selected.

Any help would be greatly appreciated! Thanks

Upvotes: 2

Views: 641

Answers (1)

Phill Pafford
Phill Pafford

Reputation: 85308

I'm assuming the $id is an array of all the id's selected:

$query  = "UPDATE PropertyInfo SET order_status = 'In-Active' WHERE ";
$query .= "`order_number` IN (".implode(',', $id).") ";
$results = mysql_query($query);

Using implode to comma separate the array values

Upvotes: 4

Related Questions