DELETE FROM table WHERE ID='$id' — Variable refuses to stick - php

Trying to perform a very simple task here.
I have an <ol> that contains 4 rows of data in some handy <li>s. I want to add a delete button to remove the row from the table. The script in delete.php appears to have finished, but the row is never removed when I go back and check dashboard.php and PHPMyAdmin for the listing.
Here's the code for the delete button (inside PHP):
Print "<form action=delete.php method=POST><input name=".$info['ID']." type=hidden><input type=submit name=submit value=Remove></form>";
Moving on to delete.php:
<?
//initilize PHP
if($_POST['submit']) //If submit is hit
{
//then connect as user
//change user and password to your mySQL name and password
mysql_connect("mysql.***.com","***","***") or die(mysql_error());
//select which database you want to edit
mysql_select_db("shpdb") or die(mysql_error());
//convert all the posts to variables:
$id = $_POST['ID'];
$result=mysql_query("DELETE FROM savannah WHERE ID='$id'") or die(mysql_error());
//confirm
echo "Patient removed. <a href=dashboard.php>Return to Dashboard</a>";
}
?>
Database is: shpdb
Table is: savannah
Ideas?

It's refusing to stick because you're calling it one thing and getting it with another. Change:
"<input name=".$info['ID']." type=hidden>"
to
"<input name=ID value=".$info['ID']." type=hidden>"
because in delete.php you're trying to access it with:
$id = $_POST['ID'];
You should really quote attribute values as well ie:
print <<<END
form action="delete.php" method="post">
<input type="hidden" name="ID" value="$info[ID]">
<input type="submit" name="submit" value="Remove">
</form>
END;
or even:
?>
form action="delete.php" method="post">
<input type="hidden" name="ID" value="<?php echo $info['ID'] ?>">
<input type="submit" name="submit" value="Remove">
</form>
<?

Please, for the love of the web, don't built an SQL query yourself. Use PDO.

Just another point I'd like to make. I'm 95% sure that you can't give an input a numeric name/id attribute. It has to be like "id_1" not "1".
Also with php you can do arrays.
So you could do this
<input name="delete[2]">
then in your php
if(isset($_POST['delete']))
foreach($_POST['delete'] as $key=>$val)
if($_POST['delete'][$key]) delete from table where id = $val

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.

Get specific data after output everything from database

I've got here a table with all the data from my database. On the end on every row, I have put a checkbox with the value 1, to update the 'accept' status in my database, which is default by 0.
My problem is, that the value of a ticked checkbox should update the 'accept' status ONLY in the entry in its row after the submit button got pressed.
So basically I need to check if the checkbox is ticked, and if its ticked, the 'accept' status in the row of the checkbox gets the value of 1. For that, I think I need to get the 'match_id' of the row.
This is how it looks on the website:
This is the table:
mysql_connect("localhost", "root", "") or die(mysql_error());
mysql_select_db('lr') or die(mysql_error());
$result = mysql_query("SELECT * FROM `challenge`") or die(mysql_error());
echo "<table class='match-table'>";
echo "<thead><tr> <th><h1>match_id</h1></th> <th><h1>Team</h1></th><th><h1>Accept Status</h1></th><th><h1>Accept</h1></th> </tr></thead><tbody>";
while($row = mysql_fetch_array($result)){
echo "<tr><td>";
echo $row['match_id'];
echo "</td><td>";
echo $row['team'];
echo "</td><td>";
echo $row['accept'];
echo "</td><td>";
?>
<form action="" method="post">
<input type="checkbox" name="challenge_accept" value="1"/>
<?php
echo "</td></tr>";
}
echo "</tbody></table>";
?>
<input type="submit" value="Save"/>
<input type="reset" value="Delete"/>
</form>
Any suggestions how I can do that?
Your HTML is utterly invalid, with MULTIPLE <form> being opened and only one </form> at the end of the page. You're also splitting your tags into garbage like
<tr><td><form></td></tr>
[many repeats]
</table>
</form>
Your form fields also don't include ANY method of identifying which row was actually clicked on, e.g. <input type="submit" name="selected_row" value="<?php echo $row['id'] ?>" />. As it stands right now, you've hardcoded the value 1, so no matter which of however many buttons you spit out, they'll ALL be sending 1 back to your code.

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

request in while loop

