Hello Folks,
My challenge is to write 15 rows of employee hours to the database. Each row has the same variable names, the only thing that changes is the index number. I'm trying to use a loop where I concatenate the loop index number with the variable name. My code has a major bug. It seems to be writing an infinite number of records to the database. Could someone give me some feedback please:
//give variable table name
$tableName = "tbl_labour";
//create sql statement
$add = "Insert into $tableName(job_number, job_date, employee_name,
regular_time, travel_time, over_time )values
('%s','%s', '%s','%s','%s','%s' )";
//=================================================================
//initialize counter
$x = (int)1;
//start loop
while ($x <= 15)
{
//concatenate variable names
$employee_name = ${"employee_name".$x};
$regular_time = ${"regular_time".$x};
$travel_time = ${"travel_time".$x};
$over_time = ${"over_time".$x};
// write employee info to database
//check that value is set
if (!empty($employee_name))
{
//check that either regular or overtime hours field is not blank
if ((!$regular_time) && (!$over_time))
{
DisplayErrMsg("Error: Regular hours and over time hours cannot both be blank for employee $x");
exit();
}
else
{
//execute query
if(!mysql_query(sprintf($add,$job_number, $job_date, $employee_name, $regular_time, $travel_time, $over_time ), $link))
{
DisplayErrMsg(sprintf("Error in executing %s stmt", $stmt));
DisplayErrMsg(sprintf("error:%d %s", mysql_errno($link), mysql_error($link)));
exit();
}
}
}
$x = $x + 1;
}