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
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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.