简体   繁体   English

我有这个函数来生成一个随机数,我正在尝试使用 PDO 将此随机数插入到 mysql 数据库中,但是我遇到了错误

[英]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

Error code错误代码

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.

相关问题 我无法使用 PDO 插入数据库 - i'm unable to insert database using PDO 我正在尝试使用PDO将数据插入数据库,但是每当我尝试查询失败时,我就会在学习PDO - I'm trying to insert data into database using PDO but whenever i try the query fail, i'm learning PDO 我正在尝试使用 Faker 通过 Laravel 生成随机图像 - I'm trying to use Faker to generate random images with Laravel 我无法使用PHP插入MySQL(无错误显示) - I'm unable to insert into MySQL using PHP (no errors are being displayed) 我正在尝试将数据插入 mysql 数据库,但我不断收到:警告:尝试访问 null 类型值的数组偏移量 - I'm trying to insert data into mysql database, but i kept getting: Warning: Trying to access array offset on value of type null 我正在尝试自动增加发票编号 - i'm trying to increase the invoice number automatically 我正在尝试将Javascript函数值添加到Mysql数据库中 - I'm trying to get a Javascript Function Value into a Mysql Database 生成一个随机的10位数字并插入到mysql中? - Generate a random 10 digit number and insert into mysql? 我正在尝试使一个函数(按钮)将图像上传到CodeIgniter(PHP)中的数据库。 但是我出错了 - I'm trying to make a function (button) to upload images to my database in CodeIgniter (PHP). But I'm getting an error 我正在尝试更新我的MySQL数据库 - I'm trying to update my mysql database
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM