gpt4 book ai didi

php - 存储在数据库中的值

转载 作者:可可西里 更新时间:2023-11-01 07:07:47 26 4
gpt4 key购买 nike

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

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

如果我在我能够存储的条件下给出直接行值,以防万一我给出 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();
}
}

提前致谢

最佳答案

代码中有 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();
}
}

关于php - 存储在数据库中的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44733360/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com