derekshull Posted October 7, 2012 Share Posted October 7, 2012 I have an html form that takes a date and send it to an php script: <form action='tupdatecompletiondate.php' method='post'><input type='hidden' name='idtosubmit' value=$id><tr> <td><label>Date:</label></td> <td><input type= 'date' name= 'date' value='Select Date' id='popupDatepicker'></td> </tr><input type='submit' value='Click to Update'></form> I've got it going to this script: $id = $_POST['idtosubmit']; $date = $_POST['date']; $datetoset = date('Y-m-d', strtotime($date)); $sql = "UPDATE needs SET completiondate=$datetoset WHERE ID=$id"; if (!mysql_query($sql)) { die('Error: ' . mysql_error()); } mysql_close(); Confused as to why it's not working. It takes the date, sends it to the php script, makes it into a format that is accepted by the DATE column, and then puts it in that column....well at least its supposed to. Quote Link to comment https://forums.phpfreaks.com/topic/269180-updating-date-column-not-working/ Share on other sites More sharing options...
marcus Posted October 7, 2012 Share Posted October 7, 2012 Put the $datetoset in quotes in your $sql variable. So: $sql = "UPDATE `needs` SET `completiondate`='".$datetoset.'' WHERE `ID`='".$id."'"; Quote Link to comment https://forums.phpfreaks.com/topic/269180-updating-date-column-not-working/#findComment-1383391 Share on other sites More sharing options...
ManiacDan Posted October 7, 2012 Share Posted October 7, 2012 Luckily you actually pasted the error message that you're clearly getting. That helped. Quote Link to comment https://forums.phpfreaks.com/topic/269180-updating-date-column-not-working/#findComment-1383417 Share on other sites More sharing options...
PFMaBiSmAd Posted October 7, 2012 Share Posted October 7, 2012 2012-10-07 is a subtraction math expression and equals 1995, i.e. SET completiondate=1995. Literal date vales are strings and must be enclosed within single-quotes. $sql = "UPDATE needs SET completiondate='$datetoset' WHERE ID=$id"; You also need to validate/cast the $id before putting it into the query statement to prevent sql injection and to prevent sql errors when $id is not supplied at all. Quote Link to comment https://forums.phpfreaks.com/topic/269180-updating-date-column-not-working/#findComment-1383494 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.