MySQL INSERT from a SELECT with PDO

前端 未结 2 1991
再見小時候
再見小時候 2020-12-12 06:16

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          


        
相关标签:
2条回答
  • 2020-12-12 06:41

    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.

    0 讨论(0)
  • 2020-12-12 07:01

    Look at the PHP Documentation for PDO::bindParam. One user there suggested:

    ...you must use each parameter once and only once...

    So, you used username and aeskey twice. Do it this way:

    $sql_enc = '
        INSERT INTO sessionid (enc_id, enc_pass, enc_date) 
            (SELECT AES_ENCRYPT(:username1, :aeskey1), AES_ENCRYPT(:pwd, :aeskey2), DATE_ADD(NOW(), INTERVAL 15 SECOND) FROM users WHERE username = :username2)
    ';
    $res_enc = $pdo->prepare($sql_enc);
    $res_enc->bindParam(':aeskey1', $aeskey);
    $res_enc->bindParam(':aeskey2', $aeskey);
    $res_enc->bindParam(':username1', $username);
    $res_enc->bindParam(':username2', $username);
    $res_enc->bindParam(':pwd', $username);
    $res_enc->execute();
    $res_enc = null;
    
    0 讨论(0)
提交回复
热议问题