删除PHP中的表记录

时间:2018-11-19 14:06:06

标签: php html mysql

在我创建删除按钮的同时:

while($rows_cursos = mysqli_fetch_array($resultado_cursos)) {   
$tabela2 .= '<tr>';
$tabela2 .= '<td>'.$rows_cursos['IdEstado'].'</td>';
$tabela2 .= '<td>'.$rows_cursos['DataRequis'].'</td>';
$tabela2 .= '<td>'.$rows_cursos['Funcionario'].'</td>';
$tabela2 .= '<td><form action="./delete" method="post">'; 
$tabela2 .= '<input type="hidden" name="IdEstado" value="'.$rows_cursos['IdEstado'].'">'; 
$tabela2 .= '<button type="button" class="btn btn-danger">Excluir</button>'; //aqui está o seu botão
$tabela2 .= '</form></td>'; //só fechando o form
$tabela2 .= '</tr>'; 
    }
$tabela2 .= '</tr>';
$tabela2 .='</tbody>'; 
$tabela2 .= '</table>';
$tabela2 .= '</div>';
echo $tabela2;

action = "./delete"之所以这样说是因为我正在使用wordpress,这种形式的action等于action = "delete.php"

delete.php之后,我有以下代码:

$id = $_POST['IdEstado'];

$sql = "DELETE FROM `centrodb`.`RequisicaoLuvas` WHERE `RequisicaoLuvas`.`id` = " . $id;
mysqli_query($conn,$sql) or die("Erro ao tentar cadastrar registro");
mysqli_close($conn);

$sql1 = "DELETE FROM `centrodb`.`EstadoLuvas` WHERE `EstadoLuvas`.`IdEstado` = " . $id;
mysqli_query($conn,$sql1) or die("Erro ao tentar cadastrar registro");
mysqli_close($conn);

当我单击删除按钮时,它不会删除数据库表中的任何内容。

1 个答案:

答案 0 :(得分:0)

使用PDO可以轻松实现,并且避免使用SQL Inyection,如果您想使用mysqli,则可以执行类似的操作。请点击以下链接:http://php.net/manual/es/mysqli-stmt.bind-param.php

<?php
 $id = $_POST['IdEstado'];
 $pdo = new PDO("mysql:host=" . $dataBaseHost . "; dbname=" . $dataBaseName . "", $dataBaseUserName, $dataBasePassword);
 $prepared_statement = $pdo->prepare("DELETE FROM RequisicaoLuvas WHERE id=:id");
 $prepared_statement->bindParam(':id', $id);
 $prepared_statement->execute();
 $pdo = null;
 ?>