简体   繁体   English

插入PHP语句不工作

[英]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. 好吧,如果有任何术语关闭...。所以我想在我的PHP代码中使用此插入语句。 The statement works fine when i put it in using isql or sqlplus. 当我使用isql或sqlplus放置该语句时,该语句运行良好。 But when i run the query in PHP it doesn't insert anything into the table. 但是,当我在PHP中运行查询时,它不会在表中插入任何内容。 Doesn't return any errors. 不返回任何错误。 I am using PHP's PDO extension. 我正在使用PHP的PDO扩展。 http://us2.php.net/manual/en/pdo.prepare.php http://us2.php.net/manual/zh/pdo.prepare.php

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

My php code looks something like this and i am using CodeIgniter 我的PHP代码看起来像这样,我正在使用CodeIgniter

$dbh = self::$CI->db->conn_id; $ 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)"; $ sql =“插入到USER_KEY(CREATED_DATE,COMMENT,EXPIRATION_DATE,USER_KEY_ID)值(SYSTIMESTAMP,?,TO_TIMESTAMP(?,'YYYY-MM-DD'),key_sequence.NEXTVAL)”;

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

$stmt->execute(array(NULL,'2012-02-02')); $ 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. 对于数据库中的不同表,我的代码中有相似的PHP插入语句,这些语句有效。 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? 但是,那些表不使用任何时间戳,所以我想知道我对时间戳的使用是否会弄乱我在PHP中使用的insert语句? 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. 从这个错误中,我得知我输入的日期与SQL语句中TO_TIMESTAMP函数中的格式不匹配。
http://www.techonthenet.com/oracle/errors/ora01830.php http://www.techonthenet.com/oracle/errors/ora01830.php

I didn't know this but in oracle DD represents [1-31] NOT [01-31]. 我不知道这一点,但是在oracle中DD表示[1-31]而不是[01-31]。 Information found in Oracle Documentation Oracle文档中的信息

So i will try changing the format I'm using for the dates I'm binding to the SQL statement from Ymd to Ymj . 因此,我会试着改变我使用我绑定到SQL语句从日期的格式YmdYmj Since j represents [1-31] days in PHP DateTime objects. 由于j在PHP DateTime对象中代表[1-31]天。

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? 我有兴趣知道为什么更改我对SQL语句进行绑定的方式会引发错误吗? since I'm not quite understanding that. 因为我不太了解。

You need to use some sort of error reporting in PHP. 您需要在PHP中使用某种错误报告。 Your error is on the first line: 您的错误在第一行:

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

That should be 那应该是

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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