How to change image in php mysql

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

How to change image in php mysql

Chris G

7856 silver badges20 bronze badges

asked Jan 21, 2016 at 19:48

1

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

answered Jan 21, 2016 at 22:11

How to change image in php mysql

OddadmixOddadmix

1802 silver badges11 bronze badges

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>

How to change image in php mysql

jasie

2,0509 gold badges34 silver badges47 bronze badges

answered Sep 16, 2020 at 5:03

1

How we can update image in PHP?

How to update the fields without changing the uploaded image in PHP?.
Step1: Create a new field to pass the stored image from the database..
Step2: Paste the below-given code in the form action file. if(!empty($_FILES['files']['tmp_name']) && ($_POST['image_name']) || ! ... .
Step3: Now, submit the form..

How can I insert image in PHP?

Create The HTML Form.
<! DOCTYPE html>.
<html>.
<body>.
<form action="upload.php" method="post" enctype="multipart/form-data"> Select image to upload:.
<input type="file" name="fileToUpload" id="fileToUpload">.
<input type="submit" value="Upload Image" name="submit">.
</form>.
</body>.

Can I add image in MySQL database?

Well, you can put it into a database (with modern versions of mySql), but it's better to upload the file to either your server, or someone elses (like imgur) and store the URL in the database.

Can we do images using PHP?

PHP is not limited to creating just HTML output. It can also be used to create and manipulate image files in a variety of different image formats, including GIF , PNG , JPEG , WBMP , and XPM . Even more conveniently, PHP can output image streams directly to a browser.