我的表格有列:
tags_id |标签| USER_ID
1 | android | 7
2 | windows | 7
3 | windows | 2
4 | mac | 7
5 | windows | 1
6 | windows | 3
这是我的PHP代码:
<?php
if($_SERVER['REQUEST_METHOD']=='POST'){
//Getting values
$user_id = $_POST['user_id'];
//Creating an sql query
$sql = "DELETE FROM user_tags WHERE user_id = '$user_id'";
//Importing our db connection script
require_once('dbConnect.php');
//Executing query to database
if(mysqli_query($con,$sql)){
echo 'Tags updated';
}else{
echo 'Tags could not be update';
}
//Closing the database
mysqli_close($con);
}
答案 0 :(得分:2)
因为用户字段的名称,所以它是错误的。你使用了users_id而不是user_id。见下文:
在:
$sql = "DELETE FROM user_tags WHERE users_id = '$user_id'";
后:
$sql = "DELETE FROM user_tags WHERE user_id = '$user_id'";
还有一个提示:使用预备语句。请参阅此处的链接:http://www.w3schools.com/php/php_mysql_prepared_statements.asp
答案 1 :(得分:2)
使用始终准备好的声明来防止注射
<?php
require_once('dbConnect.php');
$delete_user_tags = $con->prepare(" DELETE from user_tags WHERE user_id = ? ");
$delete_user_tags->bind_param("i", $user_id);
if (isset($_POST['user_id'];)) {
$user_id = $_POST['user_id'];
if (!$delete_user_tags->execute()) {
echo ('Tags could not be update');
} else {
echo ('Tags updated!');
}
}