php – Insert datetime-local into MySql-ThrowExceptions

Exception or error:

I’m creating a simply form to insert event data into my database.

Form:

<form enctype="multipart/form-data" action="upload.php" method="POST">

Event Date/Time: <input type="datetime-local" name="event_date">

Event Title: <input type="text" name="event_title">

Event Details:
<textarea id="tiny_mce" name="description" rows="8"></textarea>

    <input type="submit" value="Upload">

</form>

Action: upload.php

// new data
$event_date     = $_POST['event_date'];
$event_title    = $_POST['event_title'];
$description    = $_POST['description'];

// query
$addevent = DB::getInstance()->insert('events', array(
    'event_date ' => $event_date,
    'event_title' => $event_title,
    'description' => $description,

));
echo "<pre>".
var_dump($addevent);
echo "</pre>";

If I do a var_dump on each value in my upload script, I get the value to return correctly.

When I do a var_dump($addevent); it comes back as bool(false) and nothing gets added to my table.

If I remove <input type="datetime-local" name="event_date"> & $event_date = $_POST['event_date']; Everything works fine.

So I narrowed down that the problem must be in the datetime.

How should my DATETIME be setup properly in my database.

Right now I have event_date DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP

Obviously this is not working… Any suggestions on what it should be?

How to solve:

Assuming why removing event_date works would be due to your DB class finds no error in casting or format and allows the save to continue as opposed to sending malformed data.

DATETIME expects a string like date("Y-m-d H:i:s")

If you can’t verify that format is coming in to your script, you can allow PHP to parse and format it for you like: date("Y-m-d H:i:s",strtotime($event_date))

Answer:

May be this is not the answer you want. But I want to suggest you how to debug data POST in upload.php.
By this your statement:

When I do a var_dump($addevent); it comes back as bool(false)

You should debug only $_POST['event_date']; by die($event_date);. So by debugging only event_date like this, you can know what is the value of $_POST['event_date'];

// new data
$event_date     = $_POST['event_date'];
die($event_date); 
//this will show you the value of $_POST['event_date'];
$event_title    = $_POST['event_title'];
$description    = $_POST['description'];

// query
$addevent = DB::getInstance()->insert('events', array(
    'event_date ' => $event_date,
    'event_title' => $event_title,
    'description' => $description,

));
echo "<pre>".
var_dump($addevent);
echo "</pre>";

Answer:

The problem was here:

// query
$addevent = DB::getInstance()->insert('events', array(
    'event_date ' => $event_date,
    'event_title' => $event_title,
    'description' => $description,

There is a space in between 'event_date ' … should be no space like this 'event_date' … Bonehead Mistake.
I ended up inserting my datetime-local into my table with a timestamp option like this so I could output it where I wanted and style the the date and time to my liking with css. Here is what my table looked like:
enter image description here

Thanks for all the help, I still learned some useful info.

Answer:

The way I see this: Do you want to use the server CURRENT_TIMESTAMP to have the time the client hit “Submit”. Or do you want to give the client the freedom to enter the time and date. You are trying to do both in your code.

I am going to guess you want the CURRENT_TIMESTAMP you set up on the MySQL database. Drop the:

In the SQL code change the $event_date VALUE to (DEFAULT).

Leave a Reply

Your email address will not be published. Required fields are marked *