We're a community of 1.1M IT Pros here for help, advice, solutions, professional growth and fun. Join us!
1,080,318 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Start New Discussion Reply to this Discussion

Trying to store path data in a table in MySQL

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

3
Contributors
21
Replies
1 Week
Discussion Span
6 Months Ago
Last Updated
22
Views
Question
Answered
GraficRegret
Junior Poster
176 posts since Nov 2012
Reputation Points: 0
Solved Threads: 2
Skill Endorsements: 0

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?

adam.adamski.96155
Junior Poster
189 posts since Oct 2012
Reputation Points: 43
Solved Threads: 40
Skill Endorsements: 4

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.

GraficRegret
Junior Poster
176 posts since Nov 2012
Reputation Points: 0
Solved Threads: 2
Skill Endorsements: 0

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.

diafol
Keep Smiling
Moderator
10,838 posts since Oct 2006
Reputation Points: 1,675
Solved Threads: 1,534
Skill Endorsements: 61

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?

GraficRegret
Junior Poster
176 posts since Nov 2012
Reputation Points: 0
Solved Threads: 2
Skill Endorsements: 0

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 />';
}
diafol
Keep Smiling
Moderator
10,838 posts since Oct 2006
Reputation Points: 1,675
Solved Threads: 1,534
Skill Endorsements: 61

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?

GraficRegret
Junior Poster
176 posts since Nov 2012
Reputation Points: 0
Solved Threads: 2
Skill Endorsements: 0

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

diafol
Keep Smiling
Moderator
10,838 posts since Oct 2006
Reputation Points: 1,675
Solved Threads: 1,534
Skill Endorsements: 61

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?

GraficRegret
Junior Poster
176 posts since Nov 2012
Reputation Points: 0
Solved Threads: 2
Skill Endorsements: 0

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

diafol
Keep Smiling
Moderator
10,838 posts since Oct 2006
Reputation Points: 1,675
Solved Threads: 1,534
Skill Endorsements: 61

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

GraficRegret
Junior Poster
176 posts since Nov 2012
Reputation Points: 0
Solved Threads: 2
Skill Endorsements: 0

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?

GraficRegret
Junior Poster
176 posts since Nov 2012
Reputation Points: 0
Solved Threads: 2
Skill Endorsements: 0

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

diafol
Keep Smiling
Moderator
10,838 posts since Oct 2006
Reputation Points: 1,675
Solved Threads: 1,534
Skill Endorsements: 61

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

GraficRegret
Junior Poster
176 posts since Nov 2012
Reputation Points: 0
Solved Threads: 2
Skill Endorsements: 0

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()?

GraficRegret
Junior Poster
176 posts since Nov 2012
Reputation Points: 0
Solved Threads: 2
Skill Endorsements: 0

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

header("Location: index.php");
diafol
Keep Smiling
Moderator
10,838 posts since Oct 2006
Reputation Points: 1,675
Solved Threads: 1,534
Skill Endorsements: 61

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.

GraficRegret
Junior Poster
176 posts since Nov 2012
Reputation Points: 0
Solved Threads: 2
Skill Endorsements: 0

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
diafol
Keep Smiling
Moderator
10,838 posts since Oct 2006
Reputation Points: 1,675
Solved Threads: 1,534
Skill Endorsements: 61

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

GraficRegret
Junior Poster
176 posts since Nov 2012
Reputation Points: 0
Solved Threads: 2
Skill Endorsements: 0

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.

diafol
Keep Smiling
Moderator
10,838 posts since Oct 2006
Reputation Points: 1,675
Solved Threads: 1,534
Skill Endorsements: 61

This question has already been solved: Start a new discussion instead

Post: Markdown Syntax: Formatting Help
 
You
 
 
© 2013 DaniWeb® LLC
Page generated in 0.1272 seconds using 2.82MB