Delete record by ID

2

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

File form.php :

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

File 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 I did not succeed.

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 do I delete a record by its ID?

    
asked by anonymous 22.04.2014 / 01:32

2 answers

4

I made a post for a question Login to PHP with Permission Levels which shows as I should proceed with POST, Mysql, PDO, Mysqli, and filter_input for better security, but I've been following your example to understand input, form, and hidden relationship with PHP. It would be very cool after understanding how this exclusion happens, to follow the normalization of coding, as above link

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

In the example I'm passing this form contains a hidden with id name with value of 1, then clicking the delete button will submit the form and execute 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";
}
    
22.04.2014 / 01:39
2
$del = "DELETE FROM autoriza WHERE ID =".$_POST['id'];
$delgo = mysql_query($del) or die('Erro ao deletar');
echo "deletado";

Create a coding to see if the id was sent, or use if (isset ($ _ POST ['id']).

And use mysqli, as mysql_ is being deprecated in the new PHP version.

    
22.04.2014 / 16:02