Move image from one table to another using button - php

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

Related

Delete data from database using php and sql

So I have a php page that looks like this
<?php
echo "<table border='1' width= 300px >
<tr>
<th>Friend Names</th>
<th>Remove Friends</th>
</tr>";
while($row = mysqli_fetch_assoc($result2))
{
$friend_id_got = $row['friend_id2'];
$query3 = "SELECT profile_name
from friends
where friend_id = '$friend_id_got' ";
$result3 = $conn->query($query3);
$final3 = mysqli_fetch_assoc($result3);
echo "<tr>";
echo "<td>" . $final3['profile_name'] . "</td>";
echo "<td>"
?>
<form action="friendlist.php" method= "POST">
<button id="add-friend-btn" type= 'submit' name = 'submit'>Unfriend</button>
</form>
<?php
"</td>";
echo "</tr>";
}
echo "</table>";
When I press the button, I need the corresponding name to delete it. The only problem I'm facing is that How do I get the name corresponding to the button.
I think we need to relate the buttons and the name somehow, so when a specific button is pressed i get the corresponding name
From the question and comments, and a glance at your code it sounds like you probably actually need two pieces of data to be submitted to the server when your button is clicked:
The action which should be undertaken in response to the request (i.e. unfriending)
The ID of the person being unfriended
To achieve that you can add some hidden fields to your form. These are invisible to the user but will be available to PHP in the $_POST data when the form is submitted.
Something like this:
<form action="friendlist.php" method= "POST">
<input type="hidden" name="unfriend_id" value="<?=$friend_id_got ?>" />
<input type="hidden" name="action" value="unfriend" />
<button id="add-friend-btn" type="submit" name= "submit">Unfriend</button>
</form>
Following on from the comment from #ADyson:
<form action="friendlist.php" method= "POST">
<input type="hidden" name="cancel_id" value="<?=$friend_id_got ?>" />
<button id="add-friend-btn" type="submit" name="submit">Unfriend</button>
</form>
By including a hidden field in the form, you're able to store more information.
You can see that I'm storing the ID of the friend you're unfriending in the value of the hidden field, so when the form is submitted (the button is clicked) you'll have access to "cancel_id" in the POST data, which will obviously contain the ID of the friend to unfriend.

Download link for each form?

I have set a site where I display small panels have some text on it and download button, all these data are linked from mysqli I successfully printed them but How do I Do like if button click open a link from the database depends on the button clicked,
I'm developing a shop by the way, So How do I achieve this? Here's my code also:
<?php
$query = "SELECT * FROM `combolist`";
$results = mysqli_query($db, $query);
if ($results)
{
while($row = mysqli_fetch_array($results))
{
echo "<div class='panel panel-primary'>";
echo "<div class='panel-heading'><span class='glyphicon glyphicon-list-alt'></span><b> ".$row['comboTitle']."</b> ".$row['comboDesc']."";
echo "<form>
<button type='submit' name='purchase' class='btn btn-default btn-block'>Download 2¢</button>
</form><b>".$row['addedDate']."</b> - Added Date";
echo "</div>";
echo "</div>";
}
}
?>
<?php
if (isset($_GET['purchase']))
{
$inciar_sessiono = $_SESSION["username"];
if ($credits > 2) {
$credits--;
$do_update = "UPDATE `users` SET credits='$credits' WHERE username='$inciar_sessiono'";
$results = mysqli_query($db, $do_update);
// ON SUCCESS OPEN LINK FROM DATABAWES?
echo '<script type="text/javascript">
window.open("http://google.com");
</script>';
} else {
echo " no funds";
}
}
?>
<div class="panel panel-primary">
<div class="panel-heading"><span class="glyphicon glyphicon-list-alt"></span> <b>Combolists
</b>
<form>
<button type="submit" name="purchase" class="btn btn-default btn-block">Download 2¢</button>
</form>
</div>
</div>
If I understand your question correctly you need to identify which button the user is clicking on. If that is the case a hidden form field would be a good solution.
<input type="hidden" id="yourid" name="downloadlink_id" value="2">
Then you can use the value of the hidden field, in my case "2" and fetch the value using $_GET['downloadlink_id'] in a mysqli query to get the value and use it in a mysqli query.
Like this:
SELECT comboDownloadLink FROM table_name WHERE id=2
Edit: You can get the correct id and print it in your while-loop:
echo '<form>';
echo '<input type="hidden" id="yourid" name="downloadlink_id" value=".$row["id"]">';
So I suppose you have a link in your DB. You can add link it by $row['comboDownloadLink'] . So now if you want to add it to the button you can place the button into an <a> tag like <a href='".$row['comboDownloadLink']."' target='_blank'><button ... > </a> . Now it's redirecting your user to a blank page and starting the download.

How to send data that is not in a textbox via post in PHP

I want to send the id no. in a form so that i can delete the specific row, how do I do it?
...
$query = "SELECT id, text1, file FROM dynamic";
$resultObj = $connection->query($query);
...
<?php while($row = $resultObj->fetch_assoc()): ?>
<p><?=$row['text1']?><br>
<?php echo "<img src=".$row['file']." height=200 width=300 />"?>
</p><form action="server.php" method="post"> <button type="submit" name="delete_data">Delete</button></form>
<br> <br>
<?php endWhile; ?>
...
The button should then delete the specific entry only
You can place all into one form. Names can have brackets generating an array. Buttons can have a value.
<button type='submit' name='delete_data[]' value='{$row['file']}'>Delete</button>
Consider doing this with checkboxes and one button 'Delete checked items'.

Retrieve DIV-content inside a Form

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.

PHP Mysql - Delete button keeps on deleting latest row

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']

Categories