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?

Recommended Answers

All 7 Replies

when using brackets "sizes[]" in input name php gets the variable as an array

$query = "UPDATE style SET sizes = '$sizes'
                WHERE id = '$i'";

you need to loop through the array of sizes and build your "size" string

$sizeString = '';
foreach($sizes as $size) {
    $sizeString .= ($sizeString == '' ? '' : ', ');
    $sizeString .= "[".$size."]";
}
$query = "UPDATE style SET sizes = '$sizeString'
                WHERE id = '$i'";

I modified the code in my program as you explained to put all the sized for one record into a string the run my loop like this, but it does not update my database.

<?php
if (isset ($_POST['submit'])) {
	$count = $_POST['count'];
	$i = 0;
	while ($i <= $count) {
		$sizeString = '';
		foreach ($sizes as $size) {
			$sizeString .= ($sizeString == '' ? '' : ', ');
			$sizeString .= "[".$size."]";
		}
    	$query = "UPDATE style SET sizes = '$sizeString'
                WHERE id = '$i'";
				mysql_query($query);
                ++$i;
	}
echo '<h2>Record Updated</h2>';
}
?>

try changing mysql_query($query); to

$result = mysql_query($query);
if (!$result) {
    die('Invalid query: ' . mysql_error());
}

and post the results

also you're not posting id to the server anywhere

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="hidden" name="id[]" value="<?php echo $id?>"/>
<input type="checkbox" name="sizes<?php echo $id?>[]" value="39" <?php if (preg_match("/39/", "$sizes")) { echo "checked";} else {echo "";} ?> />
<label for="size1">Size 39</label>
 
<input type="checkbox" name="sizes<?php echo $id?>[]" value="40" <?php if (preg_match("/40/", "$sizes")) { echo "checked";} else {echo "";} ?> />
<label for="size1">Size 40</label>    
 
<input type="checkbox" name="sizes<?php echo $id?>[]" value="41" <?php if (preg_match("/41/", "$sizes")) { echo "checked";} else {echo "";} ?> />
<label for="size1">Size 41</label>    
 
<input type="checkbox" name="sizes<?php echo $id?>[]" 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
}
?>

then your loop for your array

$ids = $_POST['id'];
foreach($ids as $id) {
    $sizes = $_POST['sizes'.$id];
    $sizeString = '';
    foreach($sizes as $size) {
        $sizeString .= ($sizeString == '' ? '' : ', ');
        $sizeString .= "[".$size."]";
    }
    $query = "UPDATE style SET sizes = '$sizeString'
                WHERE id = '$id'";				
    mysql_query($query);
}

Still no record was stored in the database. take a look at the complete update code

<?php
if (isset ($_POST['submit'])) {
	$count = $_POST['count'];
	$i = 0;
	while ($i <= $count) {
		$sizeString = '';
		foreach ($sizes as $size) {
			$sizeString .= ($sizeString == '' ? '' : ', ');
			$sizeString .= "[".$size."]";
		}
    	$query = "UPDATE style SET sizes = '$sizeString'
                WHERE id = '$i'";
				$result = mysql_query($query);
				if (!$result) {
					die('Invalid query: ' .mysql_error());
				}
                ++$i;
	}
echo '<h2>Record Updated</h2>';
}
?>

check my last reply

check my last reply

Yaay!!! you just made my day. Thanks

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.