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

时间:2016-09-26 13:54:25

标签: php mysqli

我需要在选择后选择我的表格行中的数据并在那时更新它但不起作用。 Table structure

               class DbHandler {

private $conn;

function __construct() {
    require_once dirname(__FILE__) . '/DbConnect.php';
    // opening db connection
    $db = new DbConnect();
    $this->conn = $db->connect();
}
  public function updateProfile($profile_picture, $username,$businessname, $town ) {


   $stmt = $this->conn->prepare("SELECT profile_information,username,businessname,town from profile_information WHERE profile_id= ?");
   $stmt = $this->conn->prepare("UPDATE profile_information set profile_picture = ?, username= ?, businessname= ?, town= ? where profile_id= ?");
    $stmt->bind_param("ssssi",$profile_picture, $username, $businessname, $town,$profile_id);   
    $stmt->execute();
   $stmt->close();
}

  }

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);
                } 
             ?>

此html界面主持表单

                        <!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>

1 个答案:

答案 0 :(得分:-2)

您不能在一个声明中执行两个操作。在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();
     }
}

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