13

I am trying to insert date into mysql but everytime it fails and comes out as 0000-00-00 in phpmyadmin

My date format is like 2012-08-06 (yyyy-mm-dd) and the date field type in database is date.

$date = "2012-08-06";
mysql_query("INSERT INTO data_table (title, date_of_event) 
             VALUES('". $_POST['post_title'] ."',
                    '". $date ."')") or die(mysql_error()); 

tried changing - to / or removing them, it doesn't work.

6
  • 2
    I suggest using timestamps (time()) instead of date. and use the date() function to convert it to any format when displaying. Commented Aug 6, 2012 at 11:19
  • 3
    you have two problems (sql injection vulnerability and usage of an old, soon to be deprecated extension) that can be both solved if you switch to PDO (or mysqli) Commented Aug 6, 2012 at 11:19
  • @MakuraYami Why is that ? can you explain a little ? Commented Aug 6, 2012 at 11:32
  • @mishu yes I am aware of both problems :) it's just I found about PDO and mysqli yesterday and I want to finish up the coding then convert the extension and fix security problems. Commented Aug 6, 2012 at 11:34
  • 2
    @xperator, When you use timestamp it saves your time as a int. which will never give problems as you are having now. and it has the advantage of being able to be written out in ANY date format without any risky string-to-date functions. Commented Aug 6, 2012 at 11:45

6 Answers 6

16

try

$date = "2012-08-06";
$date=date("Y-m-d",strtotime($date));
Sign up to request clarification or add additional context in comments.

3 Comments

I agree with @Mike here, what's the difference between the two? codepad.org/KFF4pi5u (except for making the script a bit more complicated)
Well in past I encountered with same problem, and the way i did work. Again, I just tried to insert date as xperator did, the date was inserted. @xperator, will you mind printing the sql statement?
This answer is nonsense -- it makes no change to the string. No one should be implementing this non-solution. Code-only answers are low value on Stack Overflow because they do very little to educate/empower thousands of future researchers.
14

try CAST function in MySQL:

mysql_query("INSERT INTO data_table (title, date_of_event)
VALUES('". $_POST['post_title'] ."',
CAST('". $date ."' AS DATE))") or die(mysql_error()); 

4 Comments

try again there was one extra comma
whats is the exact data type of your field date_of_event?
oops I had a tiny problem with that comma. fixed and works now :)
A prepared statement should be used for security/stability. This snippet should not be used in any professional code.
2

Unless you want to insert different dates than "today", you can use CURDATE():

$sql = 'INSERT INTO data_tables (title, date_of_event) VALUES ("%s", CURDATE())';
$sql = sprintf ($sql, $_POST['post_title']);

PS! Please do not forget to sanitize your MySQL input, especially via mysql_real_escape_string ()

2 Comments

No it's not the current date, today date was just an example :)
A prepared statement should be used for security/stability. This snippet should not be used in any professional code.
0

try converting the date first.

$date = "2012-08-06";

mysql_query("INSERT INTO data_table (title, date_of_event)
               VALUES('" . $_POST['post_title'] . "',
                      '" . $date . "')") 
           or die(mysql_error());

1 Comment

A prepared statement should be used for security/stability. This snippet should not be used in any professional code.
0

How to debug SQL queries when you stuck

Print you query and run it directly in mysql or phpMyAdmin

$date = "2012-08-06";
$query= "INSERT INTO data_table (title, date_of_event) 
             VALUES('". $_POST['post_title'] ."',
                    '". $date ."')";
echo $query;
mysql_query($query) or die(mysql_error()); 

that way you can make sure that the problem is not in your PHP-script, but in your SQL-query

How to submit questions on SQ-queries

Make sure that you provided enough closure

  • Table schema
  • Query
  • Error message is any

1 Comment

A prepared statement should be used for security/stability. This snippet should not be used in any professional code.
-1
$date=$year."-".$month."-".$day; 
$new_date=date('Y-m-d', strtotime($dob)); 
$status=0;  
$insert_date = date("Y-m-d H:i:s");  
$latest_insert_id=0; 

$insertSql="insert into participationDetail (formId,name,city,emailId,dob,mobile,status,social_media1,social_media2,visa_status,tnc_status,data,gender,insertDate)values('".$formid."','".$name."','".$city."','".$email."','".$new_date."','".$mobile."','".$status."','".$link1."','".$link2."','".$visa_check."','".$tnc_check."','".json_encode($detail_arr,JSON_HEX_APOS)."','".$gender."','".$insert_date."')";

1 Comment

A prepared statement should be used for security/stability. This snippet should not be used in any professional code. Furthermore, code-only answers are low value on Stack Overflow because they do very little to educate/empower thousands of future researchers.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.