通过将用户ID与PDO一起使用来更新用户信息

时间:2019-03-08 10:20:53

标签: php mysql pdo

我正在尝试更新数据库中的现有用户信息,但是无论我尝试执行什么操作都不会执行。

代码:

public function updateUser()
{
    $this->userId = $_SESSION['user_id'];
    var_dump($this->userId);
    include "DBConnect.php";
    $updateUser = $conn->prepare("UPDATE `users` SET 
    `user_username`=:user_name,`user_password`=:user_password,
    `user_firstname`=:user_firstname,`user_middlename`=:user_middlename,
    `user_lastname`=:user_lastname,`user_country`=user_country,
    `user_city`=:user_city,`user_street`=:user_street,
    `user_housenumber`=:user_housenumber,`user_postalcode`=:user_postalcode
    ,`user_email`=:user_email,`user_phone`=:user_phone 
    WHERE `user_id` =:user_id");
    $updateUser->bindParam(':user_id', $_SESSION['user_id']);
    $updateUser->bindParam(':user_name', $this->userName);
    $updateUser->bindParam(':user_password', $this->userPassword);
    $updateUser->bindParam(':user_firstname', $this->userFirstName);
    $updateUser->bindParam(':user_middlename', $this->userMiddleName);
    $updateUser->bindParam(':user_lastname', $this->userLastName);
    $updateUser->bindParam(':user_country', $this->userCountry);
    $updateUser->bindParam(':user_city', $this->userCity);
    $updateUser->bindParam(':user_street', $this->userStreet);
    $updateUser->bindParam(':user_housenumber', $this->userHouseNumber);
    $updateUser->bindParam(':user_postalcode', $this->userPostalCode);
    $updateUser->bindParam(':user_email', $this->userEmail);
    $updateUser->bindParam(':user_phone', $this->userPhone);

    var_dump($this->userId);
    var_dump($this->userName);
    var_dump($this->userPassword);
    var_dump($this->userFirstName);
    var_dump($this->userMiddleName);
    var_dump($this->userLastName);
    var_dump($this->userCountry);
    var_dump($this->userCity);
    var_dump($this->userStreet);
    var_dump($this->userHouseNumber);
    var_dump($this->userPostalCode);
    var_dump($this->userEmail);
    var_dump($this->userPhone);

    $updateUser->execute();
}

完全错误:

string(1) "9" string(1) "9" string(4) "test" string(4) "test" string(4) "test" string(4) "test" string(4) "test" string(4) "test" string(4) "test" string(4) "test" string(2) "21" string(4) "test" string(4) "test" string(8) "12345678" 
Fatal error: Uncaught PDOException: SQLSTATE[HY093]: Invalid parameter number: number of bound variables does not match number of tokens in /Applications/MAMP/htdocs/webshop/classes/User.php:279 Stack trace: #0 /Applications/MAMP/htdocs/webshop/classes/User.php(279): PDOStatement->execute() #1 /Applications/MAMP/htdocs/webshop/adminpanel/userdashboard.php(23): User->updateUser() #2 {main} thrown in /Applications/MAMP/htdocs/webshop/classes/User.php on line 279

3 个答案:

答案 0 :(得分:1)

我找到了它,您只是错过了要添加到user_country的冒号,请尝试

{
    $this->userId = $_SESSION['user_id'];
    var_dump($this->userId);
    include "DBConnect.php";
    $updateUser = $conn->prepare("UPDATE `users` SET 
    `user_username`=:user_name,`user_password`=:user_password,
    `user_firstname`=:user_firstname,`user_middlename`=:user_middlename,
    `user_lastname`=:user_lastname,`user_country`=:user_country,
    `user_city`=:user_city,`user_street`=:user_street,
    `user_housenumber`=:user_housenumber,`user_postalcode`=:user_postalcode
    ,`user_email`=:user_email,`user_phone`=:user_phone 
    WHERE `user_id` =:user_id");
    $updateUser->bindParam(':user_id', $_SESSION['user_id']);
    $updateUser->bindParam(':user_name', $this->userName);
    $updateUser->bindParam(':user_password', $this->userPassword);
    $updateUser->bindParam(':user_firstname', $this->userFirstName);
    $updateUser->bindParam(':user_middlename', $this->userMiddleName);
    $updateUser->bindParam(':user_lastname', $this->userLastName);
    $updateUser->bindParam(':user_country', $this->userCountry);
    $updateUser->bindParam(':user_city', $this->userCity);
    $updateUser->bindParam(':user_street', $this->userStreet);
    $updateUser->bindParam(':user_housenumber', $this->userHouseNumber);
    $updateUser->bindParam(':user_postalcode', $this->userPostalCode);
    $updateUser->bindParam(':user_email', $this->userEmail);
    $updateUser->bindParam(':user_phone', $this->userPhone);

    var_dump($this->userId);
    var_dump($this->userName);
    var_dump($this->userPassword);
    var_dump($this->userFirstName);
    var_dump($this->userMiddleName);
    var_dump($this->userLastName);
    var_dump($this->userCountry);
    var_dump($this->userCity);
    var_dump($this->userStreet);
    var_dump($this->userHouseNumber);
    var_dump($this->userPostalCode);
    var_dump($this->userEmail);
    var_dump($this->userPhone);

    $updateUser->execute();
}

答案 1 :(得分:1)

查找行:

user_lastname`=:user_lastname,`user_country`=user_country,

替换为:

user_lastname`=:user_lastname,`user_country`=:user_country,

user_country之前缺少的分号不将其绑定到SQL查询。

答案 2 :(得分:0)

您的问题是user_name和user_country的输入错误,会话可能尚未初始化。最后,您应该检查所有变量是否包含拼写,错字,冒号,逗号错误等。

以下是我从您的代码中得到的发现

首先拥有

`user_username`=:user_name,

代替

`user_name`=:user_name,

第二次

您有(这里没有冒号)

 `user_country`=user_country,

代替

 `user_country`=:user_country,

第三名   user_id 是基于会话的,我看不到代码中初始化会话的位置

session_start();

尝试使用此代码

public function updateUser()
{
session_start();
    $this->userId = $_SESSION['user_id'];
    var_dump($this->userId);
    include "DBConnect.php";

    $updateUser = $conn->prepare("UPDATE `users` SET 
    `user_name`=:user_name,
    `user_password`=:user_password,
    `user_firstname`=:user_firstname,
    `user_middlename`=:user_middlename,
    `user_lastname`=:user_lastname,
    `user_country`=:user_country,
    `user_city`=:user_city,
    `user_street`=:user_street,
    `user_housenumber`=:user_housenumber,
    `user_postalcode`=:user_postalcode
    ,`user_email`=:user_email,
    `user_phone`=:user_phone WHERE `user_id` =:user_id");

    $updateUser->bindParam(':user_id', $_SESSION['user_id']);
    $updateUser->bindParam(':user_name', $this->userName);
    $updateUser->bindParam(':user_password', $this->userPassword);
    $updateUser->bindParam(':user_firstname', $this->userFirstName);
    $updateUser->bindParam(':user_middlename', $this->userMiddleName);
    $updateUser->bindParam(':user_lastname', $this->userLastName);
    $updateUser->bindParam(':user_country', $this->userCountry);
    $updateUser->bindParam(':user_city', $this->userCity);
    $updateUser->bindParam(':user_street', $this->userStreet);
    $updateUser->bindParam(':user_housenumber', $this->userHouseNumber);
    $updateUser->bindParam(':user_postalcode', $this->userPostalCode);
    $updateUser->bindParam(':user_email', $this->userEmail);
    $updateUser->bindParam(':user_phone', $this->userPhone);

    var_dump($this->userId);
    var_dump($this->userName);
    var_dump($this->userPassword);
    var_dump($this->userFirstName);
    var_dump($this->userMiddleName);
    var_dump($this->userLastName);
    var_dump($this->userCountry);
    var_dump($this->userCity);
    var_dump($this->userStreet);
    var_dump($this->userHouseNumber);
    var_dump($this->userPostalCode);
    var_dump($this->userEmail);
    var_dump($this->userPhone);

    $updateUser->execute();
}

或者。

而不是使用所有这些绑定绑定绑定……。 您可以只使用数组方法。让我知道是否还有更多问题

public function updateUser()
{

//initialized session if you have not done so
session_start();
    $this->userId = $_SESSION['user_id'];
    var_dump($this->userId);
    include "DBConnect.php";


$updateUser = $conn->prepare("UPDATE `users` SET 
    `user_name`=:user_name,
    `user_password`=:user_password,
    `user_firstname`=:user_firstname,
    `user_middlename`=:user_middlename,
    `user_lastname`=:user_lastname,
    `user_country`=:user_country,
    `user_city`=:user_city,
    `user_street`=:user_street,
    `user_housenumber`=:user_housenumber,
    `user_postalcode`=:user_postalcode
    ,`user_email`=:user_email,
    `user_phone`=:user_phone WHERE `user_id` =:user_id");




$updateUser->execute(array(
 ':user_id' =>  $_SESSION['user_id'],
 ':user_name' => $this->userName,
 ':user_password'=> $this->userPassword,
 ':user_firstname'=> $this->userFirstName, 
 ':user_middlename'=> $this->userMiddleName, 
 ':user_lastname'=> $this->userLastName, 
 ':user_country'=> $this->userCountry, 
 ':user_city'=> $this->userCity, 
 ':user_street'=> $this->userStreet, 
 ':user_housenumber'=> $this->userHouseNumber, 
 ':user_postalcode'=> $this->userPostalCode,  
 ':user_email'=> $this->userEmail, 
 ':user_phone'=> $this->userPhone,

));


}