Jump to content

Updating Date Column Not Working!


derekshull

Recommended Posts

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.

Link to comment
https://forums.phpfreaks.com/topic/269180-updating-date-column-not-working/
Share on other sites

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.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.