Jump to content

How to save checkboxes to MySQL?


JsusSalv

Recommended Posts

Hello Everyone:

 

I have an UPDATING form that gets its information from one MySQL table.  However, one of the fields is made up of about 20 checkboxes and the values come from a 2nd MySQL table on the same database.

I must be missing the simplest thing but I can't seem to get the checkboxes to save to the 1st MySQL table.  Here's the code, if anyone can help point out the flaws please do:

 

<?php
$expected = array('category');

// Get details of selected record.
if ($_GET && !$_POST) {
  if (isset($_GET['set_id']) && is_numeric($_GET['set_id'])) {
    $set_id = $_GET['set_id'];
}
  else {
    $set_id = NULL;
}
  if ($set_id) {
    $sql = "SELECT * FROM xxxxxxxxx WHERE set_id = $set_id";
  $result = mysql_query($sql) or die (mysql_error());
  $row = mysql_fetch_assoc($result);
}
  }
// If form has been submitted, update record.
if (array_key_exists('update', $_POST)) {
  // prepare expected items for insertion in to database
  foreach ($_POST as $key => $value) {
    if (in_array($key, $expected)) {
      ${$key} = mysql_real_escape_string($value);
      }
    }
  // Abandon the process if primary key is invalid.
  if (!is_numeric($set_id)) {
    die('Invalid request');
}
  
  $category = array();
  
  // Prepare the first SQL query.
  $sql = "UPDATE xxxxxxx SET category = '$category' WHERE set_id = $set_id";

  // Submit the query and redirect if successful.
  $done = mysql_query($sql) or die(mysql_error());
  }

// Redirect page on success or if $article_id is invalid.
if ($done || !isset($set_id)) {
  header('Location: ./xxxxxxx.php');
  exit;
  }

print '<?xml version="1.0" encoding="UTF-8"?>';
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html lang="en-us" xml:lang="en-us" xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="application/xhtml+xml; charset=utf-8" />
<meta http-equiv="Content-Language" content="en-us" />
<title>XXXXXXXX</title>
</head>
<body>
<?php if (empty($row)) {
?>
<p class="warning">Invalid request: record does not exist.</p>
<?php } 
else {
?>
<form id="XXXXX" name="XXXX" method="post" action="">
  <div style="width:100%; margin: 0 0 0.5em; 0;">
  <div style="float:left; width:15%; text-align: right; font-weight:bold; margin: 0 0.3em 0 0;"><label for="category">Categories:</label></div>
  <div style="float:left; width:80%;">
    <?php
        // The checkbox values come from a 2nd table that can be updated.
      $allCats = "SELECT * FROM XXXXXXXX";
      $catList = mysql_query($allCats) or die (mysql_error());
      $category = isset($category) ? $category : array('None selected');

     while ($cat = mysql_fetch_assoc($catList)) {
     ?>
        <input type="checkbox" name="category[]" id="category" value="<?php echo $cat['category']; ?>"
        <?php
          if ($OK && isset($category) && in_array($cat['category'], $category)) {
		echo 'checked="checked"';}
	 ?>
	   /><?php echo $cat['category']; ?>
        <?php } ?>
  </div>
  <br clear="both">
  </div>

  <div style="width:100%; margin: 0 0 0.5em; 0;">
    <input type="submit" name="update" value="UPDATE" />
    <input name="set_id" type="hidden" value="<?php echo $row['set_id']; ?>" />
    <input TYPE="button" onClick="parent.location='xxxxxxxx.php'" value="CANCEL">
  <br clear="both">
</form>
<?php } ?>
</body>
</html>

Link to comment
https://forums.phpfreaks.com/topic/121219-how-to-save-checkboxes-to-mysql/
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.