繁体   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