简体   繁体   中英

jQuery datepicker in HTML form to update MySQL rows

I've done heaps of research on this topic but for some reason nothings working!

I want to use the datepicker widget to update a row in MySQL (which has a date column). For some reason, whenever I try to update the row (which works for other input things) it says in my MySQL table 0000-00-00 ??

This is my jQuery code:

<script>
  $( function() {
    $( "#datepicker" ).datepicker({
        dateFormat:"yy-mm-dd",
        altField: "#alternate",
      altFormat: "DD, d MM, yy"
    });
    $( "#selectable" ).selectable();
  } );
  </script>

And my HTML:

<p> Date: <input name="slot_date" id="datepicker" > &nbsp; <input type="text" id="alternate" size="30"></p>

My PHP:

  if( isset($_POST['btn-book']) ) { 

 $slot_date = date('yy-mm-dd',strtotime($_POST['slot_date']));
 $slot_line=$_POST['slot_line'];
 $reason=$_POST['reason'];
 $id=$_POST['id'];

 $sql="UPDATE appointments SET slot_date='$slot_date', slot_line='$slot_line', reason='$reason' WHERE id='$id'";
 $result=mysql_query($sql);


 if($result){
  header ("Location: homepage_loggedin_book_success.php");
 }else {
  echo "ERROR";
 } 
}

Anyway, i've been trying for ages but the datepicker wont enter into my MySql table??

Use strtotime() and date() to enforce your desired format.

$slot_date = date('Y-m-d',strtotime($slot_date)); # will output 2016-08-16
$sql="UPDATE appointments SET slot_date='$slot_date', slot_line='$slot_line', reason='$reason' WHERE id='$id'";

Your code may be prone to SQL injections. Please refer to http://php.net/manual/en/security.database.sql-injection.php .

In the original form which created the record, is everything being set correctly? Are the slot_line and reason fields updating correctly? If they aren't the problem could be in your PHP. Could you post more of the PHP code for context?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM