Using PHP and MySQL, how can I insert multiple records based on check boxes?

I have a form in php that has users insert information for an upcoming event, say date, time, and city. Currently, the city option is a radio button with 5 possible selections. The code works fine and inserts the record with the selected city. Now, however, I need to change the radio button to a check box (easy enough in the form) but then I don't want to enter the multiple selections into the one 'city' field. This would make it difficult to select records that match each city. What I would like to do is create a record for each city in the database. So instead of the entry looking like this:

date           time            city
July 22       noon           Tucson, Phoenix

I would get this:

date          time            city
July 22       noon          Tucson
July 22       noon          Phoenix

I'm thinking like a for each loop or something, but can't figure it out.
LVL 1
wiggy353Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

hieloCommented:
>> I'm thinking like a for each loop or something, but can't figure it out.
Yes.  You need to add brackets to the name of the checkbox --ex:
<input name="city[]" value="1"/>Tucson
<input name="city[]" value="2"/>Phoenix

Then on the server $_POST['city'] IS an array of the selected cities (assuming you selected at least one).
if( array_key_exists('city',$_POST) )
{
   //connect to the db first.  
   $link = mysqli_connect(...);

   $sql = 'INSERT INTO TableName(`city`) VALUES ';
   foreach( $_POST['city'] as $k=>$value)
   {
       $sql .= sprintf('(%s),', mysqli_real_escape_string($link, $value) );
   }
   $sql = trim($sql, ',');
   echo $sql;//show show you the INSERT query
}

Open in new window

0
wiggy353Author Commented:
That makes sense. So if I want to insert all 3 fields for each record, I would do something like this?:

if( array_key_exists('city',$_POST) )
{
   //connect to the db first.  
   $link = mysqli_connect(...);

   $sql = 'INSERT INTO TableName('date','time','city') VALUES ';
   $date = $_POST['date'];
   $time = $_POST['time'];
   foreach( $_POST['city'] as $k=>$value)
   {
       $sql .= sprintf('(%s),', mysqli_real_escape_string($link, $date, $time, $value) );
   }
   $sql = trim($sql, ',');
   echo $sql;//show show you the INSERT query
}

Open in new window

0
hieloCommented:
You need to use mysqli_real_escape_string() once per value.  So the date and time should be done outside the loop.  As for the sprintf, you need one %s per value/column.  See below:
if( array_key_exists('city',$_POST) )
{
   //connect to the db first.  
   $link = mysqli_connect(...);

   $sql = 'INSERT INTO TableName('date','time','city') VALUES ';
   $date = mysqli_real_escape_string($link,$_POST['date']);
   $time = mysqli_real_escape_string($link,$_POST['time']);
   foreach( $_POST['city'] as $k=>$value)
   {
       //on every iteration, only $value is changing.
       // as for sprintf(),  the first argument is always a formatting string.  The %s are "place holders".
       // for every %s you include, you need a parameter. Notice I am providing three values AFTER  the formatting string:
       //  $data, $time, and $value (it's just hat I am escaping the $value).
       $sql .= sprintf( "(%s,%s,%s),", $date, $time, mysqli_real_escape_string($link, $value) );
   }

   //now remove the trailing comma
   $sql = trim($sql, ',');

   echo $sql;//show show you the INSERT query
}

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
wiggy353Author Commented:
Perfect, thanks!
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.