I work with this code:
<form method="get" name="MobileDetails">
<input name="brand" id="brand" value="<?php echo $brand;?>" type="hidden">
<input name="brid" id="brid" value="<?php echo $brandid;?>" type="hidden">
<button type="button" name="submitButton" value="get Details" onclick="getDetails()">
</form>
java script
<script type="text/javascript">
function getDetails(){
var brand = document.getElementById('brand').value;
var brandid = document.getElementById('brid').value;
document.MobileDetails.action = 'details.php?brand='+brand+'&id='+brandid;
document.MobileDetails.submit();
}
</script>
But it does not work in while loop. Whats the problem? My code is given below.
When i click on the button it do not do anything. But the code work great with out while loop given on the top.
<?php
require_once('connection.php');
$SQL= "SELECT*FROM mobile ORDER BY price ASC LIMIT 10";
$result= mysql_query($SQL);
while ($db_field = mysql_fetch_assoc($result)){
$brand=$db_field['brand'];
$id=$db_field['id'];
$model=$db_field['model'];
echo "<form method='get' name='MobileDetails'>";
echo " <input name='brand' id='brand' value='". $brand ."' type='hidden'>";
echo" <input name='brid' id='brid' value='". $id ."' type='hidden'>";
echo" <input name='mod' id='mod' value='". $model ."' type='hidden'>";
echo" <button type='button' name='submitButton' value='get Details' onclick='getDetails()'/>
</form> ";
echo "CLICK HERE";
}
?>
You're using several times the same id. Ids have to be unique.
Uou are dealing with multiple id's. The job of an ID is to be unique identifier for the element. I suggest just using
<form action="details.php" type="get">
this will do exactly what you are trying to achieve without using the function.
The thing with element ID's is that they need to be unique for the page; however, as you may see, not required for HTML to be displayed. When calling your JS function getDetails(), it grabs the element by ID but when you have multiple ID's in the page, this will fail.
So what can you do? Well, in your loop, you create a new form for each 'brand'. You can pass a reference of the form to the grabdetails and then, by NAME, grab the values from that form.
Rather than using Javascript to generate a link based on given details put in a hidden field, you should just generate the action at the PHP level.
echo "<form method='get' name='MobileDetails' action='details.php?brand=$brand&id=$brandid'>";
But since you do have hidden fields, using just action='details.php' the form will take the user to
details.php?brand={brand}&brid={id}&mod={model}
You should look into POST or making your button into a plain link rather than having a form.

Checking Which Button was Clicked

I have a PHP generated form which consists of a list of items, each with a button next to them saying "Remove This" it outputs similar to below:
Item A - [Remove This]
Item B - [Remove This]
...
I wish to be able to click Remove This and it will detect which item it is, and then remove that from the database. Here's my code so far:
selectPlaces.php
<?php
include 'data.php';
mysql_connect($host, $user, $pass) or die ("Wrong Information");
mysql_select_db($db) or die("Wrong Database");
$result = mysql_query("SELECT * FROM reseller_addresses") or die ("Broken Query");
while($row = mysql_fetch_array($result)){
$placeName = stripslashes($row['b_name']);
$placeCode = stripslashes($row['b_code']);
$placeTown = stripslashes($row['b_town']);
$outputPlaces .= "<strong>$letter:</strong> $placeName, $placeTown, $placeCode <input type=\"button\" onclick=\"removePlace()\" value=\"Remove This\" /><br />";
}
mysql_close();
?>
Coupled with my admin.php
<div id="content" style="display:none">
Remove a Place<br><br>
<?php include 'selectPlaces.php'; echo $outputPlaces; ?>
</div>
I know I need to add some javascript to detect which button is clicked but I can't seem to get it working. I tried modifying the onclick="removePlace()" by perhaps passing a variable in the function removePlace(placeID) or something like that, but I'm new to JavaScript and I have no idea how to receive this in the removePlace function.
This seems easier to do without JavaScript. For each entry instead of generating just a button, generate a form that posts to a PHP script that does the deleting.
<form action="deletePlace.php?id=<?php echo $idOfThePlace?>">
<input type="submit" value="Remove This" />
</form>
$idOfThePlace would be the ID with you use to identify the data row.
You don't need JavaScript for that. Try running this example:
<?php var_dump($_POST); ?>
<form action="post.php" method="post">
<p>
<input type="submit" value="a" name="action" />
<input type="submit" value="b" name="action" />
</p>
</form>
You'll see that $_POST['action'] will depend on which button was pressed. For your example, you just need to set the value to identify the item that needs to be deleted. It might be useful to use the <button> element for that: <button name="delete" type="submit" value="12345">delete item 12345</button>. It'll show up as $_POST['delete'] with 12345 as value when submitted.

Categories