Ricky97
Ricky97

Reputation: 121

PHP date insert using datepicker

My PHP script writes "Data entered!", but there are no records in the database. Here is the code.

<?php
$startdate = $_POST['start'];
$enddate = $_POST['end'];
$type = $_POST['type'];

if ($startdate && $enddate && $type) {
$con=mysqli_connect("localhost", "root", "")or die("Can not connect to DB"); 
mysqli_select_db($con,"booker")or die("Can not select DB");
mysqli_query($con,"INSERT INTO events(start_date,end_date,type) VALUES('$startdate','$enddate','$type')");
echo "Data entered!";
}
else {
echo "Please fill in the form!";
}


?>

I select dates using datepicker and type is selected through select option in html (3 options available). Names of html form fields are start, end, type. Fields in DB are start_date, end_date, type. PHP script runs with no errors and everything seems ok, but there are no records in DB.

Upvotes: 2

Views: 1567

Answers (2)

Raden Bagus
Raden Bagus

Reputation: 560

Nothing wrong with your PHP script. It just comes from the input. Make sure you have a standard format like

"YYYY-MM-DD" (use - 'hypen').

MySql won't respond if you use outside of that format, for example

"YYYY/MM/DD" -> this won't work.

Upvotes: 0

Daniel G
Daniel G

Reputation: 549

Send all dates you want to INSERT with SQL through date() like so:

$date = date('Y-m-d', strtotime($_POST['date']));

Upvotes: 0

Related Questions