TheNotebook Posted April 14, 2011 Share Posted April 14, 2011 I'm trying to get an expiry date to work on my project.... but at the moment... it doesnt seem to be working. Heres the code for the main news submission form: <form action='newsubmit.php' method='POST'> Title: <input type='text' name='title' /><br /> Story: <input type='text' name='story' /><br /> Upload Image: <a href="#" onclick="window.open('fileuploadprocess.php','mywindow','width=450,height=300,toolbar=no,location=no,directories=no,s tatus=no,menubar=no,scrollbars=yes,copyhistory=yes,resizable=yes')"> Click here</a> <?php require_once('../../../2740/recaptcha/recaptchalib.php'); $publickey = "6Ldbd8ASAAAAAL96lIWHSDRmoaVJhgQJuKQwXNbd"; echo recaptcha_get_html($publickey); ?> <input type='submit' name='submit' value='Submit' /> <input name='reset' type='reset' value='Reset' /> </form> Heres the code for the submission page: <?php include_once("functions.php"); session_start(); $submit = clean_string($_POST['submit']); $title = clean_string($_POST['title']); $story = clean_string($_POST['story']); $datetoday = date('Y-m-d', $expires); $daystoexpire = '7'; $expires = time() + (86400 * $daystoexpire); $userid = $_SESSION['userID']; $newsimage = $_SESSION['imagelocation']; $message = ''; require_once('../../../2740/recaptcha/recaptchalib.php'); $privatekey = "6Ldbd8ASAAAAAFz8VT29H5w4WLNjsbI-mFY2QkaC"; $resp = recaptcha_check_answer($privatekey, $_SERVER["REMOTE_ADDR"], $_POST["recaptcha_challenge_field"], $_POST["recaptcha_response_field"]); $message = ""; if ($submit == 'Submit') { if (!$resp->is_valid) { // What happens when the CAPTCHA was entered incorrectly $errMessage = $resp->error; $message = "<strong>The reCAPTCHA wasn't entered correctly. Please try again.</strong><br />" . "(reCAPTCHA said: $errMessage)<br />"; } else { // Process valid submission data here if ($title && $story) { //process details here if (strlen($title) > 80) { $message = "The title you have provided is too long. Please <a href='news_entry.php'>try again</a>"; } else { if (strlen($story) > 300) { $message = "The news story you have provided is too long. Please <a href='news_entry.php'>try again</a>"; } else { require_once("db_connect.php"); if ($db_server) { mysql_select_db("a200458721"); //Insert news story into news table $query = "INSERT INTO news (usersID, title, story, image, datetoday, expires) VALUES ('$userid', '$title', '$story', '$image', '$datetoday', '$expires')"; mysql_query($query) or die("User insert failed. " . mysql_error() . "<br />" . $query); $message = "News Story Successfully Submitted! Return to <a href='index.php'>homepage</a>"; } } } } else { $message = "Failed to connect to database"; } if ($expires < time()) { // Expired! } require_once("db_close.php"); } } else { $message = "please fill in all the required fields"; } echo $message; mysql_close($db_server); ?> At the moment, this is the error its throwing up: I'm not sure why its saying about the 1970 date either... User insert failed. Incorrect datetime value: '1303391514' for column 'expires' at row 1 INSERT INTO news (usersID, title, story, image, datetoday, expires) VALUES ('13', 'NEWS TITLE GOES HERE', 'NEWS STORY', '', '1970-01-01', '1303391514') Any ideas on why its throwing me up the error? Quote Link to comment Share on other sites More sharing options...
gristoi Posted April 14, 2011 Share Posted April 14, 2011 you are calling the $expires variable before you have declared it. The code should look like: $submit = clean_string($_POST['submit']); $title = clean_string($_POST['title']); $story = clean_string($_POST['story']); $daystoexpire = '7'; $expires = time() + (86400 * $daystoexpire); $datetoday = date('Y-m-d', $expires); Quote Link to comment Share on other sites More sharing options...
Muddy_Funster Posted April 14, 2011 Share Posted April 14, 2011 You are trying (for reasons I can't fathom) to force an integer value into a datetime field. Look into using CAST or CONVERT (or rethinking the whole process, which-ever works for you). As for the "1970 thing"...what is the actual value of $expires *at run time* and where is it coming from? Quote Link to comment Share on other sites More sharing options...
Psycho Posted April 14, 2011 Share Posted April 14, 2011 PHP timestamps and MySQL timestamps are not compatible. First though, regarding the 1970 reference, look at how you define the variables $today and $expires $datetoday = date('Y-m-d', $expires); $daystoexpire = '7'; $expires = time() + (86400 * $daystoexpire); $userid = $_SESSION['userID']; $newsimage = $_SESSION['imagelocation']; You first try to define $datetoday using date() with $expires as the timestamp - but you have not yet defined $expires! And timestamps start at, you guessed it, 1-1-1970. Just use date() without a timestamp to have it generate a value based upon current date/time. Ok, as for $expires, you need to convert between PHP and MySQL timestamps. So, when inserting the record you can use the following: $query = "INSERT INTO news (usersID, title, story, image, datetoday, expires) VALUES ('$userid', '$title', '$story', '$image', '$datetoday', FROM_UNIXTIME($expires))"; Then when you retrieve the value from the db you will need to convert back to unit time. $query = "SELECT UNIX_TIMESTAMP(expires) FROM news"; Here is a great article on handling dates and times between PHP and MySQL: http://www.richardlord.net/blog/dates-in-php-and-mysql Quote Link to comment Share on other sites More sharing options...
TheNotebook Posted April 14, 2011 Author Share Posted April 14, 2011 Thanks guys, that seems to have sorted the date problem out. However, I still seem to be getting this error: User insert failed. Incorrect integer value: '' for column 'usersID' at row 1 INSERT INTO news (usersID, title, story, image, datetoday, expires) VALUES ('', 'TITLE WILL GO HERE', 'NEWS STORY WILL GO HERE', '', '2011-04-21', '1303397348') My table in mysql looks like this: Table name: news ID (int) usersID (int) title (varchar) story (longtext) image (varchar) datetoday (date) expires (timestrap) i've looked through the code, and still can't see whats wrong with it and why it isnt working... Does anyone have any ideas? Quote Link to comment Share on other sites More sharing options...
Psycho Posted April 14, 2011 Share Posted April 14, 2011 I don't mean to be rude, but I'm curious. Your code actually has some good debugging functionality (which I rarely see), but you seem to have no clue on how to interpret it? Did you just copy paste that code or something? Your error message clearly states that the value for 'usersID' is invalid. Plus, from the query you can see that the value is a null string. User insert failed. Incorrect integer value: '' for column 'usersID' at row 1 INSERT INTO news (usersID, title, story, image, datetoday, expires) VALUES ('', 'TITLE WILL GO HERE', 'NEWS STORY WILL GO HERE', '', '2011-04-21', '1303397348') Looking at your query construction the variable $useridis supposed to be used as that value. From that we can determine that either that value was not set or it is set as an empty value.So, the next step is to determine if that value was ever set. And we can determine that is is based upon this line earlier in the script: [/color][color=#0000BB]$userid [/color][color=#007700]= [/color][color=#0000BB]$_SESSION[/color][color=#007700][[/color][color=#DD0000]'userID'[/color][color=#007700]][/color][/color] So, finally, we can conclude that either that session value is not set or it has a null string value. Did you start the session at the top of that page? Did you confirm that the value was being set correctly previously? Quote Link to comment 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.