Update data in mysql without change of exists file

Sometimes when you update the data but not upload the file, then the file (which is already in the database) is automatically removed. So, if you want to update only the data but not the file in database then the syntax is:

<?php
$connect=mysql_connect("hostname","username","password") or die ("connection failed");
$db=mysql_select_db("database_name",$connect);

if(isset($_POST['submit']))
{
$name=$_POST['name'];
$email=$_POST['email'];
$filename=$_FILES['image']['name'];
$tempname=$_FILES['image']['tmp_name'];
if($filename!=="")
{
$query="UPDATE users SET name='$name',email='$email',image='$filename' WHERE id=1";
}
else
{
$query="UPDATE users SET name='$name',email='$email' WHERE id=1";
}
$update=mysql_query($query);
$movefile=move_uploaded_file($tempname,"images/".$filename);
if($update)
{
$msg="Your profile has been updated.";
}
else
{
$msg="Error occurred....";
}
}

?>

<?php

$sql=mysql_query("select * from users where id=1");
$fetch=mysql_fetch_array($sql);
$name=$fetch['name'];
$email=$fetch['email'];
$image=$fetch['image'];

?>

<form action="" method="post">
Profile Image: <img src="images/<?php echo $image; ?>" style="width:50px; height:50px;"><br>
Name: <input type="text" name="name" value="<?php echo $name; ?>"><br>
Email: <input type="email" name="email" value="<?php echo $email; ?>"><br>
Upload Image: <input type="file" name="image">

<input type="submit" name="submit" value="Click Here">
</form>



Please follow and like us:

Leave a Reply

Your email address will not be published. Required fields are marked *