1

Possible Duplicate:
Convert MMDDYYYY to date for PHP
PHP mysql insert date format

I use PDO to insert data on my database, however I have a date in the form, here is the part of the code:

$name = "John Doe"; // from form
$bdate = 05/13/1989; // from form

$query = "INSERT INTO details (name, bdate) VALUES (:name, :bdate)";
$stmt = $this->handle->prepare($query);
$stmt->bindParam(':name',$name,PDO::PARAM_STR);
$stmt->bindParam(':bdate',$bdate,PDO::PARAM_STR);
$stmt->execute();

Upon running the script, I encounter this error on my log: "SQLSTATE[22007]: Invalid datetime format: 1292 Incorrect date value: '05/13/1989' for column 'bdate' at row 1".

My database:

+-------+--------------+
| name  | varchar(100) |
+-------+--------------+
| bdate |    date      |
+-------+--------------+
0

3 Answers 3

3

MySQL generally requires dates in the YYYY-MM-DD format. It would be best to convert your slashed date to that.

This isn't a problem with escaping slashes as slashes aren't escaped in the first place. Only things that could wreck your string escaping would be changed. Obviously MYSQL is getting the correct string but is rejecting it because of formatting issues.

1

To enter a date into a database it needs to be YYYY-MM-DD, or in your case 1989-05-13. Just create a function that swaps the date around in the correct order.

function swap_date($date) {
  $newDate = explode("/", $date);
  return "{$newDate[2]}-{$newDate[0]}-{$newDate[1]}";
}

//-------------

$name = "John Doe";
$bdate = swap_date("05/13/1989"); // swap_date($_GET['date']) or whatever you use

$query = "INSERT INTO details (name, bdate) VALUES (:name, :bdate)";
$stmt = $this->handle->prepare($query);
$stmt->bindParam(':name',$name,PDO::PARAM_STR);
$stmt->bindParam(':bdate',$bdate,PDO::PARAM_STR);
$stmt->execute();
0

Before inserting anything into databse you better make sure that it soesnt contain characters like ", / , ' etc. so its a better idea to make a function that adds or stripes slashes and returns a riskfree string to insert into database.. so you what you can do is,

$bdate = 05/13/1989;
$bdate = mysql_prep($bdate);

function mysql_prep($value) {
    $magic_quotes_active = get_magic_quotes_gpc();
    $new_enough_php = function_exists("mysql_real_escape_string"); // PHP >= v4.3.0
    if ($new_enough_php) {
        if ($magic_quotes_active) {
            $value = stripslashes($value);
        }
        $value = mysql_real_escape_string($value);
    } else { // before PHP v4.3.0
        if (!$magic_quotes_active) {
            $value = addslashes($value);
        }
    }
    return $value;
}
2
  • 1
    Please learn from the OP how to use prepared statements. Nov 16, 2012 at 8:14
  • The OP is using PDO, not mysql_; why do you still care about anything lower than PHP 5.2, let alone PHP 4; don't use addslashes for anything; that's not even the problem.
    – deceze
    Nov 16, 2012 at 8:24

Not the answer you're looking for? Browse other questions tagged or ask your own question.