AES_ENCRYPT MySQL更新查询

时间:2018-02-27 16:56:42

标签: php mysql encryption aes

我希望你能提供帮助。我试图在我的更新查询中使用AES_ENCRYPT,但我无法让它工作。

我正在尝试加密first_name变量,但是当我运行查询时,它拒绝更新该字段。当我从更新查询中删除AES_ENCRYPT方法时,它可以正常工作。

我目前的代码如下:

<?php
include_once("db_connect.php");


function test_input($data) {
  $data = trim($data);
  $data = stripslashes($data);
  $data = htmlspecialchars($data);
  return $data;
}


if ($_SERVER["REQUEST_METHOD"] == "POST") {
  $name=test_input($_POST['name1']); // Fetching Values from URL.
  $email=test_input($_POST['email1']);
  $gender=test_input($_POST['gender1']);
  $mobile=test_input($_POST['mobile1']);
  $password= test_input(sha1($_POST['password1'])); // Password Encryption, If you like you can also leave sha1.

echo "pranav";    // these dont work
echo $name+$email+$gender; // this also doesnt work


  // Check if e-mail address syntax is valid or not
  $email = filter_var($email, FILTER_SANITIZE_EMAIL); // Sanitizing email(Remove unexpected symbol like <,>,?,#,!, etc.)
  if (!filter_var($email, FILTER_VALIDATE_EMAIL)){
    echo $email;
  } 
  else{
    $result = mysql_query("SELECT * FROM users WHERE email='$email'");
    $data = mysql_num_rows($result);
    if(($data)==0){
      $query = mysql_query("insert into users(name, email, password, gender, mobile) values ('$name', '$email', '$password', '$gender', '$mobile')"); // Insert query
      if($query){
        echo "registered";
      }
      else
      {
        echo "Error....!!";
      }
    }
    else
    {
      echo "1";
    }
  }
}
?>  

1 个答案:

答案 0 :(得分:0)

我设法使用以下版本修复了我的AES_ENCRYPT更新查询问题:

$encrypt_key = '4a7s3n3j93n98lk';

$statement = $db->prepare("UPDATE cases SET 
first_name = AES_ENCRYPT(:first_name, '$encrypt_key'), 
last_name = AES_ENCRYPT(:last_name, '$encrypt_key'), 
WHERE cases_id = :id");

$statement->bindParam(':id', $id, PDO::PARAM_INT);
$statement->bindParam(':first_name', $first_name, PDO::PARAM_STR);
$statement->bindParam(':last_name', $last_name, PDO::PARAM_STR);
$statement->execute();

$db = null;