【问题标题】:Values stored in database存储在数据库中的值
【发布时间】:2017-06-24 06:04:25
【问题描述】:

我在我的网站中创建了两级注册,在第一级注册中我可以将我的值存储在数据库中,但在第二级我不能存储在同一个表中。

在第一个寄存器中我使用了插入查询,在同一张表的第二级寄存器中,我使用了更新查询。

如果我在可以存储的条件下给出直接行值,以防我给出user_id 我不应该接受。

第一次注册:

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();
        }               
    }

第二个寄存器:

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();
        }               
    }

提前致谢

【问题讨论】:

  • 缺少绑定user_id stmt->bindparam(":user_id", $user_id);并删除单引号 user_id =':user_id'
  • 你把user_id绑定到register1的什么地方了?
  • 基本上就是 JYoThl 所说的。一般来说,请查看$this->conn->error 了解更多信息。 try...catch 不会捕获错误,因为 mysqli 默认情况下不会抛出错误。
  • 我不知道“为什么这个问题得到了 5 票”

标签: php mysql


【解决方案1】:

您的代码中有 3 个错误

  1. user_id 未插入表中
  2. 在二级注册时将user_id =':user_id' 更改为user_id =:user_id,即删除单引号
  3. 缺少bindparam 到user_id,即$stmt->bindparam(":user_id", $user_id);

尝试更改此代码

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();
    }               
}

第二次注册

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();
    }               
}

【讨论】:

    【解决方案2】:

    您需要像这样将$user_id 作为参数传递给functionbind

    第一个:缺少binduser_id

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

    2nd : 也去掉单引号

    user_id =':user_id' 
    

    改成

      user_id =:user_id
    

    【讨论】:

      【解决方案3】:

      使用这个

      $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;  
      

      我觉得它对你有用。

      【讨论】:

      • 问题是您没有在查询中绑定 user_id 值。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-03-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多