簡體   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;
      }
   }

我收到此錯誤: "Uncaught PDOException: SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'user_id' cannot be null

錯誤代碼

您的代碼中有錯誤

我不認為你的意思是從$data數組中訪問值
$this->db->bind(':user_id', $data[$user_id]);

您可能想直接使用 $user_id 值
$this->db->bind(':user_id', $user_id);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM