Remove Mysql registry with PHP

Asked

Viewed 277 times

0

I have a PHP page with Mysql connection. Everything works perfectly, however I have a DELETE button, which when clicking on it, takes the ID of that entry, goes to a page del.php and should remove this entry in Mysql. However, "it seems" that when you click on delete, nothing happens. If you hover over the delete icon, it returns the ID of each record perfectly. Can someone help me understand what I’m doing wrong?

Code where returns the data to index.php

 <?php  
    while($row = $stm->fetch())  {




   echo "<tr>"."<td><input type=checkbox name='check[]' value='[]' ></td>"."</td><td>" .$row['ip']. "</td><td>". $row['hostname'] . "</td><td>" . $row['sender'] . "</td><td>" . $row['subject'] . "</td><td>" .$row['quantidade']. " <td><a href=del.php?id=". $row['id'] . " data-placement='top' data-toggle='tooltip' title='Delete'><button class='btn btn-danger btn-xs' data-title='Delete' data-toggle='modal' data-target='#delete' ><span class='glyphicon glyphicon-trash'></span></button></p>
</tr>" .'' ;
}


?>

Contents of del.php

<?php

    // Dados da conexão com o banco de dados
    define('SERVER', 'xxxxx');
    define('DBNAME', 'yyyy');
    define('USER', 'qqqq');
    define('PASSWORD', '');

     if (isset($_GET['id']))
        $id = $_GET['id'];
     else
        $tmpString = null;


$opcoes = array(PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES UTF8');
$conexao = new PDO("mysql:host=".SERVER."; dbname=".DBNAME, USER, PASSWORD, $opcoes);

$sql  = "DELETE FROM spammer WHERE id=".$id;
$stm = $conexao->prepare($sql);
$stm->execute();


?>

1 answer

1

I found the bug. I had a form tag in my HTML that was generating this problem, removed it and started working.

I’m closing this question.

  • 1

    Note: Since you are using prepare, use bind to pass the ID, not concatenation. Do a search on "Prepared statements" right here on the site for more details

  • @Thanks for the tip. I will search yes.

Browser other questions tagged

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