Reputation: 1
I can't insert date(even my whole form) to mySQL. Here's my code:
if($_SERVER["REQUEST_METHOD"]=="POST")
{
$con = mysqli_connect('localhost','root','','e-Laundrydb');
$tglPickup = $_POST['tglPickup'];
$barang = $_POST['inputItems'];
$waktuPickup = $_POST['waktuPickup'];
$query_order = "INSERT INTO order(Barang,Tanggal_Pengambilan,Waktu_Pengambilan) VALUES('$barang','$tglPickup','$waktuPickup');";
$result = mysqli_query($con,$query_order);
if($result == false)
{
echo "<script>alert('INSERT IS NOT SUCCESS');</script>";
}
else
{
echo "<script>alert('INSERT SUCCESS');</script>";
}
}
I use input type="date"
in my php. And here's my table in database:
Table Order
I can't find where's the problem. i updated the code and follow like your suggestions but still get nothing :( here's the code again:
if($_SERVER["REQUEST_METHOD"]=="POST")
{
//include('config/koneksi.php');
$con = mysqli_connect('localhost','root','','e-laundryDB');
//INSERT KE TABLE ORDER
$tglPickup = date('Y/m/d', strtotime($_POST['tglPickup']));
$barang = $_POST['inputItems'];
$waktuPickup = $_POST['waktuPickup'];
$query_order = "INSERT INTO 'order'(Barang,Tanggal_Pengambilan,Waktu_Pengambilan) VALUES('$barang','$tglPickup','$waktuPickup')";
$result = mysqli_query($con,$query_order);
if($result == false)
{
echo "<script>alert('INSERT IS NOT SUCCESS');</script>";
}
else
{
echo "<script>alert('INSERT SUCCESS');</script>";
}
mysqli_close($con);
}
Upvotes: 0
Views: 3799
Reputation: 1
Convert 'd-m-Y' date format to 'Y-m-d' date format
$tglPickup=$_POST['tglPickup']; //d-m-Y
$tglPickup=explode('-',$tglPickup);
$tglPickup=$tglPickup[2]."-".$tglPickup[1]."-".$tglPickup[0]; //Y-m-d
Upvotes: 0
Reputation: 1
$query_order = INSERT INTO order(Barang,Tanggal_Pengambilan,Waktu_Pengambilan) VALUES('$barang','$tglPickup','$waktuPickup')
INSERT order SET `Barang`='".$barang."',`Tanggal_Pengambilan`='".$tglPickup."',`Waktu_Pengambilan`='".$waktuPickup."'";
Upvotes: 0
Reputation: 2704
Ideally you'll want the the following Date format: Y-m-d H:i:s
You probably want to validate the format before attempting the insert query, that way if the format is invalid you can return a friendly error message to the user at this point, rather than attempting an insert and then not being easily able to decipher what went wrong.
Something like this should work:
function validDateFormat($date, $format = 'Y-m-d H:i:s') {
$date = DateTime::createFromFormat($format, $date);
if ($date === false) {
return false;
}
return true;
}
So you can call it like so:
$tglPickup = $_POST['tglPickup'];
if (validDateFormat($tglPickup)) {
// Insert logic
} else {
// Return a friendly error to the user
}
I presume the user isn't just entering the date and is selecting it so you should have full control of the format that you receive, but personally I always find programming paranoid helps :d
Hope that helps anyway.
Edit: As @devpro mentioned you are using Date
and not DateTime
so for the format, you can simply remove the H:i:s
in the example I provided, or add a second parameter to the function call, i.e.
if (validDateFormat($tglPickup, 'Y-m-d')) {}
Upvotes: 1
Reputation: 16117
Well there are so many answers available related to your issue. As per your Database, your date column type is DATE
not DATETIME
, so you need to store values in this format:
YYYY-MM-DD
How can you get date in this format?
$tglPickup = date('Y-m-d',strtotime($_POST['tglPickup']));
I think this column Tanggal_Pengambilan
is your DATE column so i use it.
Side Note:
I am not sure, what kind of input are you getting from the $_POST['tglPickup']
, if its proper than it will work.
Upvotes: 1
Reputation: 551
if($_SERVER["REQUEST_METHOD"]=="POST")
{
$con = mysqli_connect('localhost','root','','e-Laundrydb');
$tglPickup = date('Y-m-d H:i:s',strtotime($_POST['tglPickup']));
$barang = $_POST['inputItems'];
$waktuPickup = $_POST['waktuPickup'];
$query_order = "INSERT INTO order(Barang,Tanggal_Pengambilan,Waktu_Pengambilan) VALUES('$barang','$tglPickup','$waktuPickup');";
$result = mysqli_query($con,$query_order);
if($result == false)
{
echo "<script>alert('INSERT IS NOT SUCCESS');</script>";
}
else
{
echo "<script>alert('INSERT SUCCESS');</script>";
}
}
Upvotes: 1
Reputation: 159
Try to adapt your date format to the date format in your database. for example:
date('Y-m-d H:i:s'); //2016-01-19 12:48:00
Upvotes: 0
Reputation: 1081
You have to set date format to 'YYYY-MM-DD HH:ii:ss
'. becasue mysql used this format for date.
Check for date format you are passing in query, there might be this issue.
Thanks.
Upvotes: 3