First of all I am displaying a list of values in a table, What I am trying to do is add a delete button based off of the tables unique table id.
So I am trying to delete a value from my database using a this button i have declared, however with what I am doing at the moment it's just not deleting anything to the database.
This is my database table
CREATE TABLE `5050goosedown` (
`goosedown_id` int(11) unsigned NOT NULL auto_increment,
`name` varchar(100) NOT NULL default '',
`width` int(8),
`height` int(8),
`normal_fill` int(8),
`our_fill` int(8),
`old_price` DECIMAL(3,2),
`price` DECIMAL(3,2),
PRIMARY KEY (`goosedown_id`)
) TYPE=MyISAM;
this is my button, its inside a form which reloads this same page..
echo '<td><input type="submit" name="'.$row['goosedown_id'].'" value="Delete" /></td>"';
So this is a button that says Delete, and its name is the unique id of that table... (one of the several tabels I have and would like to do this on)
Then when the page reloads I have this if statement to capture the particular delete button being pressed... which is not working atm..
//DELETE QUERIES
if(isset($_POST['goosedown_id']) and is_numeric($_POST['goosedown_id']))
{
// here comes your delete query: use $_POST['deleteItem'] as your id
mysql_query("DELETE FROM 5050goosedown WHERE goosedown_id='goosedown_id'");
}
Change your HTML to:
echo '<td><form method="post" action=""><input type="hidden" name="goosedown_id" value="'.$row['goosedown_id'].'" /><input type="submit" name="sumbit" value="Delete" /></form></td>"';
and script:
if(isset($_POST['goosedown_id']) and is_numeric($_POST['goosedown_id'])){
mysql_query("DELETE FROM 5050goosedown WHERE goosedown_id=".(int)$_POST['goosedown_id']);
}
You do not pass the ID
with sumbit button as it's value is Delete
, you need to create an hidden field.
As goosedown_id
is defined as int(11) unsigned
you must not quote it in your query:
"DELETE FROM 5050goosedown WHERE goosedown_id=$goosedown_id"
// ^ ^ no quote
And of course $goosedown_id is the posted value from the form:
$goosedown_id = (int)$_POST['goosedown_id'];
// ^^^ prevents SQL Injection
So you have to do a 'fake' post, because the value cannot be in the submit:
<input type="hidden" name="goosedown_id" value="<?php echo $row['goosedown_id'] ?>" />
You can also write this as a complete php string:
<?php echo '<input type="hidden" name="goosedown_id" value="' . $row['goosedown_id'] . '" /> ?>
Try this query instead:
mysql_query("DELETE FROM 5050goosedown WHERE goosedown_id=".mysql_real_escape_string($_POST'goosedown_id'));
Also, make sure that the button is in fact inside a form
with method="post"
.