我的代码有什么问题,mysql,PDO prepare delete 语句?
What is wrong with my code, mysql, PDO prepare delete statement?
为什么我的代码不起作用。求助,我没有看到任何错误。如果我使用 user_id=$current_user 而不是 user_id=:current_id,它会起作用。但出于安全原因,我需要做好准备。请帮忙
<?php
$hostname = 'localhost';
$username = '**';
$password = '**';
$dbname = '**';
try {
$db = new PDO("mysql:host=$hostname;dbname=$dbname", "$username" ,"$password" ,
array(PDO::ATTR_EMULATE_PREPARES => false, PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION));
} catch (PDOException $e) {
echo" There was an error with the connection";
}
$current_user=$_POST['users_id'];//get user id
include 'db_tag.php';
$stmt=$db->prepare("DELETE FROM object_sign WHERE user_id=:current_id");
$stmt->bindParam(":current_id",$current_user,PDO::PARAM_INT);
$stmt->execute();
?>
@conan,使用下面我提到的代码,你需要用这段代码检查以下几点。
1) 打印 $current_user id 以确保 id 正确出现与否,另外我已经设置了条件 if post contain user id then only正在执行查询。
2) 我已经设置了异常,所以你可以通过异常消息检查错误。
<?php
include 'db_tag.php';
$hostname = 'localhost';
$username = '**';
$password = '**';
$dbname = '**';
try {
$db = new PDO("mysql:host=$hostname;dbname=$dbname", "$username" ,"$password" ,
array(PDO::ATTR_EMULATE_PREPARES => false, PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION));
if(isset($_POST['users_id'])) {
$current_user=$_POST['users_id'];//get user id
$stmt=$db->prepare("DELETE FROM object_sign WHERE user_id=:user_id");
$stmt->bindParam(":user_id",$current_user);
$stmt->execute();
}
} catch(PDOException $e) {
echo "Error: " . $e->getMessage();
}
?>
$current_user是整数还是字符串。如果字符串尝试 PDO::PARAM_STR 而不是 PDO::PARAM_INT
$stmt->bindParam(":current_id",$current_user,PDO::PARAM_STR);
$stmt->execute();
如果还是不行试试这个
//$stmt->bindParam(":current_id",$current_user,PDO::PARAM_STR);
$stmt->execute(array(:current_id"=>$current_user));
为什么我的代码不起作用。求助,我没有看到任何错误。如果我使用 user_id=$current_user 而不是 user_id=:current_id,它会起作用。但出于安全原因,我需要做好准备。请帮忙
<?php
$hostname = 'localhost';
$username = '**';
$password = '**';
$dbname = '**';
try {
$db = new PDO("mysql:host=$hostname;dbname=$dbname", "$username" ,"$password" ,
array(PDO::ATTR_EMULATE_PREPARES => false, PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION));
} catch (PDOException $e) {
echo" There was an error with the connection";
}
$current_user=$_POST['users_id'];//get user id
include 'db_tag.php';
$stmt=$db->prepare("DELETE FROM object_sign WHERE user_id=:current_id");
$stmt->bindParam(":current_id",$current_user,PDO::PARAM_INT);
$stmt->execute();
?>
@conan,使用下面我提到的代码,你需要用这段代码检查以下几点。
1) 打印 $current_user id 以确保 id 正确出现与否,另外我已经设置了条件 if post contain user id then only正在执行查询。
2) 我已经设置了异常,所以你可以通过异常消息检查错误。
<?php
include 'db_tag.php';
$hostname = 'localhost';
$username = '**';
$password = '**';
$dbname = '**';
try {
$db = new PDO("mysql:host=$hostname;dbname=$dbname", "$username" ,"$password" ,
array(PDO::ATTR_EMULATE_PREPARES => false, PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION));
if(isset($_POST['users_id'])) {
$current_user=$_POST['users_id'];//get user id
$stmt=$db->prepare("DELETE FROM object_sign WHERE user_id=:user_id");
$stmt->bindParam(":user_id",$current_user);
$stmt->execute();
}
} catch(PDOException $e) {
echo "Error: " . $e->getMessage();
}
?>
$current_user是整数还是字符串。如果字符串尝试 PDO::PARAM_STR 而不是 PDO::PARAM_INT
$stmt->bindParam(":current_id",$current_user,PDO::PARAM_STR);
$stmt->execute();
如果还是不行试试这个
//$stmt->bindParam(":current_id",$current_user,PDO::PARAM_STR);
$stmt->execute(array(:current_id"=>$current_user));