I am trying to insert a user entered date, which comes as a POST variable, in a DATE field and continuously getting ORA-01847. Here is what I have so far,
$name = strtoupper(trim($_POST['name']));
$dob = date('d-M-y', strtotime($_POST['dob']));
$query = "INSERT INTO TEST_TABLE (TABID, TABNAME, DOB, CREATEDBY) VALUES (SEQ_TAB_TABID.NEXTVAL, :1, TO_DATE(:2, 'DD-MM-YY'), :3)";
$ins = array($name, $dob, $_SESSION['usrid']);
$conn = oci_connect($username, $password, $constr);
$stid = oci_parse($conn, $query);
$count = 1;
foreach ($ins as $i) {
oci_bind_by_name($stid, sprintf(':%d', $count), $i);
$count++;
}
$exec = oci_execute($stid);
The user input that I am getting is in the format 08/29/2015 ($_POST['dob'])
I keep getting ORA-01847: day of month must be between 1 and last day of month
I have tried printing the final dob and it prints out 29-AUG-2015. When I use the same query and insert using SQL Developer it works perfectly.
I have even tried the "non-bind" version of the query as follows:
$query = sprintf("INSERT INTO TEST_TABLE (TABID, TABNAME, DOB, CREATEDBY) VALUES (SEQ_TAB_TABID.NEXTVAL, %s, TO_DATE(%s, 'DD-MM-YY'), %d)", escapeshellarg($name), escapeshellarg($dob), $_SESSION['usrid'])
In which case I get ORA-01858: a non-numeric character was found where a numeric was expected
and again when I run that query with SQL developer it works.
I know I am missing a minor thing here but this is my first time working with PHP and Oracle and any help would be greatly appreciated.
Try parsing the date with the right format:
INSERT INTO TEST_TABLE(TABID, TABNAME, DOB, CREATEDBY)
VALUES (SEQ_TAB_TABID.NEXTVAL, :1, TO_DATE(:2, 'MM/DD/YYYY'), :3)