Inserting a Datepicker Date Into a Mysql Record

6,250

3

4

This is a short tutorial to show to capture a datepicker date from a HTML web form and capture it in a MYSQL table. The steps are as follows:

1. Create a web page ADD_FORM.php file with a form object and assign a datepicker object. This will be used to capture the data and send it to a second php file which writes it to a MySql database. This involves setting up links to the appropriate js libraries in the header section. Next, create the web form and and datepicker object in the body of the web page.

2. Create a php file to capture the data from the \web_\form page.

Teacher Notes

Teachers! Did you use this instructable in your classroom?
Add a Teacher Note to share how you incorporated it into your lesson.

Step 1: Create a Web Page ADD_FORM.php and Form Object and Assign a Datepicker Object

  1. Here is my code for ADD_FORM.php. You will note that the initial header section is native html and in fact you could write all the code in HTML.
  2. Header section - see attached file.
  3. The relevant form element is shown in the attached file. The form calls the next php file - insert_record.php


Step 2: Insert Your Datepicker Field Into the Mysql Database

I am not going to cover the complete php file but the relevant parts are as follows:

1. $due_date= $_POST['datepicker'];

Please ensure that you escape the variable to prevent sql injections.

$due_date = mysql_real_escape_string($due_date);

Also ensure that references to your database environment are not in webroot, e.g. if /var/www/html is web root, place it outside this.

Also use PDO (PHP Data Objects) to insert your date using a parameterized SQL query. - ensure that your data field has the right data type

due_date | datetime | YES | | NULL

An example of the captured data in MySql is as follows:

2017-10-29 18:48:12

This is the default MYSql date format. That's it, good luck

Be the First to Share

    Recommendations

    • Made with Math Contest

      Made with Math Contest
    • Multi-Discipline Contest

      Multi-Discipline Contest
    • Robotics Contest

      Robotics Contest

    4 Discussions

    0
    None
    Rumi Johnson

    6 months ago

    Sir, this tutorial kinda helpful, but I'm really kinda stuck to insert the date into my database.

    can you help me with that part?

    1 reply
    0
    None
    scanosFaisal Shah Khan

    Reply 5 months ago

    It definitely works. For example, in the form below I built a html form using the code in this Instructable and when I click the submit button, I returned a POST variable with the following value - datepicker: 2019-05-11 (11th May 2019)

    So, usually, you would write a PHP script to insert datepicker: 2019-05-11 into a MySQl table.
    To show how this works in Mysql, I created a temporary table
    CREATE TABLE tempdate (
    -> id INT(6) UNSIGNED AUTO_INCREMENT PRIMARY KEY,
    -> reg_date DATETIME
    -> );
    Query OK, 0 rows affected (0.09 sec)
    I am able to add the value 2019-05-11 successfully to the MySql table
    > insert into tempdate (reg_date) values ('2019-05-11');
    Query OK, 1 row affected (0.01 sec)
    > select * from tempdate;
    +----+---------------------+
    | id | reg_date |
    +----+---------------------+
    | 2 | 2019-05-11 00:00:00 |
    +----+---------------------+
    1 row in set (0.00 sec)

    Hope this helps.




    Untitled.png