簡體   English   中英

如何在面向對象的php內部函數中使用where條件

[英]how to use where condition in object oriented php inside function

當我想像這樣更新適當的 user_id 時,我的函數無法在我的函數中使用 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);

} 

?>

由於您有五個占位符 ( ? ) = 您應該將 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();
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM