2

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

1 Answer 1

4

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

Sign up to request clarification or add additional context in comments.

4 Comments

Ohhh I like this way better than mine!
Thank you very much! Works like a charm. Reading up on implode to learn about it. Thanks again for the info!
@Phill Pafford What would you do if you wanted a quoted version of implode (as in string values rather than integer values?)
$query .= "order_number IN ('".implode("','", $id)."') ";

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.