【问题标题】:Using AES_ENCRYPT in Prepared Statement with Bind Variables在带有绑定变量的预处理语句中使用 AES_ENCRYPT
【发布时间】:2016-07-10 01:37:59
【问题描述】:

想要加密特定数据变量但不断收到“PHP 致命错误:调用未定义函数 AES_ENCRYPT()...”

研究提示我使用的是 PHP 而不是 MySQL?

$key="xyz";

$stmt = mysqli_prepare($mysqli, "INSERT INTO details (FirstName, LastName, EncryptThis) VALUES (?,?,?)");

if ($stmt === false) {
        trigger_error('Statement failed! ' . htmlspecialchars(mysqli_error($mysqli)), E_USER_ERROR);
    }                      

$bind = mysqli_stmt_bind_param($stmt, "sss", $FirstName, $LastName,  AES_ENCRYPT('$EncryptThis','$key'));

        if ($bind === false) {
        trigger_error('Bind param failed!', E_USER_ERROR);
    }                  

$exec = mysqli_stmt_execute($stmt);

我在数据库中使用 varbinary...

尝试过各种用途

AES_ENCRYPT('$EncryptThis','$key')

EG

AES_ENCRYPT($EncryptThis,$key) 

等等等等

【问题讨论】:

标签: php mysqli aes


【解决方案1】:

MySQL 期望 values 作为绑定参数传递。不是函数名称或其他 SQL 表达式。只是

如果你想调用 MySQL AES_ENCRYPT 函数,它需要作为 SQL 文本的一部分出现(作为 SQL 语句准备的字符串)。函数名不能作为绑定参数的一部分传递。

像这样:

 "INSERT ... VALUES ( ? , ? , AES_ENCRYPT( ? , ? ) )" 

 mysqli_stmt_bind_param($stmt, "ssss", $FirstName, $LastName, $EncryptThis, $key);

【讨论】:

  • 已更新 - 没有 PHP 致命错误,除了现在 DB 列中显示的空白值。error_log 中没有错误。我没有在数据库中为 $key 添加额外的列? INSERT 是否应该包含密钥?
  • 不-一切都好。我对引号的使用不当。谢谢@spencer7593
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-06-07
  • 1970-01-01
  • 1970-01-01
  • 2020-06-14
  • 1970-01-01
  • 2014-04-05
  • 2015-07-14
相关资源
最近更新 更多