I am trying to get my code working so that I can store the path of an uploaded picture onto a table so that I can call upon the uploaded file whenever I need it.

So what happens on the web page is, a person enters information on a vehicle and they click submit to enter the information onto our database. Then after that they are taken to another page that I am trying to set up to be referenced to that specific vehicle entry using the entry ID, on this page the person will be able to upload photos of the vehicle to our web server which will be stored in a folder and I would like to be able to pull off of it an upload ID and the path to access the photo and store it in another table that will hold the vehicles ID in the vehicle table and then the photos id in the photo table and the path to access the photo all of course in their own separate columns in the photo table so that on the page that results, I can display the vehicle information with a main picture decided by its reference ID matching the vehicle ID and the picture ID being the lowest in the sequence that is attached to that vehicle's ID number. Then I would like to be able to display the remaining photoes if there are any in a grid so that others can view them at their liesure. I would also like to be able to run a slide show that shows the main pictures of each vehicle stored on the database using the same method used to display the main picture of the vehicle

Recommended Answers

All 21 Replies

Hey GraficRegret, welcome to Daniweb :)
Your description sounds like a typical use of PHP and MySql, but what help do you specifically need? Is there some code you need some assistance with? Are you looking for general considerations and tips from those who already walked that road?

Thanks for the welcome, I am looking for maybe some direction as to where I might find some answers, I am trying to learn how to do this all myself and thus far have had no success finding any direciton on this problem searching W3 Schools, PHP Manual, and other similar sights, I always find and now understand how to set up an upload and how to move it to a desired folder for storeage but everywhere I have looked gives no disernable solution to taking the path that is created that referes to that specific upload and inserting it into a table I created to be able to join with the table holding the vehicle information.

Member Avatar for diafol

Jeez - that must be one of the longest sentences ever. Out of breath!

Don't store a path in the DB if you're uploading all files to the same folder. Just the filename.

My appologies, I tend to forget my punctuation when typing. Will I be able to pull up the immages like I need to with just the file name?

Member Avatar for diafol

To where are you storing the images - just one folder?
In order to make your site resources manageable, store paths etc in constants:

define('IMG_PATH', 'images/');

The when you get your filenames from the DB, e.g.

while($row = mysql_fetch_assoc($result)){
    echo '<img src = "' . IMG_PATH . $row['img'] . '" /> <br />';
}

Thank you sir I will try that, any clues as to where I can find a way to pull the id of the refering vehicle entry? or should that be a paramiter that I pass into a the insert when I set it up for the picture table?

Member Avatar for diafol

You should include code if you're asking questions about it. I have no idea what you've done or your SQL tables.

Im sorry I am not sure what code you need in order to help me with this, what information can I give you that would help you understand what it is my code needs to do?

Member Avatar for diafol

SHow your relevant SQL table(s) - fieldnames and datatypes
SHow the code used to extract the filename
Show the code used to insert a filename
Show your form html

ok I will get that all together and put it up for you in a relevant manner so that it should be relatively easy for you to read

ok so here is the mock form that I made in order to test the code as I was writing it:

<html>
<body>

<form action = "insert.php" method = "post"></br>
Make: <input type = "text" name = "make" /></br>
Model: <input type = "text" name = "model" /></br>
Year: <input type = "text" name = "year" /></br>
Odometer: <input type = "text" name = "mile" /></br>
Description: <TEXTAREA name = "des" ROWS = 5 COLS = 15/>
    </TEXTAREA></br>
<input type = "submit" />
</form>

</body>
</html>

Here is the code inserting the information into the vehicle table

<?php
$con = mysql_connect("localhost","username","password");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }

mysql_select_db("my_db", $con);

$sql="INSERT INTO Vehicles (year, make, model, mileage, description)
VALUES
('$_POST[year]','$_POST[make]','$_POST[model]','$_POST[mile]',
    '$_POST[des]')";

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

mysql_close($con);
?>

this is the form i made to test my upload code

<html>
<body>

<form action="upload.php" method="post"
enctype="multipart/form-data">
<label for="file">Filename:</label>
<input type="file" name="file" id="file" /> 
<br />
<input type="submit" name="submit" value="Submit" />
</form>

</body>
</html>

and finally the code for the upload itself

<?php
$con = mysql_connect("localhost","username","password");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }

mysql_select_db("my_db", $con);

