user2643580
user2643580

Reputation:

Inserting Image to MySQL Database

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

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.

Upvotes: 0

Views: 3299

Answers (3)

dispake
dispake

Reputation: 3329

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.

Upvotes: 1

Jose Garrido
Jose Garrido

Reputation: 732

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]')";
}

Upvotes: 1

Julien
Julien

Reputation: 2319

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>

Upvotes: 1

Related Questions