jQuery datepicker in HTML form to update MySQL rows

你离开我真会死。 提交于 2020-01-06 04:09:14

问题


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


回答1:


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.




回答2:


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?



来源:https://stackoverflow.com/questions/38964225/jquery-datepicker-in-html-form-to-update-mysql-rows

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!