BelowZero Posted March 21, 2012 Share Posted March 21, 2012 I'm having some trouble understanding how to use date and time when updating my database. The table is already populated with Date and Time fields in date and time format. I need to update the table based on the date and time, but I can't make it work. I tested all the variables and they are being sent correctly. I'm pretty sure the problem lies in how I'm trying to match the Date and Time fields with $dateID and $timeID in the WHERE part of the query, but I'm not sure how to fix that! <?php include("opendatabase.php"); $size = count($_POST['player1']); $i=0; $thisyear = date("Y"); while ($i < $size) { $dateID = $_POST['date'][$i]; $timeID = $_POST['time'][$i]; $player1 = ($_POST['player1'][$i]); $player2 = ($_POST['player2'][$i]); $player3 = ($_POST['player3'][$i]); $player4 = ($_POST['player4'][$i]); $player5 = ($_POST['player5'][$i]); mysql_query(" UPDATE Daysheets$thisyear SET Player1='$player1', Player2='$player2', Player3='$player3', Player4='$player4', Player5='$player5' WHERE Date = '$dateID' AND Time = '$timeID' "); $i++; } header("Location: /teetimes/publicteetimes.php?teetimedate=$dateID"); mysql_close($con) ?> Any help appreciated! I should also mention that whenever I run this query it erases anything that was previously entered in the database except the date and time columns... Quote Link to comment https://forums.phpfreaks.com/topic/259435-date-and-time-issues/ Share on other sites More sharing options...
freelance84 Posted March 21, 2012 Share Posted March 21, 2012 Have you check for a mysql_error()? A nice trick I like to do when something looks like it should working but isn't is just echo out the whole query and check everything looks ok. <?php include("opendatabase.php"); $size = count($_POST['player1']); $i=0; $thisyear = date("Y"); while ($i < $size) { $dateID = $_POST['date'][$i]; $timeID = $_POST['time'][$i]; $player1 = ($_POST['player1'][$i]); $player2 = ($_POST['player2'][$i]); $player3 = ($_POST['player3'][$i]); $player4 = ($_POST['player4'][$i]); $player5 = ($_POST['player5'][$i]); echo <<<_END mysql_query(" UPDATE Daysheets$thisyear SET Player1='$player1', Player2='$player2', Player3='$player3', Player4='$player4', Player5='$player5' WHERE Date = '$dateID' AND Time = '$timeID' "); <br/><br/> _END; $i++; } //header("Location: /teetimes/publicteetimes.php?teetimedate=$dateID"); mysql_close($con) ?> Quote Link to comment https://forums.phpfreaks.com/topic/259435-date-and-time-issues/#findComment-1329932 Share on other sites More sharing options...
BelowZero Posted March 21, 2012 Author Share Posted March 21, 2012 freelance84, Ran it with your additions to check for errors and got this: Parse error: syntax error, unexpected $end in /var/www/html/teetimes/insert_tee_times.php on line 46 Should that give me a clue? Quote Link to comment https://forums.phpfreaks.com/topic/259435-date-and-time-issues/#findComment-1329938 Share on other sites More sharing options...
freelance84 Posted March 21, 2012 Share Posted March 21, 2012 Probably, what is on line 46? And is insert_tee_times.php the script you are posting about? Quote Link to comment https://forums.phpfreaks.com/topic/259435-date-and-time-issues/#findComment-1329941 Share on other sites More sharing options...
BelowZero Posted March 21, 2012 Author Share Posted March 21, 2012 line 46 is the end of the script - ?> and yes, the file in question is insert_tee_times.php Quote Link to comment https://forums.phpfreaks.com/topic/259435-date-and-time-issues/#findComment-1329945 Share on other sites More sharing options...
smerny Posted March 21, 2012 Share Posted March 21, 2012 whenever you see unexpected end, you're missing something... like a bracket or semi-colon looks like you are missing a semi-colon on your mysql_close() Quote Link to comment https://forums.phpfreaks.com/topic/259435-date-and-time-issues/#findComment-1329946 Share on other sites More sharing options...
BelowZero Posted March 21, 2012 Author Share Posted March 21, 2012 yes, smerny. I put the semi-colon in but am still getting the unexpected end. Beings that this file is the result of an include() in another file, could the error be outside the <<<_end ..._end? I don't see anything missing in the query. Could the date and time comparisons be the issue? Quote Link to comment https://forums.phpfreaks.com/topic/259435-date-and-time-issues/#findComment-1329963 Share on other sites More sharing options...
Mahngiel Posted March 21, 2012 Share Posted March 21, 2012 mysql_close($con); ?> Quote Link to comment https://forums.phpfreaks.com/topic/259435-date-and-time-issues/#findComment-1329965 Share on other sites More sharing options...
BelowZero Posted March 21, 2012 Author Share Posted March 21, 2012 This is the code I now have: <?php include("opendatabase.php"); $size = count($_POST['player1']); $i=0; $thisyear = date("Y"); while ($i < $size) { $dateID = $_POST['date'][$i]; $timeID = $_POST['time'][$i]; $player1 = ($_POST['player1'][$i]); $player2 = ($_POST['player2'][$i]); $player3 = ($_POST['player3'][$i]); $player4 = ($_POST['player4'][$i]); $player5 = ($_POST['player5'][$i]); echo <<<_END mysql_query(" UPDATE Daysheets$thisyear SET Player1='$player1', Player2='$player2', Player3='$player3', Player4='$player4', Player5='$player5' WHERE Date = '$dateID' AND Time = '$timeID' "); <br/><br/> _END; $i++; } //header("Location: /teetimes/publicteetimes.php?teetimedate=$dateID"); mysql_close($con); ?> The error checking still results in an unexpected end. Quote Link to comment https://forums.phpfreaks.com/topic/259435-date-and-time-issues/#findComment-1329970 Share on other sites More sharing options...
smerny Posted March 21, 2012 Share Posted March 21, 2012 you moved _END it always needs to be far left, no whitespace before it Quote Link to comment https://forums.phpfreaks.com/topic/259435-date-and-time-issues/#findComment-1329975 Share on other sites More sharing options...
BelowZero Posted March 21, 2012 Author Share Posted March 21, 2012 Okay. Well after now running the script I am getting a whole page full of info. Every query in the while loop is displayed. Does this mean each line contains an error? If so, then the problem has to be back to the date and time issue. Quote Link to comment https://forums.phpfreaks.com/topic/259435-date-and-time-issues/#findComment-1329984 Share on other sites More sharing options...
freelance84 Posted March 21, 2012 Share Posted March 21, 2012 Okay. Well after now running the script I am getting a whole page full of info. Every query in the while loop is displayed. Does this mean each line contains an error? If so, then the problem has to be back to the date and time issue. No. You have not run the query. you have simply echo'd each query that would have been. Now check the queries are what you expected them to say.. Quote Link to comment https://forums.phpfreaks.com/topic/259435-date-and-time-issues/#findComment-1329988 Share on other sites More sharing options...
BelowZero Posted March 21, 2012 Author Share Posted March 21, 2012 Well... The query looked good so I removed the error checking and ran the script. Everything worked as expected. Thanks for your help!! Quote Link to comment https://forums.phpfreaks.com/topic/259435-date-and-time-issues/#findComment-1329991 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.