Values ​​stored in the database

I created two levels of registration on my website, at the first level of registration I can save my value in the database, but at the second level I can not store in the same table.

In the first register, I used the insert request, in the second register level in the same table, I used the update request.

If I gave a direct value to a string in which a condition that I can save, in case I give as user_id , I should not accept.

First register:

 public function register($uname,$umail,$upass,$mobile,$gender) { try { $new_password = password_hash($upass, PASSWORD_DEFAULT); $stmt = $this->conn->prepare("INSERT INTO users(user_name,user_email,user_pass,mobile,gender) VALUES(:uname, :umail, :upass, :mobile, :gender )"); $stmt->bindparam(":uname", $uname); $stmt->bindparam(":umail", $umail); $stmt->bindparam(":upass", $new_password); $stmt->bindparam(":mobile", $mobile); $stmt->bindparam(":gender", $gender); $stmt->execute(); return $stmt; } catch(PDOException $e) { echo $e->getMessage(); } } 

second register:

 public function register1($marital_status,$applicant_photo,$date_of_birth,$home_name,$birth_time) { try { $stmt = $this->conn->prepare("UPDATE users SET marital_status= :marital_status, applicant_photo=:applicant_photo, date_of_birth=:date_of_birth, home_name=:home_name, birth_time=:birth_time WHERE user_id =':user_id'"); $stmt->bindparam(":marital_status", $marital_status); $stmt->bindparam(":applicant_photo", $applicant_photo); $stmt->bindparam(":date_of_birth", $date_of_birth); $stmt->bindparam(":home_name", $home_name); $stmt->bindparam(":birth_time", $birth_time); $stmt->execute(); return $stmt; } catch(PDOException $e) { echo $e->getMessage(); } } 

Thank you in advance

+5
source share
3 answers

3 Code Errors

  • user_id is not inserted into the table
  • When changing the registration of the second level user_id =':user_id' to user_id =:user_id , that is, remove single quotes
  • Missing bindparam for user_id, i.e. $stmt->bindparam(":user_id", $user_id);

Try changing this code.

 public function register($uname,$umail,$upass,$mobile,$gender,$user_id) { try { $new_password = password_hash($upass, PASSWORD_DEFAULT); $stmt = $this->conn->prepare("INSERT INTO users(user_name,user_email,user_pass,mobile,gender,user_id) VALUES(:uname, :umail, :upass, :mobile, :gender, :user_id )"); $stmt->bindparam(":uname", $uname); $stmt->bindparam(":umail", $umail); $stmt->bindparam(":upass", $new_password); $stmt->bindparam(":mobile", $mobile); $stmt->bindparam(":gender", $gender); $stmt->bindparam(":user_id", $user_id); $stmt->execute(); return $stmt; } catch(PDOException $e) { echo $e->getMessage(); } } 

Second registration

 public function register1($marital_status,$applicant_photo,$date_of_birth,$home_name,$birth_time) { try { $stmt = $this->conn->prepare("UPDATE users SET marital_status= :marital_status, applicant_photo=:applicant_photo, date_of_birth=:date_of_birth, home_name=:home_name, birth_time=:birth_time WHERE user_id =:user_id"); $stmt->bindparam(":marital_status", $marital_status); $stmt->bindparam(":applicant_photo", $applicant_photo); $stmt->bindparam(":date_of_birth", $date_of_birth); $stmt->bindparam(":home_name", $home_name); $stmt->bindparam(":birth_time", $birth_time); $stmt->execute(); return $stmt; } catch(PDOException $e) { echo $e->getMessage(); } } 
+1
source

you need to pass $user_id argument as function and bind it like this

1st: Missing bind user_id

$stmt->bindparam(":user_id", $user_id);

2nd: Also remove single quotes

 user_id =':user_id' 

change to

  user_id =:user_id 
+1
source

Use this

 $stmt = $this->conn->prepare("UPDATE users SET marital_status= :marital_status, applicant_photo=:applicant_photo, date_of_birth=:date_of_birth, home_name=:home_name, birth_time=:birth_time WHERE user_id =:user_id"); $stmt->bindparam(":marital_status", $marital_status); $stmt->bindparam(":applicant_photo", $applicant_photo); $stmt->bindparam(":date_of_birth", $date_of_birth); $stmt->bindparam(":home_name", $home_name); $stmt->bindparam(":birth_time", $birth_time); $stmt->bindparam(":user_id", $user_id); $stmt->execute(); return $stmt; 

I think this will work for you.

0
source

Source: https://habr.com/ru/post/1269174/


All Articles