php - Inserting Specific Image for MYSQL Database -
i creating application grabs list of instagram images , inserts ones (based on choice) specific database.
currently, when select submit button grabbing last available image in list , inserting database. each image assigned submit button. how make sure proper image, relative submit button, 1 being inserted database.
the following code list of images , submit button each image:
foreach ($media->data $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 how inserting image database. remember, grabs last available image in list , inserts that.
if(isset($_post['submit'])) { // there variables database information $hostname = "random"; $username = "random"; $dbname = "random"; $password = "random!"; $usertable = "random"; //connecting database $con = mysql_connect($hostname, $username, $password) or die ("unable connect database! please try again later."); mysql_select_db($dbname, $con); $sql="insert $usertable (image) values ('$pictureimage')"; if (!mysql_query($sql,$con)) { die('error: ' . mysql_error($con)); } mysql_close($con); }
any suggestions?
change form this:
echo "<form action='tag.php' method='post'>"; foreach ($media->data $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 process $image[] array on backend.
foreach ($_post['image'] $k=>$image){ $sql="insert $usertable (image) values ('$image')"; if (!mysql_query($sql,$con)) { die('error: ' . mysqli_error($con)); } }
you might need make few more small changes code logic should work.
update: since op wants 1 button each image, code that:
foreach ($media->data $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 changed to:
$sql="insert $usertable (image) values ('".file_get_contents($_post['image'])."')"; if (!mysql_query($sql,$con)) { die('error: ' . mysqli_error($con)); }
this should insert actual image image in database op wants.
Comments
Post a Comment