0

I have to update image in php and mysql database. I know I cannot set the value of file type input field but how do I achieve this?

//upload.php
User Image<input type="file" name="image" accept="image/png, image/jpeg, image/ico" value="<?php echo $_GET['image'];?>" />
//upload query

    $id= $_GET['id'];
    $fname=$_GET['fname'];
    $lname=$_GET['lname'];
    $image=$_GET['image'];
  $sql = "UPDATE myGuest SET firstname='$fname', lastname='$lname', image='$image' WHERE id='$id'";  
    if($conn->query($sql))
    {
        echo "<script>alert('Record updated succesfully')</script>";
        header("Refresh:0; url=view.php");
    }
    else{
        echo "Error: ".$sql."<br>".$conn->error;
}
//view.php
<td><a href="update.php?id=<?php echo $row['id']?>&fname=<?php echo $row['firstname']?>&lname=<?php echo $row['lastname']?>&image=<?php echo $row['image']?>">EDIT</a></td>
6
  • 1
    Same way as you do with INSERT new data. Commented Apr 21, 2019 at 18:08
  • Please elaborate on that Commented Apr 21, 2019 at 18:09
  • Note that your query is vulnerable to SQL injections. Consider using prepared statements Commented Apr 21, 2019 at 18:09
  • I'm aware...I'm new to php and database connectivity. and this is only my homework. Commented Apr 21, 2019 at 18:11
  • Even for homework, it's always a good habit to get :). Imagine, you want to insert the name O'Bryan, your query will fail because of the single quote Commented Apr 21, 2019 at 18:12

1 Answer 1

1

The data type should be longblob to insert image in DB.

 $image_content = addslashes(file_get_contents($_FILES['image']['tmp_name']));

Now you can use variable $image_content to insert/update image in DB.

To display the image use

header("Content-type: image/jpg"); 
echo $row['image'];

For more details Follow this

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.