Lisa23 Posted September 27, 2010 Share Posted September 27, 2010 Hi i have a form which display the date in the format that i want which is m/d/yyyy but then when i try update it sets the date back to 01/01/1970 help please this how the update script looks like <?php //Get the key field to be amended$news_id = $_GET['news_id']; $news_date = date('Y-m-d', strtotime($_GET['new_date']));$subject = $_GET['subject'];$news_artical = $_GET['news_artical'];// check if there were any errors// check if there were any errors//$news_date = STR_TO_DATE('$news_date','%Y/%m/%d');$query = "UPDATE news SET news_date = '$news_date', subject='$subject', news_artical='$news_artical' WHERE news_id = '$news_id'";mysql_query($query) or die(mysql_error()); // execute query print "<p>The following records has been updated: </p>";$result = mysql_query($query) ;//if there was a problem - get the error message and go back if (!$result) { echo "There were errors :<br>". mysql_error(); } else //OK, then the insertion was successful { //Create a new query to display the new row in a table $query = "SELECT news_id, news_date, subject, news_artical, DATE_FORMAT(news_date, '%d/%m/%Y') as new_date FROM news WHERE news_id = '$news_id' "; $result = mysql_query($query) or die ("Error in query: $query. ".mysql_error()); echo "<table cellpadding=10 border=1>"; while($row = mysql_fetch_array($result)) { echo "<tr>"; echo "<td>".$row["news_id"]."</td>"; echo "<td><strong>" .$row["new_date"]."</strong></td>"; echo "<td><strong>".$row["subject"]."</strong></td>"; echo "<td width='55%'>".$row["news_artical"]."</td>"; echo "</tr>"; } //End while echo "</table>"; } //End Else insertion successful//End else successful Amendment ?> Quote Link to comment https://forums.phpfreaks.com/topic/214551-update-sets-date-back-to-01011970/ Share on other sites More sharing options...
Pikachu2000 Posted September 27, 2010 Share Posted September 27, 2010 Are you sure this $_GET['new_date'] shouldn't be $_GET['news_date']? Quote Link to comment https://forums.phpfreaks.com/topic/214551-update-sets-date-back-to-01011970/#findComment-1116452 Share on other sites More sharing options...
Lisa23 Posted September 27, 2010 Author Share Posted September 27, 2010 i dnt think so why do u think that do u think thats whats causing the problem?? i tried to change to $_GET['news_date'] but still doesnt solve the problem?? Quote Link to comment https://forums.phpfreaks.com/topic/214551-update-sets-date-back-to-01011970/#findComment-1116456 Share on other sites More sharing options...
rwwd Posted September 27, 2010 Share Posted September 27, 2010 The question is; What IS the contents of $_GET['new_date']; because if your using in within the strtotime() function, it actually is quite fussy on how the string is given, if it can't convert the string into an integer (which is what strtotime() does) it defaults to epoch (01/01/1970) So my advice is check HOW $_GET['new_date']; gets set in the first place, and check where it gets set, then try it in the function as a static string before you go dynamic... Rw Quote Link to comment https://forums.phpfreaks.com/topic/214551-update-sets-date-back-to-01011970/#findComment-1116459 Share on other sites More sharing options...
Pikachu2000 Posted September 27, 2010 Share Posted September 27, 2010 i dnt think so why do u think that do u think thats whats causing the problem?? i tried to change to $_GET['news_date'] but still doesnt solve the problem?? I asked because that seemed to be the naming convention you were using with the other variables, $_GET['news_id'], $_GT['news_artical'] . . . Quote Link to comment https://forums.phpfreaks.com/topic/214551-update-sets-date-back-to-01011970/#findComment-1116464 Share on other sites More sharing options...
gizmola Posted September 27, 2010 Share Posted September 27, 2010 I'm not sure how we would know when we don't know what $_GET['new_date'] contains. I'm also not sure why you would use strtotime when you don't care about the time element, and only want a php date variable. Quote Link to comment https://forums.phpfreaks.com/topic/214551-update-sets-date-back-to-01011970/#findComment-1116466 Share on other sites More sharing options...
Lisa23 Posted September 27, 2010 Author Share Posted September 27, 2010 well i thought that strot would allow me to set the date in a format of how i wish cz before i couldnt display how i wanted but now that strot and this line allow me to display the date in any format i want the problem is when update it sets back to 01-01-1927 $query = "UPDATE news SET news_date = '$news_date', subject='$subject', news_artical='$news_artical' WHERE news_id = '$news_id'"; Quote Link to comment https://forums.phpfreaks.com/topic/214551-update-sets-date-back-to-01011970/#findComment-1116468 Share on other sites More sharing options...
gizmola Posted September 27, 2010 Share Posted September 27, 2010 Again, you're not going to get any help with this until you inform us what $_GET['new_date'] contains when the script is run. We are not mind readers. Put in an: echo $_GET['new_date'] ; Also --- what is the datatype of the news_date column in the database. Something tells me that it's a timestamp. Quote Link to comment https://forums.phpfreaks.com/topic/214551-update-sets-date-back-to-01011970/#findComment-1116480 Share on other sites More sharing options...
Lisa23 Posted September 27, 2010 Author Share Posted September 27, 2010 ohh yeah new date is this line u'll notice i have news_date as new dare which comes from the form the other part is the action script of the this form $query = "SELECT news_id, news_date, subject, news_artical, DATE_FORMAT(news_date, '%d/%m/%Y') as new_date FROM news WHERE news_id = '$news_id' "; Quote Link to comment https://forums.phpfreaks.com/topic/214551-update-sets-date-back-to-01011970/#findComment-1116482 Share on other sites More sharing options...
Lisa23 Posted September 27, 2010 Author Share Posted September 27, 2010 this is the form script where new date is entered <?php // query to get records $news_id = $_GET['news_id'] ; // create query to delete record $query = "SELECT news_id, news_date, subject, news_artical, DATE_FORMAT(news_date, '%d/%m/%Y') as new_date FROM news WHERE news_id = '$news_id' "; //Run the query $result = mysql_query($query); //see if any rows were returned if (mysql_num_rows($result) > 0) { // yes - Display Form $row = mysql_fetch_array($result); //Fetch the row //Display the form with original values ?> [/php and this is the update action part of the form [code=php:0]<?php //Get the key field to be amended $news_id = $_GET['news_id']; $news_date = date('Y-m-d', strtotime($_GET['new_date'])); $subject = $_GET['subject']; $news_artical = $_GET['news_artical']; // check if there were any errors // check if there were any errors $query = "SELECT news_id, news_date, subject, news_artical, DATE_FORMAT(news_date, '%d/%m/%Y') as new_date FROM news WHERE news_id = '$news_id' "; //$news_date = STR_TO_DATE('$news_date','%Y/%m/%d'); $query = "UPDATE news SET news_date = '$new_date', subject='$subject', news_artical='$news_artical' WHERE news_id = '$news_id'"; mysql_query($query) or die(mysql_error()); Quote Link to comment https://forums.phpfreaks.com/topic/214551-update-sets-date-back-to-01011970/#findComment-1116488 Share on other sites More sharing options...
mikosiko Posted September 27, 2010 Share Posted September 27, 2010 ..... We are not mind readers. Put in an: echo $_GET['new_date'] ; ..... Is that difficult to understand what Gizmola asked for ? either the code that you posted is incomplete or you are using $_GET in the wrong way.... if your code is incomplete... post it again Quote Link to comment https://forums.phpfreaks.com/topic/214551-update-sets-date-back-to-01011970/#findComment-1116516 Share on other sites More sharing options...
Lisa23 Posted September 27, 2010 Author Share Posted September 27, 2010 sorry was trying to figure out it echos like 05/01/2010 Quote Link to comment https://forums.phpfreaks.com/topic/214551-update-sets-date-back-to-01011970/#findComment-1116517 Share on other sites More sharing options...
gizmola Posted September 28, 2010 Share Posted September 28, 2010 Ok, I think I see your problem: $query = "UPDATE news SET news_date = '$news_date', subject='$subject', news_artical='$news_artical' WHERE news_id = '$news_id'"; Notice that the variable needs to be $news_date not $new_date which is what you had. Quote Link to comment https://forums.phpfreaks.com/topic/214551-update-sets-date-back-to-01011970/#findComment-1116549 Share on other sites More sharing options...
gizmola Posted September 28, 2010 Share Posted September 28, 2010 And in case you were wondering why, the basic format for a mysql date needs to be YYYY-MM-DD. You were inadvertantly passing the '05/01/2010' which is not a valid mysql string constant for a date column. The code that converted it does in fact work properly but because you did not actually pass the variable you constructed, mysql took that as an invalid date, and defaulted it to the 1970 date, which for a timestamp is the first possible date you can store. Quote Link to comment https://forums.phpfreaks.com/topic/214551-update-sets-date-back-to-01011970/#findComment-1116551 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.