Delete record by ID

Asked

Viewed 19,537 times

2

I created a button to delete a record in the database with the following code:

Filing cabinet form.php:

<form action='deletar.php' method='post'>
<input type='submit' name='deletar' value='deletar' />

Filing cabinet deletar.php:

$del = "DELETE FROM autoriza WHERE ID = '' ";
$delgo = mysql_query($del) or die('Erro ao deletar');
echo "deletado";

I have tried some alternatives to collect the ID of each record, but was not successful.

What I found most was to create a variable ( $id = $_POST['id']; ) and use the variable in the mysql command (DELETE FROM... ID='$id').

How can I delete a record by its ID?

  • 3

    Function mysql_ are being deprecated... Use PDO or mysqli.

  • 1

    What do you mean, you want to delete a specific record without saying which one? You can explain better?

  • @bfavaretto: when the user sends a record through the form, it automatically generates an ID. I wanted to take this ID to put DELETE each record.

  • @Gustavocave It is not yet clear... You mean you just created a record but don’t know how to send it back to the corresponding delete button?

  • @bfavaretto exactly that!

  • Apparently you want to know something totally different from what you asked. Your question is about how to pass the browser id to the server. Apparently you already know how to do this, and the answer from Fccdias also explains. But what you really want to know is how to return a server ID to the browser right after creating a record. In this case it would be better to have a separate question (if you change this one, invalidates the answers you already have). Or I got it all wrong?

  • This is @bfavaretto. Just delete the record you are being asked for. In this case, use the registration ID to delete it.

  • I’ve managed to get it deleted by ID, but it’s picking up all the lines and then everything is deleted.

  • And I was also able to delete, but I always take the smaller ID and then delete it, regardless of which one I tried to delete.

  • I’m sorry Gustavo, but as much as you explain I can’t understand what you’ve done and what you can’t do.

  • @bfavaretto no problem! thank you for your try!

Show 6 more comments

2 answers

5

I did a post for a question PHP Login with Permission Levels that shows how it should proceed with $POST, Mysql*, PDO, Mysqli and filter_input for better security, but, I followed your example in order to understand input, form, Hidden relationship with PHP. It would be very nice after understanding how this deletion happens, to proceed to the normalization of the encoding, as link above

In the form should contain the field text or hidden with the value of the code you want to send to the script PHP perform the exclusion task via SQL bank MySql.

In the example I am passing you this form contains a Hidden with name id with value of 1, then by clicking the delete button it will submit the form and run the delete SQL

//Aqui o código id ta fixo (campo hidden)
<form action='deletar.php' method='post'>
    <input type='hidden' name='id' value='1'>
    <input type='submit' name='deletar' value='deletar' />
</form>

//Aqui você digita o código (campo text)
<form action='deletar.php' method='post'>
    <input type='text' name='id' value=''>
    <input type='submit' name='deletar' value='deletar' />
</form>

if (isset($_POST['id'] && is_numeric($_POST['id'])) {
     $del = "DELETE FROM autoriza WHERE ID = " . $_POST['id'];
     $delgo = mysql_query($del) or die('Erro ao deletar');
     echo "deletado";
}

3

$del = "DELETE FROM autoriza WHERE ID =".$_POST['id'];
$delgo = mysql_query($del) or die('Erro ao deletar');
echo "deletado";

Creates a codition to know if the id has been sent, or use if (isset($_POST['id'].

And use mysqli asmysql_ is being discontinued in the new version of PHP.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.