herghost Posted March 21, 2010 Share Posted March 21, 2010 Hi all, I have been having some problems with saving a checkbox to a database. (Please see other thread http://www.phpfreaks.com/forums/index.php?topic=291884.new;topicseen#new) . Basically what I want to know is If I am going about it correctly. My form input looks like this: <tr><td><input type="checkbox" name="features[]" value="1"> Ability to Change MX</input></td></tr> <tr><td><input type="checkbox" name="features[]" value="1"> Ability to Trace an email address</input></td></tr> <tr><td><input type="checkbox" name="features[]" value="1"> Addon Cgi Scripts</input></td></tr> <tr><td><input type="checkbox" name="features[]" value="1"> Addon Domain Manager</input></td></tr> <tr><td><input type="checkbox" name="features[]" value="1"> Advanced DNS Zone Editor</input></td></tr> <tr><td><input type="checkbox" name="features[]" value="1"> Advanced Guestbook</input></td></tr> <tr><td><input type="checkbox" name="features[]" value="1"> Agora Shopping Cart</input></td></tr> <tr><td><input type="checkbox" name="features[]" value="1"> Analog Stats</input></td></tr> <tr><td><input type="checkbox" name="features[]" value="1"> Apache Handlers Manager</input></td></tr> <tr><td><input type="checkbox" name="features[]" value="1"> Autoresponder Manager</input></td></tr> <tr><td><input type="checkbox" name="features[]" value="1"> Awstats Stats</input></td></tr> <tr><td><input type="checkbox" name="features[]" value="1"> Backup Manager</input></td></tr> I have about a 100 entries like this. in the database the fields have a default value of 0 and are stored as int's, and if a text box is selected then it should insert a 1 and leave the default value of 0 if not checked? Am I doing the right thing so far? Now my form entry data looks like this: <?php session_start(); include('../../config/connect.php'); $name = (isset($_POST['name'])) ? trim($_POST['name']) : ''; $checkBoxes = array(features[]); foreach ($checkBoxes as $key) { $data[$key] = isset($_POST[$key])?trim($_POST[$key]) : ''; } $formBoxes = implode(', ', $checkBoxes); $formData = implode(', ', $data); $query = "INSERT INTO product_features (name, $formBoxes) VALUES ('$name', $formData)"; echo $query; if (!mysql_query($query)) { die('Error: ' . mysql_error()); } else { $_SESSION['featureadd'] = 'featureadd'; header("Location: $adminbase/viewfeaturelist.php"); } ?> Which does not work as I am doing something wrong around here: $checkBoxes = array(features[]); Can someone point me back in the right direction? Thanks Link to comment https://forums.phpfreaks.com/topic/195977-checkbox-help/ Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.