0

I have some form data that need to be written into a database, after it's confirmed. I form a query string and pass it to mysqli, but get error that my primary key can't be null. It's not null, and query string is executed fine in mysql terminal.

$query_string = "insert into podnosilac (ime, prezime, jmbg) 
        values (
        aes_encrypt('". $mysqli->real_escape_string($_SESSION["ime"])."', @key_str, @init_vector), 
        aes_encrypt('". $mysqli->real_escape_string($_SESSION["prezime"])."', @key_str, @init_vector),
        aes_encrypt('". $mysqli->real_escape_string($_SESSION["jmbg"])."', @key_str, @init_vector)
)";
if (!$mysqli->query($query_string)) {
        echo $mysqli->error;
}

This is the value of $query_string when I echo it just above mysqli->query line:

insert into podnosilac (ime, prezime, jmbg)
    values ( aes_encrypt('ivana', @key_str, @init_vector), aes_encrypt('corovic', @key_str, @init_vector), aes_encrypt('12345678910', @key_str, @init_vector) )

As a matter of fact, when I try this line:

$mysqli->query("insert into podnosilac (ime, prezime, jmbg)
    values ( aes_encrypt('ivana', @key_str, @init_vector), aes_encrypt('corovic', @key_str, @init_vector), aes_encrypt('12345678910', @key_str, @init_vector) )");

I still get "Column 'jmbg' cannot be null".

It's something to do with aes_encrypt. @key_str and @init_vector are defined on database, maybe this is not the way to pass them?

Why could this be happening?

10
  • 1
    Where are defined @key_str and @init_vector? Commented Apr 13, 2016 at 14:24
  • Does your primary key auto increment? Commented Apr 13, 2016 at 14:24
  • what is the error you get ? Commented Apr 13, 2016 at 14:24
  • @EduardoGalván, key_str and init_vector are defined on database, from mysql terminal. Commented Apr 13, 2016 at 14:51
  • 1
    I have a hunch that the SQL variables @key_str and @init_vector are being set up in the SQL command line environment but not the PHP environment. What do you get if you select @key_str, @init_vector from PHP? (Just NULL or not NULL - you don't have to give anything away). Where and how are they set? Commented Apr 14, 2016 at 6:51

1 Answer 1

1

I can think two things:

1) You are not connecting to the correct database

2) You are not setting these mysql variables when you create the connection to database. Do something like $mysqli->query('SET @yourvariable := whatever'); right after your mysqli_connect because these variables are session-specific. This could explaing the query working on your terminal (where you actually setting these variables) and not working in your script (where you don't).

Sign up to request clarification or add additional context in comments.

1 Comment

@Mike, key_str and init_vector were null. Those are user defined variables and they are available in mysql terminal for the session only. I have defined $key_str and $init_vector to use in php.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.