0

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'");
}

3 Answers 3

2

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.

Sign up to request clarification or add additional context in comments.

2 Comments

+1 name="goosedown_id" value="'.$row['goosedown_id'].'" now you've got it ;-)
:), I made a mistake when I cleaned the code and accidentally deleted that part.
1

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'] . '" /> ?>

1 Comment

cool thank you.. however i do get an error on this line echo "<td><input type="hidden" name="goosedown_id" value="'.$row['goosedown_id'].'" /></td>"; the error says Parse error: syntax error, unexpected T_STRING, expecting ',' or ';' in
0

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".

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.