問題描述
我在使用 PHP PDO 從 SELECT 查詢中插入 INSERT 時有一個奇怪的行為.直接在 MySQL 中測試查詢效果很好,我插入了我的行:
I have a strange behaviour using PHP PDO for a INSERT from a SELECT query. Testing the query directly in MySQL it works well, I get my row inserted :
INSERT INTO sessionid (enc_id, enc_pass, enc_date)
SELECT AES_ENCRYPT(username, 'aeskey'), AES_ENCRYPT(pwd, 'aeskey'),
DATE_ADD(NOW(), INTERVAL 15 SECOND) FROM users WHERE username = 'a_user_name';
但是使用 PDO,我為每個用戶一次插入一行(279 行)......這是 PHP :
But using PDO, I have one row per user inserted at once (279 rows) .... Here is the PHP :
$sql_enc = '
INSERT INTO sessionid (enc_id, enc_pass, enc_date)
(SELECT AES_ENCRYPT(username, :aeskey), AES_ENCRYPT(pwd, :aeskey), DATE_ADD(NOW(), INTERVAL 15 SECOND) FROM users WHERE username = :username)
';
$res_enc = $pdo->prepare($sql_enc);
$res_enc->bindParam(':aeskey', $aeskey);
$res_enc->bindParam(':username', $username);
$res_enc->bindParam(':pwd', $username);
$res_enc->execute();
$res_enc = null;
我錯過了什么?我幾乎可以肯定它什么都不是,但不能讓它插入那一行.
What am I missing? I'm almost sure it's nothing but can't make it insert that single row.
謝謝.
法比恩.
推薦答案
這不是可能的問題,而是您在代碼的密碼字段中輸入了用戶名.在您的查詢中,您在那里插入 aeskey.這是我能發現的唯一區別.
Not that it is the probable problem, but you put a username in the password field in your code. In your query you insert the aeskey there. It is the only difference I can spot.
這篇關于MySQL 從帶有 PDO 的 SELECT 插入的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!