Any help appreciated. I am in the process of making a timesheet. The issue I have is the foreach will work correctly when I have only one table row entry as soon as I have two table row entries in the form, the end time from the first entry disappears from the database ... heaven knows why.Hope it makes sense. Any help appreciated. Thank you
### the php ############
function updateEntries() {
### GRAB FORM SUBMITS ###
$startTime = $_POST['startTime'];
$endTime = $_POST['endTime'];
$comments = $_POST['comments'];
$timesheet_id = $_POST['timesheet_id'];
### USE THE STARTTIME TO WORK THROUGH EACH ENTRY
foreach($startTime as $key => $value) {
$value = "$value";
$endTime = "$endTime[$key]";
echo "$endTime <br/ >$key<br />";
###################################################################################################################################################
### CHECK IF THE STARTTIME IS EQUAL TO 5 CHARACTERS OR 4 CHARACTERS
if (((int)(strlen($value) == 5) && (@ereg('[0-9]{2}[:]{1}[0-9]{2}',$value))) || ((int)(strlen($value) == 4) && (@ereg('[0-9]{4}',$value)) )) {
$starthr = substr($value,0,2);
$startmin = substr($value,-2);
} else {
echo "echo Start time error 5 <br/ >";
}
if ((strlen($endTime) == 5) && (@ereg('[0-9]{2}[:]{1}[0-9]{2}',$endTime)) || ((strlen($endTime) == 4) && (@ereg('[0-9]{4}',$endTime)))) {
$endhr = substr($endTime,0,2);
$endmin = substr($endTime,-2);
} else {
echo "end time error 5<br/ >";
}
$startTime = @date('Y-m-d'.' '.$starthr.':'.$startmin.':00');
$endTime = @date('Y-m-d'.' '.$endhr.':'.$endmin.':00');
$query = "update timesheet set startTime = '$startTime', endTime = '$endTime', comments = '$comments[$key]' where timesheet_id = {$timesheet_id[$key]}";
echo $query."<br />";
$result = mysql_query($query);
}
}
#### the html ###########
<form name="update" action="/timeSheet/" method="post">
<input type="image" src="../images/save.png" width="76" height="22" border="0" alt="Go" /></a><input type="hidden" name="save" value="true" />
<table class="timeSheet" width="100%" border="0" cellpadding="10" cellspacing="10">
<tr>
<th>Client</th>
<th>SO No</th>
<th>Start</th>
<th>End</th>
<th>Activity</th>
<th>Comments</th>
<th>Action</th>
</tr>
<tr>
<td bgcolor="#ffffff">test2</td>
<td bgcolor="#ffffff">15</td>
<td bgcolor="#ffffff"><input type="text" name="startTime[0]" size="2" maxlength="5" value="15:45" /></td>
<td bgcolor="#ffffff"><input type="text" name="endTime[0]" size="2" maxlength="5" value="" /></td>
<td bgcolor="#ffffff">Communicating with A N other</td>
<td bgcolor="#ffffff"><textarea name="comments[0]" cols="45" rows="5"></textarea></td>
<td><a href="delete.php?entry=175" ><img src="../images/bin.png" width="46" height="46" alt="delete" /></a><input type="hidden" name="timesheet_id[0]" value="175"</td>
</tr><tr>
<td bgcolor="#eeeeee">test3</td>
<td bgcolor="#eeeeee">435</td>
<td bgcolor="#eeeeee"><input type="text" name="startTime[1]" size="2" maxlength="5" value="15:15" /></td>
<td bgcolor="#eeeeee"><input type="text" name="endTime[1]" size="2" maxlength="5" value="" /></td>
<td bgcolor="#eeeeee">Communicating with A N other</td>
<td bgcolor="#eeeeee"><textarea name="comments[1]" cols="45" rows="5"></textarea></td>
<td><a href="delete.php?entry=172" ><img src="../images/bin.png" width="46" height="46" alt="delete" /></a><input type="hidden" name="timesheet_id[1]" value="172"</td>
</tr>
</table>
</div>
</form>
### the php ############
function updateEntries() {
### GRAB FORM SUBMITS ###
$startTime = $_POST['startTime'];
$endTime = $_POST['endTime'];
$comments = $_POST['comments'];
$timesheet_id = $_POST['timesheet_id'];
### USE THE STARTTIME TO WORK THROUGH EACH ENTRY
foreach($startTime as $key => $value) {
$value = "$value";
$endTime = "$endTime[$key]";
echo "$endTime <br/ >$key<br />";
###################################################################################################################################################
### CHECK IF THE STARTTIME IS EQUAL TO 5 CHARACTERS OR 4 CHARACTERS
if (((int)(strlen($value) == 5) && (@ereg('[0-9]{2}[:]{1}[0-9]{2}',$value))) || ((int)(strlen($value) == 4) && (@ereg('[0-9]{4}',$value)) )) {
$starthr = substr($value,0,2);
$startmin = substr($value,-2);
} else {
echo "echo Start time error 5 <br/ >";
}
if ((strlen($endTime) == 5) && (@ereg('[0-9]{2}[:]{1}[0-9]{2}',$endTime)) || ((strlen($endTime) == 4) && (@ereg('[0-9]{4}',$endTime)))) {
$endhr = substr($endTime,0,2);
$endmin = substr($endTime,-2);
} else {
echo "end time error 5<br/ >";
}
$startTime = @date('Y-m-d'.' '.$starthr.':'.$startmin.':00');
$endTime = @date('Y-m-d'.' '.$endhr.':'.$endmin.':00');
$query = "update timesheet set startTime = '$startTime', endTime = '$endTime', comments = '$comments[$key]' where timesheet_id = {$timesheet_id[$key]}";
echo $query."<br />";
$result = mysql_query($query);
}
}
#### the html ###########
<form name="update" action="/timeSheet/" method="post">
<input type="image" src="../images/save.png" width="76" height="22" border="0" alt="Go" /></a><input type="hidden" name="save" value="true" />
<table class="timeSheet" width="100%" border="0" cellpadding="10" cellspacing="10">
<tr>
<th>Client</th>
<th>SO No</th>
<th>Start</th>
<th>End</th>
<th>Activity</th>
<th>Comments</th>
<th>Action</th>
</tr>
<tr>
<td bgcolor="#ffffff">test2</td>
<td bgcolor="#ffffff">15</td>
<td bgcolor="#ffffff"><input type="text" name="startTime[0]" size="2" maxlength="5" value="15:45" /></td>
<td bgcolor="#ffffff"><input type="text" name="endTime[0]" size="2" maxlength="5" value="" /></td>
<td bgcolor="#ffffff">Communicating with A N other</td>
<td bgcolor="#ffffff"><textarea name="comments[0]" cols="45" rows="5"></textarea></td>
<td><a href="delete.php?entry=175" ><img src="../images/bin.png" width="46" height="46" alt="delete" /></a><input type="hidden" name="timesheet_id[0]" value="175"</td>
</tr><tr>
<td bgcolor="#eeeeee">test3</td>
<td bgcolor="#eeeeee">435</td>
<td bgcolor="#eeeeee"><input type="text" name="startTime[1]" size="2" maxlength="5" value="15:15" /></td>
<td bgcolor="#eeeeee"><input type="text" name="endTime[1]" size="2" maxlength="5" value="" /></td>
<td bgcolor="#eeeeee">Communicating with A N other</td>
<td bgcolor="#eeeeee"><textarea name="comments[1]" cols="45" rows="5"></textarea></td>
<td><a href="delete.php?entry=172" ><img src="../images/bin.png" width="46" height="46" alt="delete" /></a><input type="hidden" name="timesheet_id[1]" value="172"</td>
</tr>
</table>
</div>
</form>