如何在面向对象的php里面使用where条件的函数

时间:2016-09-25 18:03:17

标签: php

当我想更新相应的user_id时,我的函数无法在我的函数中使用where where:where user_id= ?

查看我的功能:

public function createProfile($profile_picture, $username,$businessname,    $town) {
    $stmt = $this->conn->prepare("UPDATE profile_information set profile_picture = ?, username= ?, businessname= ?, town= ? where user_id= ?");

    $stmt->bind_param("ssss",$profile_picture, $username,$businessname, $town);

    $stmt->execute();
}

我调用createProfile函数让我们更新

      <?php

include './DbHandler.php';
$db = new DbHandler();


$response = array();

  if (  (   isset($_POST['profile_picture']) && isset($_POST['username']) &&     isset($_POST['businessname']) && isset($_POST['town'])  )!= '') {



 $profile_picture = $_POST['profile_picture'];
 $username = $_POST['username'];
 $businessname = $_POST['businessname'];
    $town = $_POST['town'];


   $res = $db->createProfile($profile_picture, $username,$businessname);

} 

?>

1 个答案:

答案 0 :(得分:0)

由于您有五个占位符(?)=您应该将5个参数传递给bind_param。简单的方法是将$user_id作为参数传递:

public function createProfile($profile_picture, $username,$businessname, $town, $user_id) {  // user_id is a five parameter here
    $stmt = $this->conn->prepare("UPDATE profile_information set profile_picture = ?, username= ?, businessname= ?, town= ? where user_id= ?");
    // I suppose user_id has is `integer` type
    $stmt->bind_param("ssssi",$profile_picture, $username, $businessname, $town, $user_id);

    $stmt->execute();
}

由于我们不知道你班级里有什么 - 也许用户ID在类属性中的某个地方(在我的例子中名为user_id),然后你可以像这样使用它:

public function createProfile($profile_picture, $username,$businessname, $town) {  // NO user_id passed
    $stmt = $this->conn->prepare("UPDATE profile_information set profile_picture = ?, username= ?, businessname= ?, town= ? where user_id= ?");
    // I suppose user_id has is `integer` type
    $stmt->bind_param("ssssi",$profile_picture, $username, $businessname, $town, $this->user_id);

    $stmt->execute();
}