Code Snippet - MySQL insert

Coding Critique is the place to post source code for peer review by other members of DevNetwork. Any kind of code can be posted. Code posted does not have to be limited to PHP. All members are invited to contribute constructive criticism with the goal of improving the code. Posted code should include some background information about it and what areas you specifically would like help with.

Popular code excerpts may be moved to "Code Snippets" by the moderators.

Moderator: General Moderators

Post Reply
ibroker
Forum Newbie
Posts: 1
Joined: Mon Jun 21, 2010 9:57 am

Code Snippet - MySQL insert

Post by ibroker »

I have never seen this behavior before. This code is somehow adding an extra row or two of data into the database. The loop goes through the appropriate number of times because it prints out the correct user inputted data, but when I look up the data in the database, an extra row of data somehow gets created.


the user inputs any number of rows with 3 inputs in each row the inputs are:
a date in the form of YYYY-DD-MM,
a start time (drop down)
an end time (drop down)

The code then inserts these rows into the database:

$client = mysql_query($query, $mysql) or die(mysql_error());
$reservationid = mysql_insert_id();

$datepattern = "([0-9]{1,2})-([0-9]{1,2})-([0-9]{4})";
$resdates = $_POST['dates']; //array of dates
$resstarttimes = $_POST['starttime']; // array of start times
$resendtimes = $_POST['endtime']; // array of end times


$statusquerybegin = "INSERT into dates_times SET reservation_id=$reservationid, ";

$datetimerequest="\nRequested Dates and Hours: \n\n";
$webdatetimerequest="\nRequested Dates and Hours:<br><br>";

while(list($dkey, $dval) = each($resdates)) { //execute if it's time check number of days left
list($tkey, $t1val) = each($resstarttimes);
list($tkey, $t2val) = each($resendtimes);
if (strlen(trim($dval)) >1) {
ereg( $datepattern, $dval, $regs);
$dataval = $regs[3]."-".$regs[1]."-".$regs[2];

$statusqueryend = "date='$dataval',";

$statusqueryend .= "start_time='$t1val',";
$statusqueryend .= "end_time='$t2val'";
$statusquery = $statusquerybegin.$statusqueryend;

$datetimerequest .= "Date: $dval - Start Time: $t1val - End Time: $t2val\n";
$webdatetimerequest .= "Date: $dval - Start Time: $t1val - End Time: $t2val<br>";

//echo $statusquery."<br>";
$statusresult = mysql_query($statusquery) or die("Problems with query: $statusquery <BR>".mysql_error());
usleep(500000);
}
}



Somehow, when I select rows in the dates_times table based on the reservation_id, I get extra rows of data that weren't entered into the form.

I've never seen this... There must be something I am doing that I am not seeing....

Thanks.
Post Reply