I Need to to update multiple checkbox values on multiple records at the same time. When the form loads, it should display the sizes already selected for each style as stored in the style table and if we select or deselect any sizes on a style it should update the sizes field with the appropriate all the sizes checked.
THE MySQL DATABASE
--
-- Table structure for table `style`
--
CREATE TABLE IF NOT EXISTS `style` (
`id` mediumint(8) NOT NULL AUTO_INCREMENT,
`name` varchar(255) NOT NULL,
`colour` varchar(255) NOT NULL,
`sizes` text NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=3 ;
--
-- Dumping data for table `style`
--
INSERT INTO `style` (`id`, `name`, `colour`, `sizes`) VALUES
(1, 'Ndombolo', 'Blue', '[39],[40],[41],[42]'),
(2, 'Namondo', 'Green', '[39],[40],[41],[42]');
THE FORM
Retrieve the information from the database and if any check box changes are made to it it will update the record with the new check box selected.
<?php
$db_host = "localhost"; // Databse host
$db_name = "checkbox"; // Database name
$db_username = "root"; // Database username
$db_pass = ""; // Database password
$db_table = "style"; // Database table name
// Establish database Connection
mysql_connect("$db_host","$db_username","$db_pass")
// Disconnect if database user detail are wrong
or die(mysql_error('Cannot conect to database server'));
// Select database
mysql_select_db("$db_name")
// Disconnect if database does not exist
or die("Database does not exist");
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Multiple Shoe Size Edit Via Checkbox</title>
</head>
<body>
<?php
// If the page was modified and submited run this
if (isset ($_POST['submit'])) {
// create loop to update multiple rows in the table
// $size = count($_POST['colour_sizes']);
$count = $_POST['count'];
$i = 0;
while ($i <= $count) {
$sizes = $_POST['sizes'];
$id = $_POST['id'];
$query = "UPDATE style SET sizes = '$sizes'
WHERE id = '$i'";
mysql_query($query);
++$i;
}
echo '<h2>Records updated</h2>';
}
?>
<form action="" method="post" name="submit">
<table width="100%" border="0">
<tr>
<td>ID</td>
<td>Name</td>
<td>Colour</td>
<td>Sizes</td>
</tr>
<?
// Loop through the table and display the records in tabular format
$query = "SELECT * FROM style";
$result = mysql_query ($query);
$count = mysql_num_rows($result); // Count table rows
while ($row = mysql_fetch_array($result))
{
$id = $row["id"];
$name = $row["name"];
$colour = $row ["colour"];
$sizes = $row ["sizes"];
?>
<tr>
<td><?php echo $id?></td>
<td><?php echo $name?></td>
<td><?php echo $colour?></td>
<td>
<input type="checkbox" name="sizes[]" value="39" <?php if (preg_match("/39/", "$sizes")) { echo "checked";} else {echo "";} ?> />
<label for="size1">Size 39</label>
<input type="checkbox" name="sizes[]" value="40" <?php if (preg_match("/40/", "$sizes")) { echo "checked";} else {echo "";} ?> />
<label for="size1">Size 40</label>
<input type="checkbox" name="sizes[]" value="41" <?php if (preg_match("/41/", "$sizes")) { echo "checked";} else {echo "";} ?> />
<label for="size1">Size 41</label>
<input type="checkbox" name="sizes[]" value="42" <?php if (preg_match("/42/", "$sizes")) { echo "checked";} else {echo "";} ?> />
<label for="size1">Size 42</label>
</td>
</tr>
<?php
// End the while loop
}
?>
</table>
<input type="hidden" name="count" value="<?php echo "$count" ?>" />
<input type="submit" class="chromebutton" value="Update" name="submit"/>
</form>
</body>
</html>
Currently when I check the boxes of the values that I need to update. it just update the sizes field with the word "Array" but I want it to insert the values in in brackets like this [39], [40], [41] if those sizes where checked and updated.
Any ideas?