I am having a trouble UPDATING multiple fields using PHP. First, here's what I am aiming to do: I have made a checkbox group in an index page. Clicking the submit button will POST the value (product ID) of the checked boxes to another page. In later page, the values were stored in an array. With this array of values, I would like to UPDATE a boolean column in mysql where the PRIMARY ID key in the MYSQL table is equal to the values stored in the array. How could I do this? Here's what I have done: //CHANGE Icons' selected VALUE TO TRUE / 1 $changeSelected = $_POST['iconPost']; foreach ($changeSelected as $selected) { mysql_query('UPDATE images SET selected = 1 WHERE ID = $selected'); echo $selected . '<br>'; } Code (markup):
You should use Mysqli or PDO instead of Mysql to access your database from PHP. Here is a PDO example: $dbh = new PDO('mysql:host=localhost;dbname=myDB', $userName, $password); $inParam = str_repeat('?,', count($changeSelected) - 1) . '?'; $sth = $dbh->prepare("UPDATE images SET selected = 1 WHERE ID IN ($inParam)"); $sth->execute(array_values($changeSelected)); Code (markup): It uses a PDO prepared statement to pass your array to the UPDATE query.
hey just try to visualize the following code . you can do it easily <?php echo ' <table border="1"> <tr> <td>Week</td> <td>Day</td> <td>Posting</td> </tr> '; $ctype = (int) $_GET['id']; //<-- More security by forcing ctype to be an integer. $query = mysql_query("select * from table where ctype= '$ctype'"); while ($rws = mysql_fetch_array($query){ echo ' <tr> <td>'.$rws['week'].'</td> <td>'.$rws['day'].'</td> <td>'.$rws['posting'].'</td> </tr> '; } echo '</table>'; ?>