2

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.

Jagdish
  • 259
  • 1
  • 8
  • 19
  • See the answer in this question - http://stackoverflow.com/questions/6889065/inserting-multiple-rows-in-mysql – a.yastreb Jan 22 '14 at 10:23
  • how you re getting `$attribute_values` in your insert query?Also give `name= attrname[]` to all your checkboxes – user2936213 Jan 22 '14 at 10:23

5 Answers5

1
<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."')";
}
Shahid Ahmed
  • 494
  • 2
  • 4
  • 10
0
<?php
foreach($attribute_values as $value)
{
    $sql = mysql_query("INSERT INTO `ia_attributes`(`attribute_id`, `attribute_name`) VALUES ('', '".$value."')"");
}
?>

Its help?

Arthur Yakovlev
  • 8,933
  • 8
  • 32
  • 48
0

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

            }
        }
    }   
Abhik Chakraborty
  • 44,654
  • 6
  • 52
  • 63
  • 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 '14 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 '14 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 '14 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 '14 at 10:47
0

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.

user2936213
  • 1,021
  • 1
  • 8
  • 19
0
<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 }

andrew
  • 2,058
  • 2
  • 25
  • 33