删除与用户名相关的行

Delete row(s) with respect to username

我已经解决了大部分与此类似的问题,但 none 解决了我的问题。 我有 table 四列:idusernametitle日期。我想在用户单击按钮(锚标记)时删除与特定用户名关联的整行。请问,我该如何实现?这是我试过的代码。

php

<?php 
session_start();

$uname = $_SESSION['username'];
$dbconn = mysqli_connect('localhost','root','','notesdb');
if(!$dbconn){
 die("Connection failed:". mysqli_connect_error($dbconn));
}
if($stmt = $dbconn->prepare("DELETE * FROM notes_log where username = ? ")){
 $stmt->bind_param("s",$uname);
 $stmt->execute();
 $stmt->close();
}else{
 echo "ERROR: could not prepare SQL statement.";
}
mysqli_close();
// redirect user after delete is successful
header("Location: index.php");

?>

HTML

<a href="deleteAll.php" onclick="return confirm('Are you sure?')">Delete all</a>

以上代码重定向了页面,但没有删除任何内容。

去掉查询中的*。语法只是:

DELETE FROM notes_log where username = ?

DELETE Syntax

在多 table DELETE 中,您需要将 table 名称放在 DELETE 之后,但在单 table DELETE 之后] 那里应该什么都没有。

并且当 SQL 操作失败时,您应该打印 SQL 错误消息,而不仅仅是 could not prepare SQL statement,例如

echo "ERROR: could not prepare SQL statement: " . $dbconn->error;

编辑:mysqli_close() 需要数据库连接作为其唯一参数。

参考:http://php.net/manual/en/mysqli.close.php

您将需要使用 mysqli_close($dbconn)