简体   繁体   中英

How to select and update from one table in one query using object oriented PHP inside class

I need to select data i have in my table row after its selection and i update it at that time but is not working . 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 calls above function

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

This html interface which hosts the form

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

You can't do two actions in one statement. In your updateProfile method, you need execute a SELECT role, storage the collection in a variable and after do the UPDATE role, like this:

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

Please check your code format, and use the PSR's to guide your development.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM