尝试使用PHP / SQL查询从SQL表中删除tuble(用户名)时出错。我目前的代码如下:
<html lang="en" >
<body>
<?php
$conn = new mysqli('localhost', 'myUsername', 'myPassword', 'dbName') or die ('Cannot connect to db');
$uname=$_POST['uname'];
$conn->query("DELETE FROM Account WHERE username = $uname");
echo '<script type="text/javascript">';
echo 'alert("-ADMIN-\nRemoved user successfully!");';
echo 'window.location.href = "admin.php";';
echo '</script>';
?>
</body>
</html>
这个完全相同的代码用于从不同的表中删除网站中的帖子,但不是按用户名删除用户帐户。有什么想法吗?
答案 0 :(得分:1)
大概$uname
是一个字符串,列username
也是一个字符串,所以你需要引用字符串中的变量
$conn->query("DELETE FROM Account WHERE username = '$uname'");
然而,这会使您的代码对sql注入开放,因此您最好使用预准备语句
$stmt=$conn->prepare("DELETE FROM Account WHERE username = ?" );
$stmt->bind_param('s', $uname );
$stmt->execute();
答案 1 :(得分:0)
查看此解决方案
<?php
$conn = new mysqli('localhost', 'myUsername', 'myPassword', 'dbName') or die ('Cannot connect to db');
$stmt = $conn->prepare('DELETE FROM Account WHERE username = ?');
$stmt->bind_param('s', $_POST['uname']);
$stmt->execute();