简体   繁体   中英

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

Error code

You got error in your code

I dont think you mean to access value from the $data array
$this->db->bind(':user_id', $data[$user_id]);

You probably want to use $user_id value directly
$this->db->bind(':user_id', $user_id);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM