[英]I have this function to generate a random number, and I'm trying to insert this random number into mysql database using PDO, but I'm getting errors
public function register($data) {
$user_id = random_num(10);
$this->db->query('INSERT INTO users (user_id, username, email, password) VALUES (:user_id, :username, :email, :password)');
$this->db->bind(':user_id', $data[$user_id]);
$this->db->bind(':username', $data['username']);
$this->db->bind(':email', $data['email']);
$this->db->bind(':password', $data['password']);
if ($this->db->execute()) {
return true;
} else {
return false;
}
}
I got this error: "Uncaught PDOException: SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'user_id' cannot be null
我收到此错误:
"Uncaught PDOException: SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'user_id' cannot be null
You got error in your code您的代码中有错误
I dont think you mean to access value from the $data
array我不认为你的意思是从
$data
数组中访问值$this->db->bind(':user_id', $data[$user_id]);
You probably want to use $user_id value directly您可能想直接使用 $user_id 值
$this->db->bind(':user_id', $user_id);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.