TransWikia.com

Obtain Date value from HTML form and pass it in PHP

Stack Overflow Asked on November 18, 2021

I’m trying to get a date from an HTML form into PHP, to then pass it to a MYSQL database. In PHP I get the date with a POST method.

$Data = $_POST['Data']; //Data 

In HTML i made up this:

<input type="date" name="Data" id="Data" required/>

When I try to send it to my database with the mysqli_query() method, I always get this error:

Incorrect date value: ” for column ‘Data’ at row 1

Can you please help me?

This is the query if you are interested. All the other variables work.

INSERT INTO 'table_name' ('col1', 'col2', 'col3', Data, 'col5', 'col6')

VALUES ('$var1', '$var2', '$var3', '$Data', '$var5', '$var6')";

EDIT: (Whole Form)

    <form action="insert.php">
        <div class="banner">
          <h1>Prenota pasti al Podere Diamante</h1>
        </div>
        <div class="item">
          <p>Nome</p>
          <div class="name-item">
            <input type="text" name="Nome" placeholder="Nome" required/>
          </div>
        </div>
        <div class="item">
          <p>Numero Telefonico</p>
          <input type="text" name="NTelefonico" placeholder="### ### ####" required/>
        </div>
        <div class="item">
          <p>Data di prenotazione</p>
          <input type="date" name="Data" id="Data" required/>
          <i class="fas fa-calendar-alt"></i>
        </div>
   <div class="item">
          <p>Orario di prenotazione</p>
          <input type="time" name="NGruppo" required min="19:00" max="21:30" step="1800"/>
               </div>
        <div class="item">
          <p>Con quante persone sarai?</p>
          <input type="number" name="NPersone" min="1" max="150" required/>
        </div>
         <div class="item">
          <p>Note aggiuntive</p>
          <textarea rows="3" name="Note"></textarea>
        </div>
         <div class="btn-block">
          <button type="submit" href="/">Invia</button>
        </div>
      </form>

3 Answers

If you need the current date just you the built-in date in php

$now = date('Y-m-d');

Else check your sql date column and make sure it's Varchar then do this

<form action="insert.php" 
method="post">
//in your insert try using
'".$Data."' in place of '$Data'

Answered by Temidayo Dtuzzy Omotayo on November 18, 2021

Change

<form action="insert.php">

to

<form action="insert.php" method="post">

The default HTTP method of a form is GET. You must specify POST since you are accessing it using $_POST. Then you should be able to get the value of the Data field in $Data.

If it doesn't fix the errors, try the following: 1 Check the datatype of Data column in your MySQL table, and make sure that your $Data is in correct format.

For example: If your Data column is a DATE column, $Data should be in YYYY-mm-dd format.

Other Date - Time types and format is given here:

Date and Time Types                          Description
-----------------------------------------------------------------
 DATE                         A date value in CCYY-MM-DD format
-----------------------------------------------------------------
 TIME                           A time value in hh:mm:ss format
-----------------------------------------------------------------
 DATETIME     A date and time value inCCYY-MM-DD hh:mm:ss format
-----------------------------------------------------------------
 TIMESTAMP      A timestamp value in CCYY-MM-DD hh:mm:ss format
-----------------------------------------------------------------
 YEAR                         A year value in CCYY or YY format
-----------------------------------------------------------------

Extra Read: Methods GET and POST in HTML forms - what's the difference?http://jkorpela.fi/forms/methods.html

Answered by gitwithash on November 18, 2021

In the database you need to set the date field to type date and in the input field of the form you need to capture the correct date format. Or alternatively use a date picker field


        <label><strong>Date </strong> </label></div>
              <input type="date" name="date">
            

Answered by user11948517 on November 18, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP