0

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); } 
6
  • 1
    what is the date value that goes in? if you have created the table with date function it will not take time in it Commented Jan 19, 2016 at 11:43
  • sorry, i tried to specified my question. here i edited it now, thanks. Commented Jan 19, 2016 at 11:45
  • is your problem that the whole form values are not being inserted? or is it that only date is not inserted? Commented Jan 19, 2016 at 11:47
  • what is the value you are getting in $tglPickup Commented Jan 19, 2016 at 11:48
  • First enable errors and share the error message you get. Second, you are using mysql date field, so you will need to insert date like Y-m-d, it does not matter if you a date input field, or a text. Commented Jan 19, 2016 at 11:50

7 Answers 7

3

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.

Sign up to request clarification or add additional context in comments.

Comments

1
 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>"; } } 

Comments

1

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.

1 Comment

Yes, tgl pickup is date column. I use <input type="date"></input>
1

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')) {} 

Comments

0

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 

Comments

0
$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."'"; 

Comments

0
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 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.