tdrsam tdrsam - 5 months ago 23
SQL Question

encrypting multiple variables with AES_ENCRYPT in MySql

What's the correct syntax for a SQL statement like this:

$insert = ("INSERT INTO users (email, password) VALUES (1,AES_ENCRYPT('$email',UNHEX('F3229A0B371ED2D9441B830D21A390C3'))),(1,AES_ENCRYPT('$password',UNHEX('F3229A0B371ED2D9441B830D21A390C3')));");


I'm trying to add two variables to the same row of a db, both of them encrypted. The
$email
and
$password
variables are inputs from a web form, but you probably guessed that. I get an error message saying there's an unexpected comma which must be the one between the two values.

Answer

You have wrong SQL,

Try this:

INSERT INTO users (email, password) VALUES (
  AES_ENCRYPT('$email',UNHEX('F3229A0B371ED2D9441B830D21A390C3')),
  AES_ENCRYPT('$password',UNHEX('F3229A0B371ED2D9441B830D21A390C3'))
)