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>";
}
}
?>
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);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With