Hamda
Hamda

Reputation: 53

Update image in php

I want to make an update account page. When I change the image and press update it runs correctly, but when I update any other field than image, the image is deleted from the browser and also from the database table. This is my code:

<?php       
include("includes/db.php");
    $user=$_SESSION['customer_email'];

    $get_customer="select * from costumers where customer_email='$user'"; 
    $run_customer=mysqli_query($con, $get_customer);
    $row_customer=mysqli_fetch_array($run_customer);

    $c_id=$row_customer['customer_id'];
    $name=$row_customer['customer_name'];
    $email=$row_customer['customer_email'];
    $pass=$row_customer['customer_pass'];
    $img=$row_customer['customer_image'];    
?>       

<div style="margin-left:15%; margin-top:10%">
  <form action="" method="post" enctype="multipart/form-data"  />
     <table width="500px" align="center" bgcolor="blueskay">
        <tr align="center">
           <td colspan="2"><h2>Update Your Account</h2></td>
        </tr>
        <tr>
            <td align="right">Customer Name:</td>
            <td><input type="text" name="c_name" value="<?php echo $name; ?>" required /></td>
        </tr>
        <tr>
             <td align="right">Customer Image:</td>
             <td><input type="file" name="c_image" value="<?php echo $img; ?>"  /><img src="customer_images/<?php echo $img; ?>" width="150px" height="100px"></td>
        </tr>
        <tr>
             <td align="right">Customer Email:</td>
             <td><input type="text" name="c_email" value="<?php echo $email; ?>"required /></td>
        </tr>
        <tr>
             <td align="right">Customer Password:</td>
             <td><input type="password" name="c_pass" value="<?php echo $pass; ?>" required /></td>
        </tr>
        <tr align="center">
             <td colspan="2"><input type="submit" name="update" value="Update Account"/></td>
             <td></td>
        </tr> 
     </table>
   </form>
</div>

And this my php code:

<?php

    if(isset($_POST['update'])){

    $customer_id=$c_id;
    $c_name= $_POST['c_name'];
    $c_email= $_POST['c_email'];
    $c_pass= $_POST['c_pass'];
    $c_image= $_FILES['c_image']['name'];
    $c_image_temp=$_FILES['c_image']['tmp_name'];

    move_uploaded_file($c_image_temp , "customer_images/$c_image");

        $c_update="update costumers set customer_name='$c_name', customer_email='$c_email', customer_pass='$c_pass',  customer_image= '$c_image'
         where customer_id='$customer_id'";

    $run_update=mysqli_query($con, $c_update);

    if($run_update){

        echo"<script>alert('Your Account has been Updated successfully, Thanks')</script>";
                echo"<script>window.open('my_account.php','_self')</script>";   
    }
  }
?>

Upvotes: 3

Views: 43754

Answers (2)

user14246471
user14246471

Reputation:

Update data and image in database:

<?php
include('db.php');
$id=$_GET['id'];
$sql = "select * from `bio` where id='".$id."'"; 
$result = mysqli_query($conn, $sql);
$row = mysqli_fetch_array($result);
?>

<html>
<head></head>
<body>
    <form method='post' action="Update.php?id=<?php echo $row[0];?>" enctype="multipart/form-data"> <!-- getting id -->
        <input type='hidden' name='id' value="<?php echo $row[0];?>"><br><br>
            name: <input type='text' name='name' value="<?php echo $row[1];?>"><br><br>
            fathersname: <input type='text' name='fathersname' value="<?php echo $row[2];?>"><br><br>
            mailid: <input type='email' name='mailid' value="<?php echo $row[3];?>"><br><br>
            dob: <input type='Date'name='dob' value="<?php echo $row[4];?>"><br><br>
            phoneno: <input type='text'name='phoneno' value="<?php echo $row[5];?>"><br><br>
    
        Upload image: <input type='file' name='res' value=""><br><br> 
        <img src="upload/<?php echo $row[6]?>" style="width:100px;height:100">
        <input type='submit' name='submit' value='submit'>
    </form>

<?php
if(isset($_POST['submit']))
{
    $id=$_POST['id'];   
    $n=$_POST['name'];
    $f=$_POST['fathersname'];
    $m=$_POST['mailid'];
    $d=$_POST['dob'];
    $p=$_POST['phoneno'];
    $i=$_FILES['res']['name'];
    $target_dir = "upload/";
    $target_file = $target_dir . basename($_FILES["res"]["name"]);
    $FileType = strtolower(pathinfo($target_file,PATHINFO_EXTENSION));

    if (move_uploaded_file($_FILES["res"]["tmp_name"], $target_file)) 
    {
        $sql="Update `bio` set name='$n',fathersname='$f',mailid='$m',dob='$d',phoneno='$p',uploadimage='$i' where id='".$id."'";
        if(mysqli_query($conn,$sql))
        {
            header('location:view.php');
        }
        else 
        {
            echo 'not updated';
        }
    }
}
?>

</body>
</html>

Upvotes: 0

Oddadmix
Oddadmix

Reputation: 180

You can try to check whether the image is empty or not and update by condition.

$customer_id=$c_id;
$c_name= $_POST['c_name'];
$c_email= $_POST['c_email'];
$c_pass= $_POST['c_pass'];
$c_image= $_FILES['c_image']['name'];
$c_image_temp=$_FILES['c_image']['tmp_name'];

if($c_image_temp != "")
{
    move_uploaded_file($c_image_temp , "customer_images/$c_image");
    $c_update="update costumers set customer_name='$c_name', customer_email='$c_email', customer_pass='$c_pass',  customer_image= '$c_image'
     where customer_id='$customer_id'";   
}else
{
    $c_update="update costumers set customer_name='$c_name', customer_email='$c_email', customer_pass='$c_pass'
     where customer_id='$customer_id'";
}

$run_update=mysqli_query($con, $c_update);

Upvotes: 4

Related Questions