3

I am currently creating an application that grabs a list of instagram images and then inserts certain ones (based on your choice) into a specific database.

Currently, when I select the submit button it is grabbing the last available image in the list and inserting that into my database. Each image is assigned a submit button. How do I make sure the proper image, relative to the submit button, is the one being inserted into the database.

The following is my code for the list of images and the submit button for each image:

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

This is how I am inserting the image into my database. Remember, this grabs the last available image in the list and inserts that.

if(isset($_POST['submit'])) {

// There variables are for the database information
$hostname = "random";
$username = "random";
$dbname = "random";
$password = "random!";
$usertable = "random";

//Connecting to your database
$con = mysql_connect($hostname, $username, $password) OR DIE ("Unable to 
connect to database! Please try again later.");
mysql_select_db($dbname, $con);

$sql="INSERT INTO $usertable (image) VALUES ('$pictureImage')";
if (!mysql_query($sql,$con)) {
    die('Error: ' . mysql_error($con));
}
    mysql_close($con);
}

Any suggestions?

asdasdasd
  • 31
  • 3

3 Answers3

2

Change your form to this:

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

and then process the $image[] array on the backend.

foreach ($_POST['image'] as $k=>$image){
    $sql="INSERT INTO $usertable (image) VALUES ('$image')";
    if (!mysql_query($sql,$con)) {
        die('Error: ' . mysqli_error($con));
    }
}

you might need to make few more small changes to your code but this logic should work.

Update: since the OP wants one button for each image, this is the code for that:

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

The foreach ($_POST['image'] ) part should then be changed to:

$sql="INSERT INTO $usertable (image) VALUES ('".file_get_contents($_POST['image'])."')";
if (!mysql_query($sql,$con)) {
    die('Error: ' . mysqli_error($con));
}

This should insert the actual image image in the database as the OP wants.

Maximus2012
  • 1,799
  • 2
  • 12
  • 15
  • Understanding your second part but not necessarily the first. Why didn't you show the image in the form like I had done originally? And what happened to $pictureImage? – asdasdasd Jul 31 '13 at 20:21
  • your original logic creates one form for each image which is not needed. Also, – Maximus2012 Jul 31 '13 at 20:23
  • There are some minor updates to the code in my answer that I would like to make but its giving me error every-time I try making the updates. – Maximus2012 Jul 31 '13 at 20:26
  • Try in a little while or do a hard refresh. I'm trying to use your code now but maybe my placements, etc. are off. – asdasdasd Jul 31 '13 at 20:29
  • Can you modify the input type code so the slashes don't cause issues? – asdasdasd Jul 31 '13 at 20:34
  • Since the code is escaped already so I don't think it should cause any issues. Are you getting any error message ? – Maximus2012 Jul 31 '13 at 20:35
  • See code edits I've made. Should I be changing something? Getting an error where I placed your second half of code. – asdasdasd Jul 31 '13 at 20:37
  • Can you indicate which ones are lines 38-43 and what is the error that you are getting ? – Maximus2012 Jul 31 '13 at 20:38
  • Where foreach($POST['image'] starts. – asdasdasd Jul 31 '13 at 20:39
  • Invalid argument supplied for foreach() – asdasdasd Jul 31 '13 at 20:40
  • can you do this: var_dump($_POST['image']); exit; before the start of the foreach loop in which you are getting error ? – Maximus2012 Jul 31 '13 at 20:41
  • I got null when I do that. – asdasdasd Jul 31 '13 at 20:42
  • Are you submitting the form correctly ? Can you make sure of that ? – Maximus2012 Jul 31 '13 at 20:43
  • When I do select the button I just get an array of the image URLs. – asdasdasd Jul 31 '13 at 20:44
  • on what page do you get that ? – Maximus2012 Jul 31 '13 at 20:45
  • There is only one submit button where there should be one for every image. When I select the submit button and go to the page that retrieves the database the list of URLs for all of the images are now there. – asdasdasd Jul 31 '13 at 20:46
  • The error is resolved but not the problem I originally am having. – asdasdasd Jul 31 '13 at 20:49
  • I don't want to retreive the url I want to store the actual image in my database. – asdasdasd Jul 31 '13 at 20:49
  • So storing based off which submit button is pressed – asdasdasd Jul 31 '13 at 20:50
  • Your code grabs the url and stores it. I just want to store the image itself based on the submit button attached to the image. Each image should have one submit button. – asdasdasd Jul 31 '13 at 20:52
  • ok for that you need to extend the logic in my answer to pass the image url to 2nd page, get the image from that URL and store it in the database. That is not at all advisable though: http://stackoverflow.com/questions/3014578/storing-images-in-mysql You might want to store the images on your disk and then store the location of the image on the database rather than storing the whole image in the database. – Maximus2012 Jul 31 '13 at 20:52
  • I am storing it as a BLOB and doing all of the encoding right now I'm just worried about testing. The code in the edits i just made adds a submit button for each image and submits the last one in the list to the database. Can you help me make it so it submits the one with the button attached that is being pressed? – asdasdasd Jul 31 '13 at 20:54
  • see the updated answer for that. I think it would be better if you do something like this using AJAX/jQuery. – Maximus2012 Jul 31 '13 at 20:59
  • I have no issue adding a button for each image. My issue is submitting the image attached to a specific button into the database. Right now it's adding the last image into the database. – asdasdasd Jul 31 '13 at 21:00
  • Yes. That adds the last 2 images. – asdasdasd Jul 31 '13 at 21:02
  • I am sorry then. This is all I can suggest. – Maximus2012 Jul 31 '13 at 21:11
0

If you want to have a submit button for each image, you can keep the multiple form construction and just add:

echo "<input type='hidden' name='image_url' value='".$data->images->thumbnail->url."'>";

Then retrieve the URL in tag.php with $_POST['image_url']. Be sure to validate the URL before putting it in the SQL query.

Valk6
  • 96
  • 1
  • 6
0

Since you're creating a form for each image, the submit button will only post the input fields within that submitted form.

You will need to add the image data as a hidden field in the form

Output:

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='img_url' value='{$data->images->thumbnail->url}'">;
    echo "<input type='submit' name='submit' value='Click Me'>";  
    echo "</form>";
}

Then for processing the post:

...
$hostname = "random";
$username = "random";
$dbname = "random";
$password = "random!";
$usertable = "random";
$pictureImage = file_get_contents($_POST['img_url']);
...
Chris Campbell
  • 182
  • 1
  • 1
  • 6