简体   繁体   中英

Insert statement doesnt work in PHP

Alright sorry if any of terminology is off....so I'm trying to use this insert statement in my PHP code. The statement works fine when i put it in using isql or sqlplus. But when i run the query in PHP it doesn't insert anything into the table. Doesn't return any errors. I am using PHP's PDO extension. http://us2.php.net/manual/en/pdo.prepare.php

INSERT INTO USER_KEY (CREATED_DATE, COMMENT, EXPIRATION_DATE, USER_KEY_ID)
VALUES (SYSTIMESTAMP, NULL,
TO_TIMESTAMP('2012-02-02', 'YYYY-MM-DD'), key_sequence.NEXTVAL);

My php code looks something like this and i am using CodeIgniter

$dbh = self::$CI->db->conn_id;

$sql = "INSERT INTO USER_KEY (CREATED_DATE, COMMENT, EXPIRATION_DATE, USER_KEY_ID) VALUES (SYSTIMESTAMP, ?, TO_TIMESTAMP(?, 'YYYY-MM-DD'), key_sequence.NEXTVAL)";

$stmt = $dbh->prepare($sql);

$stmt->execute(array(NULL,'2012-02-02'));

I have similar PHP insert statements in my code for different tables in my database and those statements work. But, those tables don't use any Timestamps, so im wondering if somehow my use of timestamps is whats messing up my insert statement being used in PHP? Also any explanation as to why I don't see any errors when my statement isn't executed?

UPDATE/SOLUTION

So I changed the way I was doing the bindings to

 $sql = INSERT INTO USER_KEY <br>
                    (CREATED_DATE, <br>
                     COMMENT, <br>
                     EXPIRATION_DATE, <br>
                     USER_KEY_ID)  <br>
         VALUES      (SYSTIMESTAMP, <br>
                     :comment,  <br>
                     TO_TIMESTAMP(:date, 'YYYY-MM-DD'), <br>
                     key_sequence.NEXTVAL); <br>

 $stmt = $dbh->prepare($sql); <br>
 $stmt->bindValue(':comment', $this->comment); <br>
 $stmt->bindValue(':date', $this->creation_date); <br>
 $execute(); <br>

 $var_dump($stmt->errorInfo());

Doing the bindings this way returned an error

ORA-01830: date format picture ends before converting entire input string

From this error i gathered that the date i was entering did not match the format i had in the TO_TIMESTAMP function in my SQL statement.
http://www.techonthenet.com/oracle/errors/ora01830.php

I didn't know this but in oracle DD represents [1-31] NOT [01-31]. Information found in Oracle Documentation

So i will try changing the format I'm using for the dates I'm binding to the SQL statement from Ymd to Ymj . Since j represents [1-31] days in PHP DateTime objects.

Please correct me if I'm wrong on any of that. I am Interested in knowing why changing the way i was doing the bindings for the SQL statement threw an error? since I'm not quite understanding that.

You need to use some sort of error reporting in PHP. Your error is on the first line:

$dbh = $self::$CI->db->conn_id;

That should be

$dbh = self::$CI->db->conn_id;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM