Update data in MySQL without change of exists file
×


Update data in MySQL without change of exists file

13350

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

<?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'];
name=$_FILES['image']['name'];
$tempname=$_FILES['image']['tmp_name'];
if(name!=="")
{
$query="UPDATE users SET name='$name',email='$email',image='name' 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/".name);
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

In this code the following methods and SQL statements are used to update the existing records:

* post()
* IsSet string
* update statement with where clause
* select statement
* SQL query function

Explanation of the code
We have used post method because unlimited data i.e. both text and binary data, can be easily passed through post method and it is more secure.

The update statement is used for the existing record's modification. Along with the update statement, where clause is joined together to apply the modification of those records that match the particular criteria. After the update statement, we have executed the respective query by integrating it to the PHP SQL query() function in order to update records of the table.




Best WordPress Hosting


Share:


Discount Coupons

Get a .COM for just $6.98

Secure Domain for a Mini Price



Leave a Reply


Comments
    Waiting for your comments