Jump to content

loop insert not inserting everything into database


dolcezza

Recommended Posts

I am doing an application where people can enter a schedule in a form, but if the schedule is already there it is autofilled. It loops so that if there is a one day event, only 3 schedule rows show up, 2 days 6 sho up, 3 days 9 show etc....

everything is working (it makes a pdf, displays properly etc.. the only thing is only the first day is being entered into the database. ANy suggestions?

(Only posting the relevent code... all the variables are ok because it is working in the display and the pdf.

 

This is the form:

<strong>Schedule:</strong><br />
Please schedule no more than three assemblies of 45 minutes each, leaving 5-10 minutes in between.<br />
<? php
$schedulequery = ("SELECT * FROM `schedules` WHERE eventid = '$eventid'");
$scheduleresult=mysql_query($schedulequery);

for($x=0;$x<=$date_diff;$x++) {
        echo $curdate->format("m-d-Y") . "<br />";
for ($loop=1;$loop<=3;$loop++) {
        if ($scheduleinfo=mysql_fetch_array($scheduleresult)) {
        	$schedule_date = $scheduleinfo[2];
            $time=$scheduleinfo[3];
        } else {
            $time="";
        }

?>

<input type="hidden" name="schedule-<? echo $loop."-".$x; ?>" value="<? echo $scheduleinfo['scheduleid']; ?>">
Time:<input type="text" size="8" name="time-<? echo $loop."-".$x; ?>" id="time" value="<? echo $scheduleinfo['time']; ?>">
Grades:<input type="text" size="8" name="grades-<? echo $loop."-".$x; ?>" id="grades" value="<? echo $scheduleinfo['grades']; ?>">
Room:<input type="text" size="8" name="room-<? echo $loop."-".$x; ?>" id="room" value="<? echo $scheduleinfo['room']; ?>">
Notes:<input type="text" name="notes-<? echo $loop."-".$x; ?>" id="notes" value="<? echo $scheduleinfo['notes']; ?>"><br />
<?
        }
        $curdate->modify("+1 day");
        echo "<hr>";
}
?>

 

This is where it inserts into the database:

<?php
$schedulequery = ("SELECT * FROM `schedules` WHERE eventid = '$eventid'");
$scheduleresult=mysql_query($schedulequery);
//echo "Calculating date";
$curdate = new DateTime($info['date']);
//echo "Got:'".$curdate->format("Y-m-d")."'";
//echo "Should have had:'".$info['date']."'"; 
for($i=0;$i<=$date_diff;$i++) {
//echo "Working on:'".$curdate->format("Y-m-d")."'<br>";
for ($x = 1; $x <= 3; $x++) {
	$schedule_id =$_POST['schedule-'.$x.'-'. $i];
	$time = $_POST['time-'.$x.'-'. $i];
    		$grades = $_POST['grades-'.$x.'-'. $i];
    		$room = $_POST['room-'.$x.'-'. $i];
    		$notes = $_POST['notes-'.$x.'-'. $i];
	if (strlen($time)>0) {
		$sched_date=$curdate->format("Y-m-d");
		if (strlen($schedule_id)>0){
			$scheduleinsert=("UPDATE schedules SET date= '$sched_date',time= '$time', grades = '$grades', room = '$room', notes = '$notes' WHERE scheduleid = '$schedule_id'");
		} else {
    				$scheduleinsert = "INSERT INTO schedules (eventid, date, time, grades, room, notes) VALUES ('$eventid', '$sched_date' '$time', '$grades', '$room', '$notes')";
		}
//			echo "DB statement:'".$scheduleinsert."'";
		$scheduleinsert_result = mysql_query($scheduleinsert);
//			echo "; Result was ".$scheduleinsert_result."<br>";
    		}
}
$curdate->modify("+1 day");
}
?>

 

This is where most of the variables are declared just in case:

<?php
$eventid = mysql_real_escape_string($_POST['eventid']);
$address = mysql_real_escape_string($_POST['address']);
$city = mysql_real_escape_string($_POST['city']);
$state = mysql_real_escape_string($_POST['state']);
$zip = mysql_real_escape_string($_POST['zip']);
$directions = $_POST['directions'];
$eventnotes = mysql_real_escape_string($_POST['eventnotes']);
$honorarium = mysql_real_escape_string($_POST['honorarium']);
$travelexp = mysql_real_escape_string($_POST['travelexp']);
$sig = mysql_real_escape_string($_POST['sig']);
$travelincl = implode($_POST['travelinc'],",");

$query = ("SELECT * FROM `events` WHERE eventid = '$eventid' LIMIT 1");
$result=mysql_query($query);
$info = mysql_fetch_array($result);
$authorid = $info['authorid'];
$venueid = $info['venueid'];
$date_array=explode("-", $info['date']);
$friendly_date=$date_array[1]."/".$date_array[2]."/".$date_array[0];
$endfriendly_date=$enddate_array[1]."/".$enddate_array[2]."/".$enddate_array[0];
$authorids=explode(",",$info['authorid']);

$date_diff = subtract_dates($info['date'] , $info['enddate']);
//function to find difference between two dates
function subtract_dates($begin_date, $end_date)
{
return round(((strtotime($end_date) - strtotime($begin_date)) / 86400));
}

$venuequery = ("SELECT * FROM `venues` WHERE venueid = '$venueid' LIMIT 1");
$venueresult=mysql_query($venuequery);
$venueinfo = mysql_fetch_array($venueresult);
$venue = $venueinfo['venue'];
$name = $venueinfo['name'];
$city = $venueinfo['city'];
$state = $venueinfo['state'];
$email = $venueinfo['email'];
$phone = $venueinfo['phone'];

?>

Link to comment
Share on other sites

I am not super experienced, I had a tutor helping me with this.... unfortunately he is not available for a few days and I need to get this issue fixed right away.

We didn't really go into what methods are better or why.

If you have a spare minute I wouldn't mind an explaination as to why you'd do it this way.

 

Thanks

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.