0

I am trying to upload an image by updating MySQL table column with the image location.

<?php
session_start();
if(isset($_POST['upload']))
{
$link = mysqli_connect("localhost", "root", "root", "rental");

if($link == false){ 
    die("ERROR: Could not connect. " . mysqli_connect_error());
}
$image = $_FILES['propic']['tmp_name'];
$propic = addslashes(file_get_contents($image));
$lipic = "img/user.png";
$aadhar="img/user.png";
$sql= "UPDATE profiles SET profilepic='$propic', license='$lipic', aadhar='$aadhar' WHERE email='".$_SESSION['email']."'";
$result=mysqli_query($link,$sql);
}   
?>

HTML Code:

<form id="form" method="post" action="profile.php" enctype="multipart/form-data">
 <input type="file" name="propic" accept="image/*">
 <input type="file" name="lipic" accept="image/*">
 <input type="file" name="aapic" accept="image/*">
 <input type="submit" class="mybutton myfont" name="upload" value="Submit" style="width:100px; background-color:black;">
 </form>

MySQL table

enter image description here

M. Eriksson
  • 13,450
  • 4
  • 29
  • 40
  • Please check this link : https://www.w3schools.com/php/php_file_upload.asp – Lalbhusan Yadav Mar 14 '18 at 13:02
  • 1
    You cannot upload a file to varchar database, you upload the file into the directory files, [here example](https://www.w3schools.com/php/php_file_upload.asp) – Rendi Wahyudi Muliawan Mar 14 '18 at 13:03
  • Please also look into this to not get you website hacked: https://stackoverflow.com/questions/60174/how-can-i-prevent-sql-injection-in-php – FMK Mar 14 '18 at 13:03
  • _"by updating MySQL table column with the image location"_ - `$propic` contains the image content, not the image location. – M. Eriksson Mar 14 '18 at 13:08

1 Answers1

0

Try this code. You are missing file uploading code.

$image = $_FILES['propic']['tmp_name'];
$name = "img/$_FILES['propic']['name']";
move_uploaded_file($image, $name); //Here you have to upload a file in a folder.

This is just for a reference. Use this and you will be able to upload a file on server.

SRK
  • 3,476
  • 1
  • 9
  • 23
  • 1
    However, this won't help the OP with the actual issue in the question. – M. Eriksson Mar 14 '18 at 13:38
  • After moving the image to the directory, how can I store the location of the image in my database. – sai chaitanya Mar 14 '18 at 13:49
  • You just have to store image name. Although I strongly recommend to use timestamp while creating image name, so you don't override existing image in the folder. Create unique name, move image to folder and save the name in you db. – SRK Mar 15 '18 at 04:38