• 18 Jan, 2025

How To Update/Edit In Mysql Using PHP

How To Update/Edit In Mysql Using PHP

The MySQL UPDATE query is used to update existing records in a MySQL database.
It can be used to update one or more field at the same time.and specify any condition using the WHERE clause.

 

Syntax :
Update table_name set column1=value1,column2=value2 Where column_name=value;


curd operation update code

 


if (isset($_POST['update_btn'])) {
    
              $email = $_POST['email'];
              $firstname = $_POST['firstname'];
              $lastname = $_POST['lastname'];
              $address = $_POST['address'];
              $gender = $_POST['gender'];
              $hobbies = isset($_POST['hobbies']) ? implode(", ", $_POST['hobbies']) : '';
              $phonenumber = $_POST['phonenumber'];
              $DOB = $_POST['DOB'];
              $imagePath = $row['img'];
 
   if (isset($_FILES['image']) && $_FILES['image']['error'] == 0) {
   
      $imageTmp = $_FILES['image']['tmp_name'];
      $imageName = time() . $_FILES['image']['name'];
      $imagePath = $imageName;
      if ($row['img'] && file_exists("uploads/" . $row['img'])) {
          unlink("uploads/" . $row['img']);
      }
      
      if (!move_uploaded_file($imageTmp, "uploads/" . $imagePath)) {
          $imagePath = $row['img'];
      }
  }
  
  $query = "UPDATE phpmaincrud SET 
                       email='$email', 
                       firstname='$firstname', 
                       lastname='$lastname', 
                       address='$address', 
                       gender='$gender', 
                       hobbies='$hobbies', 
                       img='$imagePath', 
                       phonenumber='$phonenumber', 
                       DOB='$DOB
                       WHERE id='$id'"
;
                       
  $data = mysqli_query($con, $query);

 

Download Full Code HERE 

Download Fome Here