我正在尝试使用PHP从MySQL数据库中删除一行。我可以运行PHP脚本,但不会删除该行。它运行else语句“错误删除记录”。这是我网站上的表格中的代码:
echo "<table class='table table-striped'>";
echo "<thead class='table-dark'>";
echo "<tr>";
echo '<th scope="col">Delete</th>';
echo '<th scope="col">Username</th>';
echo '<th scope="col">Institution</th>';
echo "</tr>";
echo "</thead>";
echo "<tbody>";
$map_user_sql = "SELECT * from oer_map_users ORDER BY username ASC";
$res_data = mysqli_query($conn,$map_user_sql);
while($row2 = mysqli_fetch_array($res_data)){
echo "<tr>";
echo "<td><a class='btn btn-success' href='delete_user.php?user=" .$row2['username']. "'>Delete</a></td>";
echo "<td>" . $row2['username'] . "</td>";
echo "<td>" . $row2['Institution'] . "</td>";
echo "</tr>";
}
mysqli_close($conn2);
echo "</tbody>";
echo "</table>";
这是我的delete_user.php文件中的代码:
<?php
$filename = "../../sql_config.ini";
$ini_array = parse_ini_file ($filename);
$servername = $ini_array["servername"];
$username = $ini_array["username"];
$password = $ini_array["password"];
$dbname = $ini_array["dbname"];
$user = $_GET['username'];
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
// sql to delete a record
$sql = "DELETE FROM oer_map_users WHERE username=$user";
if (mysqli_query($conn, $sql)) {
mysqli_close($conn);
header('Location: select_users.php');
exit;
} else {
echo "Error deleting record";
}
?>