Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I have a form. Inside that a number of checkbox present. So when someone selects some checkboxes(lets say we have selected two checkboxes Disk Space, Processor) and clicks on save. Then it should save in the database like this

id  attribute_name
1    Disk space
2    Processor


<form id="formID" method="post" action="" enctype="multipart/form-data">
  <input type="checkbox" name=""  value="Disk space"/>Disk space<br />
  <input type="checkbox" name=""  value="Color"/>Color<br />
  <input type="checkbox" name=""  value="Processor"/>Processor<br />
  <input type="submit" name="save" id="save" value="save"/>
</form>

So can someone tell me how to do this? I had made my insert query like this

INSERT INTO `ia_attributes`(`attribute_id`, `attribute_name`) VALUES ('', '".$attribute_values."')";

but this one entered all the selected attributes in a single column.

share|improve this question
 
See the answer in this question - stackoverflow.com/questions/6889065/… –  a.yastreb Jan 22 at 10:23
 
how you re getting $attribute_values in your insert query?Also give name= attrname[] to all your checkboxes –  user2936213 Jan 22 at 10:23
add comment

6 Answers

up vote 0 down vote accepted

How about this it will also validate the data before doing any insert query.

<form id="formID" method="post" action="" enctype="multipart/form-data">
  <input type="checkbox" name="type[]"  value="Disk space"/>Disk space<br />
  <input type="checkbox" name="type[]"  value="Color"/>Color<br />
  <input type="checkbox" name="type[]"  value="Processor"/>Processor<br />
  <input type="submit" name="save" id="save" value="save"/>
</form>

Then in PHP, the code will make sure if someone just submit without checking anything it will not try to execute the query.

if(isset($_POST["save"]) && isset($_POST["type"])){
        $types = $_POST["type"];
        if(sizeof($types) > 0 ){
            foreach($types as $type){
                    $qry= '';
                    $qry = "INSERT INTO `ia_attributes`(`attribute_id`, `attribute_name`) VALUES ('', '".$type."')";
                    // execute the above query

            }
        }
    }   
share|improve this answer
 
its working when I am selecting one checkbox and doing save. But when I am selecting multiple checkbox then it is inserting all the checkbox into one column. –  Jagdish Jan 22 at 10:34
 
Did you try the above code using the loop as I gave ? And changing the form to add a name to the checkboxes ? The above should work, all you need just add mysql_query() or mysqli_query() in place of // execute the above query in the above example. –  Abhik Chakraborty Jan 22 at 10:38
 
ok..that one is working fine. But can you tell me why it is inserting the same values again when the page is made refresh? So how to fix this issue. –  Jagdish Jan 22 at 10:45
 
Yes its because of POST, when u refresh the page it will re post the data which were previously posted. So thats browsers nature. All you can do is to redirect the page to somewhere once all the insert is done. –  Abhik Chakraborty Jan 22 at 10:47
add comment
<form id="formID" method="post" action="" enctype="multipart/form-data">
  <input type="checkbox" name="test[]"  value="Disk space"/>Disk space<br />
  <input type="checkbox" name="test[]"  value="Color"/>Color<br />
  <input type="checkbox" name="test[]"  value="Processor"/>Processor<br />
  <input type="submit" name="save" id="save" value="save"/>
</form>

insert query should be

foreach($_POST['test'] as $value) {
    INSERT INTO `ia_attributes`(`attribute_id`, `attribute_name`) VALUES ('', '".$value."')";
}
share|improve this answer
add comment

In yout HTML code do this:

<form id="formID" method="post" action="" enctype="multipart/form-data">
  <input type="checkbox" name="attrname[]"  value="Disk space"/>Disk space<br />
  <input type="checkbox" name="attrname[]"  value="Color"/>Color<br />
  <input type="checkbox" name="attrname[]"  value="Processor"/>Processor<br />
  <input type="submit" name="save" id="save" value="save"/>
</form>

now in your php file:

foreach($_POST['attrname'] as $attribute_values) {
  mysqli_query("INSERT INTO `ia_attributes`(`attribute_name`) VALUES ('".$attribute_values."')");
}

As your table structure is attribute_id seems to be primary key and auto incremented , so you don't need to insert it by inert query.

share|improve this answer
add comment
<input type="checkbox" name=""  value="Disk space"/>Disk space<br />

you need to change the above code to something like:

<input type="checkbox" name="attribute_name[]"  value="Disk space"/>Disk space<br />

note the [] in name

now, your $_POST['attribute_name'] is an array (if not empty), and will look like:

$_POST['attribute_name'][0] = 'Disk'
$_POST['attribute_name'][1] = 'Color'

Looping the non-empty array will allow you to insert each value into separate row in your table (note: this will run 1 query per loop item):

foreach ($_POST['attribute_name'] as $name)
{
mysql_query("INSERT INTO `ia_attributes` (`attribute_id`, `attribute_name`) VALUES ('', '".mysql_real_escape_string($name)."') ");
}

Best approach would be to execute only 1 query.
Something like this:

    $query = '';
    foreach ($_POST['attribute_name'] as $name)
    {
    $query .= " ('', '".mysql_real_escape_string($name)."'), ";
    }
    if (!empty($query))
    {
    $query = substr($query, 0, -2);
    mysql_query("INSERT INTO `ia_attributes` (`attribute_id`, `attribute_name`) VALUES ".$query." ");
     }

Even better: stop using mysql_ and switch to mysqli_ (note the i at the end) OR pdo }

share|improve this answer
add comment
<?php
foreach($attribute_values as $value)
{
    $sql = mysql_query("INSERT INTO `ia_attributes`(`attribute_id`, `attribute_name`) VALUES ('', '".$value."')"");
}
?>

Its help?

share|improve this answer
add comment

One query

$attrs = array_filter($_POST['test']);

if (!empty($attrs)){
   foreach($attrs as $attr){
      $out[] = '("","'.mysql_real_escape_string($attr).'")';
   }     

   $sql = 'INSERT INTO `ia_attributes` VALUES '.implode(',',$out);
   mysql_query($sql);
}
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.