我正在尝试从数据库中删除整行,它具有名为“ students”的表,当我运行删除查询时它包括四列,它仅删除两行 列数据RegNo和Name。可能是什么原因?
$names = [
['reg'=> $_POST['s1_id'], 'reg_set'=> $_POST['s1_id_set'] ,'name'=> $_POST['s1_name']],
['reg'=> $_POST['s2_id'], 'reg_set'=> $_POST['s2_id_set'],'name'=> $_POST['s2_name']],
['reg'=> $_POST['s3_id'], 'reg_set'=> $_POST['s3_id_set'],'name'=> $_POST['s3_name']]
];
$query_delete="delete from students where RegNo=:reg And ProjectID='$id'";
$query_update="update students SET Name=:Name, RegNo=:reg_set WHERE
RegNo=:reg And ProjectID='$id'";
foreach ( $names as $name)
{
try
{
if(!isset($name['reg_set']) && !isset($name['name']) )
{
$stmt = $conn->prepare( $query_delete );
$stmt->bindParam(':Name', $name['name']);
$stmt->bindParam(':reg', $name['reg']);
$result = $stmt->execute();
$msg = "Record deleted";
}
else
{
$stmt = $conn->prepare( $query_update );
$stmt->bindParam(':Name', $name['name']);
$stmt->bindParam(':reg', $name['reg']);
$stmt->bindParam(':reg_set', $name['reg_set']);
$result = $stmt->execute();
$msg = "Record updated";
}
//header("location:adminhome.php");
}
catch(PDOException $ex)
{
$msg = $ex -> getMessage();
}
}
答案 0 :(得分:0)
您的逻辑没有道理。您检查是否设置了名称和reg,然后即使没有设置名称,也尝试在数据库查询中使用它们。从结果看来,您似乎试图用空数据或空数据更新该行,这就是为什么它们为空而不被删除的原因。
请显示$msg
是什么,然后重做您的逻辑。