Jagdish
Jagdish

Reputation: 269

php insert data into database with multiple ids at once?

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.

Upvotes: 2

Views: 4196

Answers (5)

Arthur Yakovlev
Arthur Yakovlev

Reputation: 9309

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

Its help?

Upvotes: 0

andrew
andrew

Reputation: 2098

<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 }

Upvotes: 0

user2936213
user2936213

Reputation: 1011

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.

Upvotes: 0

Abhik Chakraborty
Abhik Chakraborty

Reputation: 44864

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

            }
        }
    }   

Upvotes: 0

Shahid Ahmed
Shahid Ahmed

Reputation: 494

<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."')";
}

Upvotes: 1

Related Questions