Jump to content

INSERT inside for loop


dolcezza

Recommended Posts

I am not able to test my code from where i am now, and I am not sure why my editor is underlining this code.

Any chance someone may be willing to help me find the issue?

It's underlining the if ($time) {

but obviously that doesn't always mean it's not before or after.

for ($loop=1;$loop<=3;$loop++) {
$time = $_POST['time-'.$loop];
if ($time) {
    $scheduleinsert = "INSERT INTO schedules (eventid, time, grades, room, notes) VALUES ('$eventid', '$time', '$grades', '$room', '$notes')";
    $schedule_result=mysql_query($scheduleinsert);

Thanks, any help would be appreciated.

 

Link to comment
https://forums.phpfreaks.com/topic/84704-insert-inside-for-loop/
Share on other sites

yes...

for ($loop=1;$loop<=3;$loop++) {
$time = $_POST['time-'.$loop];
if ($time) {
    $scheduleinsert = "INSERT INTO schedules (eventid, time, grades, room, notes) VALUES ('$eventid', '$time', '$grades', '$room', '$notes')";
    $schedule_result=mysql_query($scheduleinsert);

}

}

<?php

for ($i = 1; $i <= 3; $i++) {
    $time = $_POST['time-' . $i];

    if ($time) {
        $scheduleinsert = "INSERT INTO `schedules` (`eventid`, `time`, `grades`, `room`, `notes`) VALUES ('" .
            $eventid . "', '" . $time . "', '" . $grades . "', '" . $room . "', '" . $notes .
            "')";
        $schedule_result = mysql_query($scheduleinsert);
    }
}

?>

 

Just make sure you have the other variables defined.

I changed it to the following, realizing I had to add the loop part to those variables too, but I'm still getting the same thing.

It says parse error: expecting T_string or T_variable or T_NUM_STRING on line 21

 


for ($i = 1; $i <= 3; $i++) {
    $time = $_POST['time-' . $i];
    $grades = $_POST['grades-' . $i];
    $room = $_POST['room-' . $i];
    $notes = $_POST['notes-' . $i];
if ($time) {
        $scheduleinsert = "INSERT INTO `schedules` (`eventid`, `time`, `grades`, `room`, `notes`) VALUES ('" .
            $eventid . "', '" . $time . "', '" . $grades . "', '" . $room . "', '" . $notes .
            "')";
        $schedule_result = mysql_query($scheduleinsert);
    }
}

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.