I want to add multi data in the database table by clicking on one button, how can I do that?
<?php
if( isset($_POST['add']) ){
$ins = "INSERT INTO informations(`cate_id` ,`location_id` , `name` ,`specialization` , `address` , `telephone` , `time`) VALUES(
'".$_POST['category']."',
'".$_POST['location']."',
'".$_POST['name']."',
'".$_POST['specialization']."',
'".$_POST['address']."',
'".$_POST['telephone']."',
'".$_POST['time']."')";
$do_ins = mysql_query($ins);
echo 'Insert done';
}
?>
This is a basic version of the mysql multi-row insert syntax:
INSERT INTO mytable (field1, field2, field3)
VALUES
(value1, value2, value3),
(value4, value5, value6)
With the above query, 2 rows are inserted: (value1, value2, value3)
and (value4, value5, value6)
-- EDIT --
For your form, you can use array-like syntax in your form input names to pass the variables along to PHP in an array. If you're using locations, you can do something like this:
<input type="text" name="locations[0][name]" />
<input type="text" name="locations[0][address]" />
<input type="text" name="locations[1][name]" />
<input type="text" name="locations[1][address]" />
The above inputs will fill out the following $_POST
or $_GET
entries:
$_POST['locations'][0]['name']
$_POST['locations'][0]['address']
$_POST['locations'][1]['name']
$_POST['locations'][1]['address']
As you can see, this allows you to use a loop to go through each location.
$locations = array();
foreach($_POST['locations'] as $location) {
$fields = array_map('mysql_real_escape_string', $location);
$locations[] = "(\"{$fields['name']}\", \"{$fields['address']}\")";
}
$insert = "INSERT INTO locations (name, address) VALUES " . implode(',', $locations);
mysql_query($insert);
You'll most likely want to add some validation code to make sure they're actually filling in all the fields as well.