Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/244.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在类内使用面向对象的PHP在一个查询中从一个表中选择和更新_Php_Mysqli - Fatal编程技术网

如何在类内使用面向对象的PHP在一个查询中从一个表中选择和更新

如何在类内使用面向对象的PHP在一个查询中从一个表中选择和更新,php,mysqli,Php,Mysqli,我需要在选择后选择表行中的数据,并在当时对其进行更新,但不起作用 update.php调用上述函数 此html界面承载表单 不能在一个语句中执行两个操作。在updateProfile方法中,需要执行SELECT角色,将集合存储在变量中,然后执行UPDATE角色,如下所示: public function updateProfile(...) { $stmt = $this->conn->prepare("SELECT profile_information,username

我需要在选择后选择表行中的数据,并在当时对其进行更新,但不起作用

update.php调用上述函数

此html界面承载表单


不能在一个语句中执行两个操作。在updateProfile方法中,需要执行SELECT角色,将集合存储在变量中,然后执行UPDATE角色,如下所示:

public function updateProfile(...)
{
     $stmt = $this->conn->prepare("SELECT profile_information,username,businessname,town from profile_information WHERE phone = ? AND profile_id= ?");
     $stmt->bindParam(1, $phone);
     $stmt->bindParam(2, $profile_id);
     $stmt->execute();
     $partial = $stmt->fecthAll();

     for($i = 0, $i < count($partial), $i++)
     {
         $stmtUpdate = $this->conn->prepare("UPDATE profile_information set profile_picture = ?, username= ?, businessname= ?, town= ? where profile_id= ?");
         $stmtUpdate->bindParam(1, $profilePicture);
         $stmtUpdate->bindParam(2, $userName);
         $stmtUpdate->bindParam(3, $businessName);
         $stmtUpdate->bindParam(4, $town);
         $stmtUpdate->bindParam(5, $profileId);
         $stmtUpdate->execute();
     }
}

请检查您的代码格式,并使用来指导您的开发。

一些合理的代码缩进将是一个好主意。它帮助我们阅读代码,更重要的是,它将帮助您为自己的利益调试代码。您可能会在几周/几个月内被要求修改此代码,最终您会感谢我。WHERE phone=profile\u id=?????????您正在同一语句句柄上准备SELECT和UPDATE查询????PHP很聪明,但不是那么聪明!我猜你是在浪费时间,不知道为什么你认为你需要在更新行之前选择行
                             include './DbHandler.php';
       $db = new DbHandler();
     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'];

 $response = $db->updateProfile( $profile_picture, $username, $businessname,     $town);
                } 
             ?>
                        <!doctype html>
                       <html lang="en"
                           <body>
                       <form name="uploadForm" method="post"  action="update_profile.php" >    
                       <label>profilep</label>  <input type="text"  name="profile_picture" > <br><br>
                        <label>Uname</label>  <input type="text" name="username" > <br> <br>
                        <label>BName</label>  <input type="text" name="businessname" >             <br><br>
                       <label>Town </label>   <input type="text" name="town" > <br><br>
                       <input type="submit" name="submit" value="Submit" /> 
                     </form>  
                      </body>
                     </html>
public function updateProfile(...)
{
     $stmt = $this->conn->prepare("SELECT profile_information,username,businessname,town from profile_information WHERE phone = ? AND profile_id= ?");
     $stmt->bindParam(1, $phone);
     $stmt->bindParam(2, $profile_id);
     $stmt->execute();
     $partial = $stmt->fecthAll();

     for($i = 0, $i < count($partial), $i++)
     {
         $stmtUpdate = $this->conn->prepare("UPDATE profile_information set profile_picture = ?, username= ?, businessname= ?, town= ? where profile_id= ?");
         $stmtUpdate->bindParam(1, $profilePicture);
         $stmtUpdate->bindParam(2, $userName);
         $stmtUpdate->bindParam(3, $businessName);
         $stmtUpdate->bindParam(4, $town);
         $stmtUpdate->bindParam(5, $profileId);
         $stmtUpdate->execute();
     }
}