delete files from table

Asked

Viewed 397 times

0

I’m trying to delete an item from the table, but when I click it doesn’t delete. Recognizes the id and everything, only when I update the item has not been deleted.

<?php
     include_once("conexao.php");
    if(! $conn ) {
      die('Could not connect: ' . mysqli_error());
   }
   echo 'conexão feita com sucesso<br>';
   $sql = 'DELETE FROM fornec WHERE id = "$row"';

   if (mysqli_query($conn, $sql)) {
      echo "Deletado com sucesso";
   } else {
      echo "Error: " . mysqli_error($conn);
   }
   mysqli_close($conn);
?>

It looks like this. As I mentioned before, it recognizes the id I clickei, but does not delete.

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<?php
        $titulo = "Controle &raquo; Listar Material";
        require_once ("includes/header.php"); 
    ?>
</head>
<title> </title>
<style>
table {
    border-collapse: collapse;
    width: 100%;
}

th, td {
    text-align: left;
    padding: 8px;
}

tr:nth-child(even){background-color: #f2f2f2}

th {
    background-color: #4CAF50;
    color: white;
}
</style>
</head>
<body>
                <?php
    require_once('includes/testes.php');
    ?>


<?php
// Conexão ao banco
//require_once('conexao.php');
?>
<div id="principal">
                <form action="<?php $_SERVER['PHP_SELF']?>" method="post">
                    <fieldset>


<?php
require_once('conexao.php');


echo"<table width='100%' border='1'>";

echo '<table>';
echo '<tr>';
echo'<td>id</td>';
echo'<td>NOME</td>';
echo'<td>TELEFONE</td>';
echo'<td>Editar</td>';
echo'<td>Excluir</td>';
echo '</tr>';
$res = mysqli_query($conn, "SELECT * FROM fornec");


while($row = mysqli_fetch_array($res)){

echo '<tr>';
echo '<td>'.$row["id"].'</td>';
echo '<td>'.$row["NOME"].'</td>';
echo '<td>'.$row["FONE_2"].'</td>';
//echo '<td><a href=\"editar_fornec.php?id'.$row["id"].' \">✍</a>';
echo '<td><a href="editar_fornec.php?id='.$row['id'].'">✍</a>
      <td><a href="deleta2.php?id='.$row['id'].'">✖</a>
      </tr>';
}
echo '</table>';                

?>
<!-- a href ="listar_fornec.php?delete=<?php //echo $id; ?>">Delete</a>
-->

    </fieldset>             
        </form>
            </div> <!-- Fim da div#principal -->
</body>

</html>
  • Check that no foreign key is being used in other tables

  • You have to pass the id to delete per post. You are comparing the id to $row, but where you are setting the value of $row?

1 answer

0

Changes your code to debug.

If you have returned the 23000 code or integrity message may be foreign key. Also take a look at the permissions you have to manipulate the bank.

$sql = 'DELETE FROM fornec WHERE id = "$row"'; 
$result = mysqli_query($conn, $sql);
echo $result; //debug

if ($result)) { echo "Deletado com sucesso"; } else { echo "Error: " . mysqli_error($conn); } mysqli_close($conn); ?>

Browser other questions tagged

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