Jump to content

multiple db entries 1 form


jamesd116

Recommended Posts

Hello,

  Hopefully I can ger som help here I have been googling this for months and seem to cant find anything that will help me out.

I am trying to create a form (which is the easy part) but I want this form to send multiple entries at once from what I gather so far I need to use for each and/or loop which i am not familiar with it with my lack of knowledge........

what I have so far is

Table= locations

fields are

id

state

county

city

 

the form is set up as follows

state

county

city

city

city

city

city

city

city

city

city

city

city

city

The reason for so many cities is so i can update all cities in one county at the same time saving time I am not sure if its % doable in the manner i would like it to be done but i hope so

 

ok i enter the state and county

then i enter the cities i want

i submit the form i want it to create the state/county and the first city with the next id # then enter the state/county and next city and id#.

 

 

please some help me  if u need to see my file let me know and ill post it im just not sure which one you need i believe it will be the save.php file but i also have the create.php file which is the form itself

Thanks anyone for the help i can get

Link to comment
Share on other sites

Un-Tested:

<?php
error_reporting(-1);
ini_set('display_errors',1);

include('mysql_connection_info.php');
if(isset($_POST['submit'])) {
$state = mysql_real_escape_string(trim($_POST['state']));
$county = mysql_real_escape_string(trim($_POST['county']));
foreach($_POST['city'] as $value) {
   if(empty($value)) { continue; }
   $city = mysql_real_escape_string($value);
   $parts[] = "('$state','$county','$city')";
}
if(is_array($parts)) {
$sql = "INSERT INTO locations (state,county,city) VALUES " . implode(',',$parts);
if(mysql_query($sql)) {
    echo 'Successful<br />';
} else {
   echo $sql . ' has encountered an error: <br /> ' . mysql_error();
}
} else {
echo 'You must submit at least one city.';
}
?>
<form action="" method="post">
<input type="text" name="state" id="state" placeholder="State" /><br />
<input type="text" name="county" id="county" placeholder="County" /><br />
<?php
for($i = 0; $i < 5; $i++) {
  echo '<input type="text" name="city[]" id="city' . $i . '" placeholder="City" /><br />';
}
?>
<input type="submit" name="submit" value="Submit" />
</form>

 

Table structure

CREATE TABLE IF NOT EXISTS `locations` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `state` varchar(100) NOT NULL,
  `county` varchar(100) NOT NULL,
  `city` varchar(100) NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=MyISAM ;

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.