asdasdasd
asdasdasd

Reputation: 31

Inserting Specific Image for MYSQL Database

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?

Upvotes: 3

Views: 401

Answers (3)

Maximus2012
Maximus2012

Reputation: 1819

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.

Upvotes: 2

Chris Campbell
Chris Campbell

Reputation: 182

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

Upvotes: 0

Valk6
Valk6

Reputation: 96

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.

Upvotes: 0

Related Questions