Image update in PHP with updating of other fields - Update Image with other fields

 

 

<form action="" method="POST" enctype="multipart/form-data">

  <label for="name">Name</label>

  <input type="text" class="w3-input w3-border" id="name" name="name" required value="<?php echo $row2['stu_name']; ?>">

  <label for="email">Email</label>

  <input type="email" class="w3-input w3-border" id="email" name="email" required value="<?php echo $row2['stu_email']; ?>" >

 <label for="profilepic">Profile Picture <small> (Note : Image size should be less than 500 KB) </small></label>

 <input type="hidden" name="old-image" value="<?php if(isset($img)){ echo $img; }  ?>">

  <input type="file" class="w3-input w3-border w3-white" id="profilepic" name="profilepic">

  <img src="./profilepic/<?php if(isset($img)){ echo $img; }  ?>" alt="Profile Picture" width="100px" height="100px">

 

 <button type="submit" class="w3-btn w3-green text-center" name="update" ><i class="fa-solid fa-check"></i> Update</button>

    </form>

 

 

if(isset($_REQUEST['update'])){ if(($_POST['phone'] !== '') && ($_REQUEST['name'] !== '')){ if(empty($_FILES['profilepic']['name'])){ // If new profile photo is empty then use old image $image_name = $_REQUEST['old-image']; $name = trim($_REQUEST['name']); $phone = trim($_REQUEST['phone']);        // Update other credential with old image
$sql = "UPDATE `student` SET `stu_img` = ?, stu_name = ?, stu_phone = ? WHERE `student`.`stu_id` = ?"; $result = $conn->prepare($sql); if($result->execute(array($image_name, $name, $phone, $stu_id))){ $msg = '
Updated Successfully
'; } } else {    // If user selects new Image then $errors = array(); $file_name = $_FILES['profilepic']['name']; $file_size = $_FILES['profilepic']['size']; $file_tmp = $_FILES['profilepic']['tmp_name']; $file_type = $_FILES['profilepic']['type']; $file_e = explode('.', $file_name); $file_ext = end($file_e); $extensions = array('jpeg', 'jpg','png','JPG'); if(in_array($file_ext, $extensions) == FALSE){ $errors[] = "This file extension is not allowed, Please choose a .jpg or .png file"; } if($file_size > 512000){ $errors[] = "File size must be less than 500 KB"; } $new_name = time()."--".basename($file_name); $target = "profilepic/".$new_name; $image_name = $new_name; if(empty($errors) == TRUE){ // Check old image to delete or if default image then keep the default image for other newly registered users u $sql = "SELECT stu_img FROM student WHERE stu_id = ?"; $result = $conn->prepare($sql); $result->execute(array($stu_id)); if($result->rowCount() >= 1){ $value = $result->fetch(PDO::FETCH_ASSOC); if($value['stu_img'] !== 'image'){ // If user-old-image is not equal to default it means user had changed their profile-picture its means we have to delete // if user have image then delete otherwise not to delete unlink('profilepic/'.$value['stu_img']); // delete current image from profilepic folder } // after deleting current-image move new-image move_uploaded_file($file_tmp, $target); // update profile picture name in the database and other fields $name = trim($_REQUEST['name']); $phone = trim($_REQUEST['phone']); $sql = "UPDATE `student` SET `stu_img` = ?, stu_name = ?, stu_phone = ? WHERE `student`.`stu_id` = ?"; $result = $conn->prepare($sql); if($result->execute(array($image_name, $name, $phone, $stu_id))){ $msg = '
Updated Successfully
'; } } }else{ $msg = ''; foreach($errors as $error_msg){ $msg .= '
'.$error_msg.'
'; } /*print_r($errors); die();*/ } } // else part of - If user selects new Image then
} else { // if-else part of first validation of empty strings $msg = 'Fill all fields'; } } // if of isset
// Code to display information to update $sql2 = "SELECT stu_img, stu_name, stu_rollno, stu_email, stu_phone FROM student WHERE stu_id = ?"; $result2 = $conn->prepare($sql2); if($result2->execute(array($stu_id))){ if($row2 = $result2->fetch(PDO::FETCH_ASSOC)){ if($row2['stu_img'] == 'image'){ $img = 'projectpic.png'; }else{ $img = $row2['stu_img']; } }else{ $img = 'projectpic.png'; } }

Comments

Popular posts from this blog

Pagination in PHP

File upload using JavaScript with validation (Apply validation on frontend side)

Diagonal Difference - HackerRank Solutions