I have been searching for the solution for so long with no success.
I set a div with a foreach that displays pictures with their respective caption, which works perfectly, and I also set a delete option but when I click on the image I want to delete, it only retrieves the last value in the div (let's say I have 10 images and I want to delete the image number 3, it gives me the value of the image number 10).
Here is my code that works fine to display the images:
$a1 = new ArrayIterator($newrow);
$a2 = new ArrayIterator($newrowcaption);
$it = new MultipleIterator;
$it->attachIterator($a1);
$it->attachIterator($a2);
foreach($it as $e =>$ekey) {
?><form action ='' method='POST'>
<?php
echo '<div class="boxpic">'."<img src='../../pictures/pics/{$ekey[0]}' height='120' width='auto' />"."<br />". $ekey[1]."<input type='submit' class='deleteinput' name='deletepic' . ' value='Delete' >"."<br />".'</div>';
}
And here is my attempt to retrieve the value from a specific image
if(isset($_POST['deletepic'])){
foreach ($ekey as $ekey1=>$newvalue) {
var_dump($newvalue);
}
}
I can't find the solution to get the current value of the image I want to delete. (Note: I am using var_dump since I wanted to see the output) Can you please give me some hints?
foreach($it as $e =>$ekey) :
?>
<form action ='' method='POST'>
<div class="boxpic">
<img src='../../pictures/pics/<?php echo $ekey[0]; ?>' height='120' width='auto' />
<br />
<?php echo $ekey[1]; ?>
// i'm making a guess as to the pic id...is it in ekey[3]? anyway
// put the pic id here in the hidden input var for this form
// change $ekey[3] in this next line to the element of the unqiue id
<input type='hidden' name='deletepic' value='<?php echo $ekey[3]; ?>'>
<input type='submit' class='deleteinput' name='deletepic' value='Delete'>
<br />
</div>
</form>
?>
endforeach;
Related
I am creating a button that will allow an admin to verify the image from users:
I don't know how to get the image ID when I click accept or reject.
Here's my code:
<?php
while($row = mysqli_fetch_array($result)) {
echo "<div class='grid-item'><img src='unimages/{$row['un_image']}'
onclick=onClick(this) style='width:98%' class='verifyimage' />
<form method='post' action='adminverify.php'>
<input class='button1' type='submit' name='accept' value='✓'>
<input class='button2' type='submit' name='reject' value='✘'>
</form>
</div>
";
}
mysqli_close($db);
?>
</div>
If the admin accepts, then the image should move from table2 to table1.
I know using INSERT INTO and DELETE will work, but how do I get the id for my picture.
Table 1:
Table 2:
Mmmh, you can use GET method for easier script:
while($row = mysqli_fetch_array($result)) {
echo "
<a href='?&action=accept&id={$row['un_id']}'>Accept</a>
<a href='?&action=reject&id={$row['un_id']}'>Reject</a>
";
}
And you use it like :
if(isset($_GET['action']) && isset($_GET['id'])) {
$image_id = $_GET['id']
// Then check if you must accept or reject with $_GET['action'] value
}
I'd add a hidden input that will be sent with the form:
echo "<div .....
<form....>
<input type='hidden' name='imageId' value='{$row['un_id']}'>
....
</form></div>";
You'll then have it in your receiving php script as
$image_id = $_POST['imageId'];
I have been searching for help from various forums and similar posts, but without any progress.
I have three pages, one that lets me insert information about projects into my database, a second that show the images and names of every project in the database, and a third page which I want to have a function that shows the image and name of the selected project in the second page.
Code on the second page(dashboardadmin.php):
<?php
ini_set('display_errors',1);
error_reporting(E_ALL);
$conn = mysqli_connect("localhost","root","","wildfire");
if(mysqli_connect_errno())
{
echo mysqli_connect_error();
}
$sql= "SELECT pid, project_name, image, image_type FROM project";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
while($row = $result->fetch_array()) {
echo "<form action='omprojekt.php' method='post'>
<div id='comp' name='comp'>
<img src=pic.php?pid=".$row['pid']." width=100xp height=100xp/>"." ".$row['project_name']."
</div>
<input type='submit' name='submit' value='Choose' />
</form>";
}
}
else {
echo "0 results";
}
mysqli_close($conn);
?>
Code on the third page (omprojekt.php):
<?php
/* Tried both of the $val variables but of course only one at a time. This is only to show you what I have tried. */
$val = isset($_POST['comp']) ? $_POST['comp'] : '';
$val = $_POST['comp'];
if(isset($_POST['submit'])){
echo "$val";
}
?>
In the last code you can see that I have two $val variables, but I have only used one of them at a time in my codes. The purpose of showing both of them here is to show you that I have tried both of them.
What I want to do is to make the third page show the image and name of the selected project in the second page. As you see, I have tried to retrieve the content from the DIV using the same "name". The problem is that the third page(omprojekt.php) doesn't show any content at all, and not even any errors.
You're expecting the div to submit like an input, but it won't, because it's not an input. So put it in an input.
if ($result->num_rows > 0) {
while($row = $result->fetch_array()) {
// Don't use and id attribute because you're in a loop and you might have multiple id's with the same value.
echo "<form action='omprojekt.php' method='post'>
<div>
<img src=pic.php?pid=".$row['pid']." width=100xp height=100xp/>"." ".$row['project_name']."
</div>
<input type='hidden' name='pid' value='".$row['pid']."'>
<input type='hidden' name='project_name' value='".$row['project_name']."'>
<input type='submit' name='submit' value='Choose' />
</form>";
}
}
Then on the next page,
$val = (isset($_POST['pid']) && isset($_POST['project_name'])) ?
"<img src=pic.php?pid={$_POST['pid']} width=100xp height=100xp/> {$_POST['project_name']}" : '';
For the sake of completeness, there are a few other things wrong with your code.
1) The width and height attributes on the iamge should have quotes, and do not accept "px", they are just numbers. If you want to use "px" you should use style instead. <img src='' style='width:20px; height:20px;' />
2) You should be escaping user input before running it through your query.
Data will only be sent from a <form> to the action script if it exists in an <input...> HTML tag. You cannot pick data out of randon <DIV> tags etc.
So you could do this by using a hidden input field like this ( this is only one way )
if ($result->num_rows > 0) {
while($row = $result->fetch_array()) {
echo "<form action='omprojekt.php' method='post'>
<div>
<img src=pic.php?pid=".$row['pid'] .
" style="width:100px;height:100px" /> " .
$row['project_name']."
</div>
<input type='hidden' name='comp' value='" . $row['pid'] . "' />
<input type='submit' name='submit' value='Choose' />
</form>";
}
}
Now when you get to omprojekt.php the $_POST['comp'] variable will exist.
You can have as many hidden input fields as you like so if you want to pass the project_name as well just add another hidden field.
I dynamically create html forms with a loop in php and each submit button is assigned a name as part of an array. How can I check which submit button is set and get its value? I tried this code but it doesn't work.
<?php
if($count_eksp){for($i=0; $i<$count_eksp; $i++){
$fusha_eksp = mysql_fetch_row($query1);
echo "<br>$fusha_eksp[2] $fusha_eksp[3]<form method='post' action='<?php echo $_SERVER['PHP_SELF']; ?>'><input name='eksp_edit[]' type='submit' value='$fusha_eksp[0]' height='20' width='20' ><input id='eksp_fshi[]' type='image' src='fshi.png' height='20' width='20'></form>";
}}
?>
<?php
if(isset($_POST['eksp_edit[]'])){
foreach($_POST['eksp_edit'] as $id){
$query = mysql_query("DELETE FROM `fusha_ekspertizes` WHERE `id`='$id'", $db_server);
}
}
?>
You have error in if condition, in the key. Remove [] for that.
if (isset($_POST['eksp_edit'])) {
...
}
The second error is in action attribute, you can have another <? inside echo. When the form is processed on the same page, you can leave this attribute.
echo "[...]<form method='post'><input [...]";
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.
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]')";
}
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>
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.
When i run into a glitch, I always find find the answer on StackOverflow, but this time, although I'm sure the fix is easy, I just can't seem to get it right !
Basically, i'm trying to add a "delete" button next to each row fetched from my mysql database. The users should be able to delete a specific post, if needed.
When i hit the delete button, it's always the latest row that gets deleted. So i guess there's something wrong with the value passed in each row : seems like they're overridden by the latest one.
Below's my code:
<?php
$table = query("SELECT post, postid FROM post_list WHERE id = ? ORDER BY
time DESC LIMIT 15", $_SESSION["id"]);
foreach ($table as $row)
{
$post = $row["post"];
$postid = $row["postid"];
echo ("<table>");
echo ("<tr>");
echo("<td>" . $post . "</td>");
echo("</td>")?>
<div id="posteraser">
<form action='' method='post'>
<input type='hidden' name='postid' value='<?php echo $postid?>'>
<input type='submit' name='posteraser'>Delete</input>
</form>
</div>
<?php
echo ("</td>");
echo ("</tr>");
echo ("</table>");
echo '<hr>';
}
?>
And below on the same page, there's the delete button code:
<?php
if(isset($_POST['posteraser']))
{
$sql = query("DELETE FROM post_list WHERE postid = '$postid' ");
redirect ('home.php');
}
?>
Any help/tips will be much appreciated !
Thanks a lot !
You have to pass here the $_POST['postid']
if(isset($_POST['posteraser'])){
$postid = $_POST['postid'];
$sql = query("DELETE FROM post_list WHERE postid = '$postid' ");
redirect ('home.php');
}
OR as procedure way
$sql = query("DELETE FROM post_list WHERE postid = ? ",$postid);
A developer should always be aware of the HTML code they create with their PHP code.
It's essential thing.
As a matter of fact, HTML code is the very result of our efforts. NOT nice picture on can see in the browser windows - it's browser's job - but the very HTML code.
So, if you bother to see into generated code, you would discover something that can be boiled down to
<input type='hidden' name='postid' value='1'>
<input type='hidden' name='postid' value='3'>
<input type='hidden' name='postid' value='4'>
<input type='hidden' name='postid' value='5'>
<input type='hidden' name='postid' value='9'>
Do you have any questions why you have only last value?
Speaking of solutions, you have two choices
create a separate form for the every row
mark the very Delete button with id.
<input type='submit' name='posteraser[<?php echo $postid?>]'>Delete</input>
for example
let's check the logic from select statement.
you are selecting postid and assigning it to a hidden element and when you press delete button
that hidden id is sent to server.
so form creating under for loop is
<div id="posteraser">
<form action='' method='post'>
<input type='hidden' name='postid' value='<?php echo $postid?>'>
<input type='submit' name='posteraser'>Delete</input>
</form>
</div>
but hidden element is creating with same name for each row.
so when you press delete button . first hidden id is sent to server.
and this hidden id is already newest as from your select statement.
so what's the solution for it..
either you should sent postid through get attaching it in your url so that you can identify
which delete button is pressed.
or create a logic to send only that id on which delete is pressed.
This looks wrong:
echo ("<tr>");
echo("<td>" . $post . "</td>");
echo("</td>")?>
The trailing </td> shouldn't be there. Something else perhaps?
Also, you don't show how postid gets into $_SESSION['id']