dongwen9947 2016-04-13 14:16
浏览 88
已采纳

在PHP中插入时MySqli错误,但相同的查询字符串在终端中执行正常

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?

  • 写回答

1条回答 默认 最新

  • dongzhang1987 2016-04-14 06:52
    关注

    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).

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 TLS1.2协议通信解密
  • ¥40 图书信息管理系统程序编写
  • ¥20 Qcustomplot缩小曲线形状问题
  • ¥15 企业资源规划ERP沙盘模拟
  • ¥15 树莓派控制机械臂传输命令报错,显示摄像头不存在
  • ¥15 前端echarts坐标轴问题
  • ¥15 ad5933的I2C
  • ¥15 请问RTX4060的笔记本电脑可以训练yolov5模型吗?
  • ¥15 数学建模求思路及代码
  • ¥50 silvaco GaN HEMT有栅极场板的击穿电压仿真问题