$allowedExts = array("jpg", "jpeg", "gif", "png");
$extension = end(explode(".", $_FILES["file"]["name"]));
if ((($_FILES["file"]["type"] == "image/gif")
|| ($_FILES["file"]["type"] == "image/jpeg")
|| ($_FILES["file"]["type"] == "image/png")
|| ($_FILES["file"]["type"] == "image/pjpeg"))
&& ($_FILES["file"]["size"] < 20000)
&& in_array($extension, $allowedExts))
  {
  if ($_FILES["file"]["error"] > 0)
    {
    echo "Return Code: " . $_FILES["file"]["error"] . "<br />";
    }
  else
    {
    echo "Upload: " . $_FILES["file"]["name"] . "<br />";
    echo "Type: " . $_FILES["file"]["type"] . "<br />";
    echo "Size: " . ($_FILES["file"]["size"] / 1024) . " Kb<br />";
    echo "Temp file: " . $_FILES["file"]["tmp_name"] . "<br />";

    if (file_exists("VPics/" . $_FILES["file"]["name"]))
      {
      echo $_FILES["file"]["name"] . " already exists. ";
      }
    else
      {
      move_uploaded_file($_FILES["file"]["tmp_name"],
      "VPics/" . $_FILES["file"]["name"]);
      echo "Stored in: " . "VPics/" . $_FILES["file"]["name"];
      }
    }
  }
else
  {
  echo "Invalid file";
  }
?>

Everything I have been getting for inserting the vehicle ID into a table for reference, so that the pictures are matched propperly to the vehicles, looks to be a temperary fix, because the code would change the next time it was called and if someone pulled up and tried to add pictures to a previous entry the reference ID would have changed. where am I going wrong?

Member Avatar for diafol

OK, I think I got you. How many photos can each individual vehicle have?

I am trying to set it up so they dont have a limit, but if I need to set a limit I think we will cap it at like 15 or 20 so they can get every posible angle in there

Ok so i figured out what I need to do, its just a matter now of using the php coding to open a new web page to call the information up that I need in order to store it in the new table. So simple, I dont know why I didnt see it sooner. However I am not finding the function to open a web page, do you have any idea what the function is called? Or am I just being stupid again and its fopen()?

Member Avatar for diafol

If you need to move to a new page after processing this, use a header redirect:

header("Location: index.php");

alright thanks. Is there some reason that this code:

<html>
<body>

<?php
$con = mysql_connect("localhost","username","password");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }

mysql_select_db("my_DB", $con);

//Get all the data from the table
$result = mysql_query("SELECT * FROM Vehicles")
    or die(mysql_error());

echo "<table border = '1'>";
echo "<tr><th>ID</th><th>Year</th><th>Make</th><th>Model</th>
<th>Milage</th><th>Description</th></tr>";
//keeps getting the next row untill no more exist
while($row = mysql_fetch_array($result))
    {
        //print out the contents of each row
        echo "<tr><td>";
        echo $row['id'];
        echo "</td><td>";
        echo $row['year'];
        echo "</td></tr>";
        echo "<tr><td>";
        echo $row['make'];
        echo "<tr><td>";
        echo $row['model'];
        echo "<tr><td>";
        echo $row['mileage'];
        echo "<tr><td>";
        echo $row['description'];
    }
echo "</table>";
?>

<form action="upload.php" method="post"
enctype="multipart/form-data">
<label for="file">Filename:</label>
<input type="file" name="file" id="file" /> 
<br />
<input type="submit" name="submit" value="Submit" />
</form>

</body>
</html>

would display this:

"; echo "IDYearMakeModel MilageDescription"; //keeps getting the next row untill no more exist while($row = mysql_fetch_array($result)) { //print out the contents of each row echo ""; echo $row['id']; echo ""; echo $row['year']; echo ""; echo ""; echo $row['make']; echo ""; echo $row['model']; echo ""; echo $row['mileage']; echo ""; echo $row['description']; } echo ""; ?>
Filename: (search button)
(submit button)

am I missing something? or does any page that has PHP code in it have to be saved as .PHP? currently it is saved as a .HTML because thats what it started as.

Member Avatar for diafol

would display this:

You can't run php within html files unless you explicitly say so in your .htaccess file:

AddType application/x-httpd-php .html

ok so then the code should be displaying propperly? Do you see any reason why the page should be displaying impropperly?

Member Avatar for diafol

You need to run the file as php file - change the extension from .html to .php or modify your .htaccess file as explained in my previous post. I suggest the first option (change extension). Do that, see if there are any errors THEN come back.

wow ok that worked thanks your a very kind and patient man Dia i look forward to learning a lot from you and hopefully passing that on to others. you rock bud

Member Avatar for diafol

No worries GR. It's a common mistake and a real head-scratcher if you're not aware of it. People think that because php is installed, that the code will run in all types of files.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.