ok so here is my 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><td>";
        echo $row['make'];
        echo "</td><td>";
        echo $row['model'];
        echo "</td><td>";
        echo $row['mileage'];
        echo "</td><td>";
        echo $row['description'];
        echo "</td></tr>";
    }
echo "</table>";
?>

<form action="upload.php" method="post"
enctype="multipart/form-data">
<input type="text" name="refID" value=<?php echo $ref ?> />
<label for="file">Upload Picture:</label>
<input type="file" name="file" id="file" /> 
<br />
<input type="submit" name="submit" value="Submit" />
</form>

</body>
</html>

what I need to do is pass the value of $row['id'] to my upload.php code which is as follows:

<?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";
  }
?>

I need to pass the information over so I can insert it into a table to keep track of the uploads properly

Recommended Answers

All 11 Replies

Member Avatar for diafol
<input type="text" name="refID" value=<?php echo $ref ?> />

Where's $ref set? Is this your $row['id']?

I can't see how this is supposed to work.

Based on this code you can have several different ID's but only one upload form. How is the user picking which veh they are upload the photos for? You just need to add a hidden form field to your form once you know
<input type="hidden" name="veh_id" value="<?= echo $row['id']; ?>" />

I think instead of

<input type="text" name="refID" value=<?php echo $ref ?> />

you should cut "echo" away. And $ref I think is your id. then do a $ref=$row['id'];

Member Avatar for diafol

Trouble is the fact that you're extracting multiple records, so you have many $row['id']s. Which one should be placed in the form?

@Diafol, hello again and sorry, that part of the code should have been deleated, that was a part of a failed attempt at getting it over using an input and I had set $ref = $row['id']; but it didnt work, though I did try a few different variations of that.

@GP, The code will be adjusted to pull up only the vehicle that they click "details" for, I only have one vehicle entered into the vehicles table at the moment though so I just used that code to make it quick.

If you are only pulling one result from the DB than on your main page inside your upload form:

<input type="hidden" name="veh_id" value="<?= echo $row['id']; ?>" />

Than on your upload.php file:

$veh_id = $_POST['veh_id'];

@GP, Adding that code to my page, rather then making it hidden, i make it a text field so I can see if it is working propperly, however it just shows a "\" in stead of a number, is that because I am pulling everything from the table even though there is only one? maybe I need to pull the specific row rather then everything? then maybe it will show?

yeah pulling the specific record didnt change the value, it is still the "\" in stead of the id

Sorry, your $row variable is only available within the scope of your while loop:

while($row = mysql_fetch_array($result))    {
  //print out the contents of each row
  echo "<tr><td>";
  echo $row['id'];
  $veh_id = $row['id'];
  echo "</td><td>";
  echo $row['year'];
  echo "</td><td>";
  echo $row['make'];
  echo "</td><td>";
  echo $row['model'];
  echo "</td><td>";
  echo $row['mileage'];
  echo "</td><td>";
  echo $row['description'];
  echo "</td></tr>";
}

?>

...

<input type="text" name="veh_id" value="<?php echo $veh_id; ?>">

thanks that worked, i had tried something similar but apparently I had set and called it in the wrong place.

i must say I really like DaniWeb here, i have tried other help forums but I have only recieved responses that actually help here. the other threads send me links to things I have already tried and explored. You all rock here.

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.