Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I have set up the html and javascript here:

http://jsbin.com/aNiTixA/1/edit?html,css,output

Here is my php code:

<?php
$con=mysqli_connect("localhost","up637415_cms","up637415_cms","up637415_cms");
// Check connection
if (mysqli_connect_errno())
  {
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
  }

$sql="INSERT INTO products (prod_title, prod_category, prod_quantity, prod_description, prod_img)
VALUES
('$_POST[prod_title]','$_POST[prod_category]','$_POST[prod_quantity]','$_POST[prod_description]','$_POST[file1]')";

if (!mysqli_query($con,$sql))
  {
  die('Error: ' . mysqli_error($con));
  }
echo "1 record added";

mysqli_close($con);
?> 

What I want to do is store the path of the recently uploaded image in the MYSQL database in the column named 'prod_img' so that it can be displayed on the front end of the site. I am not sure how to get the path from the image after its uploaded and post it to the database when the submit button has been pressed.

share|improve this question
 
#1 - the FILE is accessed through $_FILES[] not $_POST, #2, The file must be moved somewhere otherwise the location will be a temporary one and is not guaranteed to exist forever, #3 - thank you for using MySQLI. –  Ohgodwhy 21 hours ago
 
And where your file_upload_parser.php store images? Also, you can store just image name in db, if upload folder location is known... –  nevermind 21 hours ago
add comment

1 Answer

You need to handle file uploads yourself and move the file to where you want it. By doing so, you will have the file path as well (because you generate it).

You can find the position of the uploaded file in $_FILES['yourfieldname']['tmp_name']. But since this is a temporary path, you need to move it by using move_uploaded_file($src, $dst);.

See the PHP documentation for a full example:

Also, you should read about MySQL injections, as your code is incredibly insecure and thus dangerous, if you run it in public.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.