0
<?php
include 'db.php';
//This is the directory where images will be saved
$target = "images/";
$target = $target . basename( $_FILES['photo']['name']);
//This gets all the other information from the form
  $stnam = $_POST['stName'];
  $stage = $_POST['stAge'];
  $stdob = $_POST['stDob'];
  $pic=($_FILES['photo']['name']);

mysqli_query("INSERT INTO test (name, age, dob, photo) VALUES ('$stnam', '$stage', '$stdob', 'pic')");

if(move_uploaded_file($_FILES['photo']['tmp_name'], $target))
{

echo "The file ". basename( $_FILES['photo']['name']). " has been uploaded, and your information has been added to the directory";
}
else {

echo "Sorry, there was a problem uploading your file.";
}

?>

I have been trying to upload an image file into sql database using phpmyadmin I used this above code when I executed it the file goes into directory well but the data values are not being inserted into mysql. Can anyone help?

2
  • you for got to place $ before pic Commented Dec 6, 2016 at 14:08
  • 1) You didn't check for any errors, make use of mysqli_error() function. 2) In the procedural way, mysqli_query() takes at least 2 arguments, first is the connection handler and second is the query string. Commented Dec 6, 2016 at 14:13

1 Answer 1

1

Changes

1) Put $ before pic in insert query.

2) And, keep insert query inside if(). Because, row will get inserted even though file is not moved to desired folder.

3) No connection variable is used in mysqli_query. It requires 2 arguments. [I assumed $conn as connection variable. Replace it according to your connection variable.] For more info, click mysqli_query

Updated Code

if(move_uploaded_file($_FILES['photo']['tmp_name'], $target)){
  mysqli_query($conn, "INSERT INTO test (name, age, dob, photo) VALUES ('$stnam', '$stage', '$stdob', '$pic')");
  echo "The file ". basename( $_FILES['photo']['name']). " has been uploaded, and your information has been added to the directory";
} else {
  echo "Sorry, there was a problem uploading your file.";
}
Sign up to request clarification or add additional context in comments.

4 Comments

Wow..it really helped, looks like I did not put the mysqli_query parameters well. Thank you so much for your help Nan Partykar.
The data is going in to database now.
Glad. It's Going. If this answer helped you. Then, Don't Forget To Mark This Answer As Correct Answer. As, It will help other user to find it easily. @Krish
@Krish : Please go through this link. You will get it. Thanks for replying. meta.stackexchange.com/questions/5234/…

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.