Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

row update error

Status
Not open for further replies.

kzn

MIS
Jan 28, 2005
209
GB
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>
 
Any help appreciated. I am in the process of making a timesheet.

curious timing! I started work on a solution to be open-sourced only this morning!

an initial comment: ALWAYS post code within [ignore]
Code:
[/ignore] tags so that it is readable. it makes like ineffably easier.

i've not looked very deeply into your code but i'd say a few things off the top of my head

1. don't use ereg. there are better ways to count the characters in a string. and if you really want to validate a time using regular expressions, then use preg_* functions.
2. there are much easier ways of doing what you want in those complex && || segments. but if you're happy with them that's fine.
3. you are using $startTime as a pointer to the array at the top of the file. So it will have $startTime[0] etc as each of the start times in your sheet. you then recast $startTime at the end of the loop to be the concatenation of $starthr etc. I'm sure you do not mean to do this as the next iteration of the loop there suddenly won't be an array to iterate.

perhaps this code might work better (caveat: not tested and the ereg and complex and/ors i have not fixed for you)

Code:
[b][COLOR=#0000FF]function[/color][/b] [b][COLOR=#000000]updateEntries[/color][/b][COLOR=#990000]()[/color] [COLOR=#FF0000]{[/color]

[tab][i][COLOR=#9A1900]### GRAB FORM SUBMITS ###   [/color][/i]
[tab][COLOR=#009900]$startTime[/color] [COLOR=#990000]=[/color] [COLOR=#009900]$_POST[/color][COLOR=#990000][[/color][COLOR=#FF0000]'startTime'[/color][COLOR=#990000]];[/color]
[tab][COLOR=#009900]$endTime[/color] [COLOR=#990000]=[/color] [COLOR=#009900]$_POST[/color][COLOR=#990000][[/color][COLOR=#FF0000]'endTime'[/color][COLOR=#990000]];[/color]
[tab][COLOR=#009900]$comments[/color] [COLOR=#990000]=[/color] [COLOR=#009900]$_POST[/color][COLOR=#990000][[/color][COLOR=#FF0000]'comments'[/color][COLOR=#990000]];[/color]
[tab][COLOR=#009900]$timesheet_id[/color] [COLOR=#990000]=[/color] [COLOR=#009900]$_POST[/color][COLOR=#990000][[/color][COLOR=#FF0000]'timesheet_id'[/color][COLOR=#990000]];[/color]

[tab][i][COLOR=#9A1900]### USE THE STARTTIME TO WORK THROUGH EACH ENTRY[/color][/i]
[tab][b][COLOR=#0000FF]foreach[/color][/b][COLOR=#990000]([/color][COLOR=#009900]$startTime[/color] [b][COLOR=#0000FF]as[/color][/b] [COLOR=#009900]$key[/color] [COLOR=#990000]=>[/color] [COLOR=#009900]$value[/color][COLOR=#990000]):[/color]
[tab][tab][COLOR=#009900]$value[/color] [COLOR=#990000]=[/color] [COLOR=#FF0000]"$value"[/color][COLOR=#990000];[/color]
[tab][tab][COLOR=#009900]$endTime[/color] [COLOR=#990000]=[/color] [COLOR=#FF0000]"$endTime[$key]"[/color][COLOR=#990000];[/color]
[tab][tab][b][COLOR=#0000FF]echo[/color][/b] [COLOR=#FF0000]"$endTime <br/ >$key<br />"[/color][COLOR=#990000];[/color]
[tab][tab][i][COLOR=#9A1900]################################################################################################################################################### [/color][/i]

[tab][tab][i][COLOR=#9A1900]### CHECK IF THE STARTTIME IS EQUAL TO 5 CHARACTERS OR 4 CHARACTERS [/color][/i]
[tab][tab][b][COLOR=#0000FF]if[/color][/b] [COLOR=#990000]([/color][tab][COLOR=#990000]([/color]
[tab][tab][tab][tab][tab][COLOR=#990000]([/color]int[COLOR=#990000])([/color][b][COLOR=#000000]strlen[/color][/b][COLOR=#990000]([/color][COLOR=#009900]$value[/color][COLOR=#990000])[/color] [COLOR=#990000]==[/color] [COLOR=#993399]5[/color][COLOR=#990000])[/color] [COLOR=#990000]&&[/color] 
[tab][tab][tab][tab][tab][COLOR=#990000]([/color][COLOR=#009900]@ereg[/color][COLOR=#990000]([/color][COLOR=#FF0000]'[0-9]{2}[:]{1}[0-9]{2}'[/color][COLOR=#990000],[/color][COLOR=#009900]$value[/color][COLOR=#990000]))[/color]
[tab][tab][tab][tab][COLOR=#990000])[/color] 
[tab][tab][tab][tab][COLOR=#990000]||[/color] 
[tab][tab][tab][tab][COLOR=#990000]([/color]
[tab][tab][tab][tab][tab][COLOR=#990000]([/color]int[COLOR=#990000])([/color][b][COLOR=#000000]strlen[/color][/b][COLOR=#990000]([/color][COLOR=#009900]$value[/color][COLOR=#990000])[/color] [COLOR=#990000]==[/color] [COLOR=#993399]4[/color][COLOR=#990000])[/color] [COLOR=#990000]&&[/color] 
[tab][tab][tab][tab][tab][COLOR=#990000]([/color][COLOR=#009900]@ereg[/color][COLOR=#990000]([/color][COLOR=#FF0000]'[0-9]{4}'[/color][COLOR=#990000],[/color][COLOR=#009900]$value[/color][COLOR=#990000]))[/color] 
[tab][tab][tab][tab][COLOR=#990000])[/color]
[tab][tab][tab][COLOR=#990000])[/color] [COLOR=#990000]:[/color]

[tab][tab][COLOR=#009900]$starthr[/color] [COLOR=#990000]=[/color] [b][COLOR=#000000]substr[/color][/b][COLOR=#990000]([/color][COLOR=#009900]$value[/color][COLOR=#990000],[/color][COLOR=#993399]0[/color][COLOR=#990000],[/color][COLOR=#993399]2[/color][COLOR=#990000]);[/color]
[tab][tab][COLOR=#009900]$startmin[/color] [COLOR=#990000]=[/color] [b][COLOR=#000000]substr[/color][/b][COLOR=#990000]([/color][COLOR=#009900]$value[/color][COLOR=#990000],-[/color][COLOR=#993399]2[/color][COLOR=#990000]);[/color]
[tab][tab][b][COLOR=#0000FF]else[/color][/b][COLOR=#990000]:[/color]
[tab][tab][tab][b][COLOR=#0000FF]echo[/color][/b] [COLOR=#FF0000]"echo Start time error 5 <br/ >"[/color][COLOR=#990000];[/color]
[tab][tab][b][COLOR=#0000FF]endif[/color][/b][COLOR=#990000];[/color]


[tab][tab][b][COLOR=#0000FF]if[/color][/b] [COLOR=#990000]([/color][tab][COLOR=#990000]([/color][b][COLOR=#000000]strlen[/color][/b][COLOR=#990000]([/color][COLOR=#009900]$endTime[/color][COLOR=#990000])[/color] [COLOR=#990000]==[/color] [COLOR=#993399]5[/color][COLOR=#990000])[/color] [COLOR=#990000]&&[/color] 
[tab][tab][tab][tab][COLOR=#990000]([/color]
[tab][tab][tab][tab][tab][COLOR=#009900]@ereg[/color][COLOR=#990000]([/color][COLOR=#FF0000]'[0-9]{2}[:]{1}[0-9]{2}'[/color][COLOR=#990000],[/color][COLOR=#009900]$endTime[/color][COLOR=#990000]))[/color] [COLOR=#990000]||[/color] 
[tab][tab][tab][tab][tab][COLOR=#990000]([/color]
[tab][tab][tab][tab][tab][tab][COLOR=#990000]([/color][b][COLOR=#000000]strlen[/color][/b][COLOR=#990000]([/color][COLOR=#009900]$endTime[/color][COLOR=#990000])[/color] [COLOR=#990000]==[/color] [COLOR=#993399]4[/color][COLOR=#990000])[/color] [COLOR=#990000]&&[/color] 
[tab][tab][tab][tab][tab][tab][COLOR=#990000]([/color][COLOR=#009900]@ereg[/color][COLOR=#990000]([/color][COLOR=#FF0000]'[0-9]{4}'[/color][COLOR=#990000],[/color][COLOR=#009900]$endTime[/color][COLOR=#990000])[/color]
[tab][tab][tab][tab][tab][COLOR=#990000])[/color]
[tab][tab][tab][tab][COLOR=#990000])[/color]
[tab][tab][tab][COLOR=#990000]):[/color]
[tab][tab][tab][tab]
[tab][tab][tab][tab][COLOR=#009900]$endhr[/color] [COLOR=#990000]=[/color] [b][COLOR=#000000]substr[/color][/b][COLOR=#990000]([/color][COLOR=#009900]$endTime[/color][COLOR=#990000],[/color][COLOR=#993399]0[/color][COLOR=#990000],[/color][COLOR=#993399]2[/color][COLOR=#990000]);[/color]
[tab][tab][tab][tab][COLOR=#009900]$endmin[/color] [COLOR=#990000]=[/color] [b][COLOR=#000000]substr[/color][/b][COLOR=#990000]([/color][COLOR=#009900]$endTime[/color][COLOR=#990000],-[/color][COLOR=#993399]2[/color][COLOR=#990000]);[/color]
[tab][tab][b][COLOR=#0000FF]endif[/color][/b][COLOR=#990000];[/color]

[tab][tab][b][COLOR=#0000FF]echo[/color][/b] [COLOR=#FF0000]"end time error 5<br/ >"[/color][COLOR=#990000];[/color]
[tab][tab]
[tab][tab][b][COLOR=#000000]date_default_timezone_set[/color][/b][COLOR=#990000]([/color][COLOR=#FF0000]'UTC'[/color][COLOR=#990000]);[/color]

[tab][tab][COLOR=#009900]$params[/color] [COLOR=#990000]=[/color] [b][COLOR=#0000FF]array[/color][/b][COLOR=#990000]([/color] [COLOR=#009900]@date[/color][COLOR=#990000]([/color][COLOR=#FF0000]'Y-m-d'[/color][COLOR=#990000].[/color][COLOR=#FF0000]' '[/color][COLOR=#990000].[/color][COLOR=#009900]$starthr[/color][COLOR=#990000].[/color][COLOR=#FF0000]':'[/color][COLOR=#990000].[/color][COLOR=#009900]$startmin[/color][COLOR=#990000].[/color][COLOR=#FF0000]':00'[/color][COLOR=#990000]),[/color]
[tab][tab][tab][tab][tab][tab] [COLOR=#009900]@date[/color][COLOR=#990000]([/color][COLOR=#FF0000]'Y-m-d'[/color][COLOR=#990000].[/color][COLOR=#FF0000]' '[/color][COLOR=#990000].[/color][COLOR=#009900]$endhr[/color][COLOR=#990000].[/color][COLOR=#FF0000]':'[/color][COLOR=#990000].[/color][COLOR=#009900]$endmin[/color][COLOR=#990000].[/color][COLOR=#FF0000]':00'[/color][COLOR=#990000]),[/color]
[tab][tab][tab][tab][tab][tab][COLOR=#009900]$comments[/color][COLOR=#990000][[/color][COLOR=#009900]$key[/color][COLOR=#990000]],[/color] 
[tab][tab][tab][tab][tab][tab][COLOR=#009900]$timesheet_id[/color][COLOR=#990000][[/color][COLOR=#009900]$key[/color][COLOR=#990000]]);[/color]
[tab][tab]
[tab][tab][COLOR=#009900]$query[/color] [COLOR=#990000]=[/color] [COLOR=#990000]<<<[/color]SQL
UPDATE timesheet 
SET 
[tab]startTime [COLOR=#990000]=[/color] [COLOR=#FF0000]'%s'[/color][COLOR=#990000],[/color] 
[tab]endTime [COLOR=#990000]=[/color] [COLOR=#FF0000]'%s'[/color][COLOR=#990000],[/color] 
[tab]comments [COLOR=#990000]=[/color] [COLOR=#FF0000]'%s'[/color] 
WHERE
[tab]timesheet_id [COLOR=#990000]=[/color] [COLOR=#FF0000]'%s'[/color]
SQL[COLOR=#990000];[/color][tab]
[tab][tab][COLOR=#009900]$query[/color] [COLOR=#990000]=[/color] [b][COLOR=#000000]vsprintf[/color][/b][COLOR=#990000]([/color][COLOR=#009900]$query[/color][COLOR=#990000],[/color][b][COLOR=#000000]array_map[/color][/b] [COLOR=#990000]([/color][COLOR=#FF0000]'mysql_real_escape_string'[/color][COLOR=#990000],[/color] [COLOR=#009900]$params[/color][COLOR=#990000]));[/color]
[tab][tab][b][COLOR=#0000FF]echo[/color][/b] [COLOR=#009900]$query[/color][COLOR=#990000].[/color][COLOR=#FF0000]"<br />"[/color][COLOR=#990000];[/color]
[tab][tab][COLOR=#009900]$result[/color] [COLOR=#990000]=[/color] [b][COLOR=#000000]mysql_query[/color][/b][COLOR=#990000]([/color][COLOR=#009900]$query[/color][COLOR=#990000]);[/color]

[tab][b][COLOR=#0000FF]endforeach[/color][/b][COLOR=#990000];[/color]

[COLOR=#FF0000]}[/color]
[COLOR=#990000]?>[/color]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top