0

I am working on a solution that will help me submit specific images from a list of images to a MySQL Database.

My database consists of the following:

Database

  • id(INT)
  • photo(BLOB)
  • caption(VAR)

Code

I am first retreiving images from a list and giving them each a submit button.

foreach ($media->data as $data) {
  echo $pictureImage = "<img src=\"{$data->images->thumbnail->url}\">";
  echo "<form action='tag.php' method='post'>";
  echo "<input type='submit' name='submit' value='Click Me'>";  
  echo "</form>";
}

$pictureImage parses the data URL and then puts it into an actual image. The submit button is below each of those images.

I am then making it so that when the submit button is pressed, it is added to the database.

if(isset($_POST['submit'])) {
  //Database code would be above the following
  $sql="INSERT INTO $usertable (image) VALUES ('$pictureImage')";
}

Problem

I am running into an issue where the last image in my list is the one being submitted to the database, rather than the image with the corresponding submit button. How do I make it so that it is grabbing the photo with the corresponding submit button?

Any help would be appreciated greatly.

  • Why do you want to store the image instead of the URL of the image? http://stackoverflow.com/questions/3748/storing-images-in-db-yea-or-nay – ChrisW Aug 01 '13 at 23:05
  • @ChrisW No issue with doing either. Just want the ability to actually store the corresponding image or URL in the database rather than the last one in the list. –  Aug 01 '13 at 23:13
  • Have you read the manual on file uploads? http://au1.php.net/manual/en/features.file-upload.php –  Aug 01 '13 at 23:16

3 Answers3

1

You are not posting any data when you click on your submit buttons.. i would suggest that for each form you would have a hidden field with the url of the image. something like this:

<form action='tag.php' method='post'>
<input type="hidden" value="{$data->images->thumbnail->url}" name="pic"/>
<input type='submit' name='submit' value='Click Me'>
</form>
Julien
  • 2,217
  • 2
  • 28
  • 49
1

You need to include any identifier to the image inside the form in order to store it.

For example you can try building the forms like this:

foreach ($media->data as $data) {
  echo $pictureImage = "<img src=\"{$data->images->thumbnail->url}\">";
  echo "<form action='tag.php' method='post'>";
  echo "<input type='hidden' name='imageid' value='{$data->images->thumbnail->url}'>";
  echo "<input type='submit' name='submit' value='Click Me'>";  
  echo "</form>";
}

And when you want to store the image on the form submit you can actually pick up the identifier of the image (in this case I used the URL you posted):

if(isset($_POST['submit'])) {
  $sql="INSERT INTO $usertable (image) VALUES ('$_POST[imageid]')";
}
Jose Garrido
  • 732
  • 1
  • 15
  • 31
  • I'd recommend you to download the image using $img = file_get_contents($data->images->thumbnail->url) after submitting the form and then saving the content of $img into the database (_photo_ field on your table)... I can elaborate this better in the answer if it's needed. – Jose Garrido Aug 01 '13 at 23:29
  • Why don't you do it **after** submitting the form? Why don't you donwload the image **after** the user selected which image to click on? – Jose Garrido Aug 01 '13 at 23:40
  • Figured it out. Thanks! –  Aug 01 '13 at 23:51
1

Problem

foreach ($media->data as $data) {
   echo $pictureImage = "<img src=\"{$data->images->thumbnail->url}\">";
   echo "<form action='tag.php' method='post'>";
   echo "<input type='submit' name='submit' value='Click Me'>";  
   echo "</form>";
}

By the time this loop is done, you will have the last image in the loop as the value for $pictureImage.

So when it gets to this point, you're $pictureImage is still... the last image.

if(isset($_POST['submit'])) {
  //Database code would be above the following
  $sql="INSERT INTO $usertable (image) VALUES ('$pictureImage')";
}

Solution

I'm not exactly sure what data value you want to save because right now it looks like the whole tag. But whatever it is, you need to put it into a form field first ...

foreach ($media->data as $data) {
    ..
    echo "<form action='tag.php' method='post'>";
    echo "<input type='hidden' name='imageurl' value='<img src=\"{$data->images->thumbnail->url}\">' />";
    ..
}

and then retrieve it in the POST part of your script:

if(isset($_POST['submit'])) {
   $imageurl = $_POST['imageurl'];
   //Database code would be above the following
   $sql="INSERT INTO $usertable (image) VALUES ('$imageUrl')";
}

This way, it won't always be the last value in your list, rather, it will be the one you selected.

dispake
  • 3,259
  • 2
  • 19
  • 22