Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Values stored in database

Tags:

php

mysql

I have created two level of registration in my website, in first level of registration I can store my value in database but in second level I can't store in the same table.

In first register I have used an insert query, in second level of register in same table, I have used an update query.

If I given direct row value in where condition I am able to store, in case if 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();
        }               
    }

Thanks in advance

like image 806
PARAM NAGARAJAN Avatar asked Oct 17 '22 10:10

PARAM NAGARAJAN


1 Answers

3 Errors in your code

  1. user_id is not inserted to the table
  2. On second level registration change user_id =':user_id' into user_id =:user_id,i.e remove single quotes
  3. Missing bindparam to user_id,i.e $stmt->bindparam(":user_id", $user_id);

Try to change 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();
    }               
}
like image 189
perumal N Avatar answered Nov 15 '22 04:11

perumal N