0

Trying to set up a scheduler with a checkbox, when I check multiple time slots I would like the checked times to each be inserted into my table.

I have timeslots set up in my database as an enum, so this just displays all the times in checkbox form.

echo "<table>";
echo "<tr>";				
  echo("<td><input type=checkbox name=time_slot[] value='$option'>$option</td>");
echo "</tr>";
echo "</table>";

This code is supposed to insert each checked item into the database with all the same information bar the timeslot.

if(isset($_POST['submit']))
{
  $ts = $_POST['time_slot'];
  $db = new PDO ("mysql:host=localhost; dbname=zzzzz", "zzzzz", "12345");
  for ($i=0; $i<sizeof($ts);$i++)
  {
    $query = "INSERT INTO timeslot (date, duration, firstName, lastName) VALUES ('".$_POST['year']."-".$_POST['month']."-".$_POST['day']."', '$ts', '".$_SESSION['firstName']."', '".$_SESSION['lastName']."')";
	$state = $db->prepare($query);
	$state->execute();
	print_r($state);
  }
	print_r($ts);
Each loop for the print_r($state) outputs INSERT INTO timeslot (date, duration, firstName, lastName) VALUES ('2020-04-20', 'Array','John', 'Doe').

The print_r($ts) outputs all the timeslots that i've checked Array ( [0] => 4:20-4:40 PM [1] => 4:40-5:00 PM [2] => 5:00-5:20 PM )

How can I fix this problem? Thanks.

1
  • 1
    Use it like $ts[$i] Commented Dec 3, 2015 at 5:14

2 Answers 2

2

If you're looping through $ts, you might want to use the $i to grab the value at that index on each loop. '" . $ts[$i] . "'

$query = "INSERT INTO timeslot (date, duration, firstName, lastName) VALUES ('".$_POST['year']."-".$_POST['month']."-".$_POST['day']."', '" . $ts[$i] . "', '".$_SESSION['firstName']."', '".$_SESSION['lastName']."')";
Sign up to request clarification or add additional context in comments.

1 Comment

Can't believe i overlooked something that simple, thank you so much!
0

Use time-slot as below to get comma separated value of it:

$ts = implode(",",$_POST['time_slot']);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.