先谢谢了。我只是在学习php。 我正在尝试从我创建的博客中删除帖子。我应该通过post_id删除帖子,后者是自动递增的主键。
但是我如何在WHERE子句中使用post_id?
我的代码是:
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "travelogy";
try{
$conn = new PDO("mysql:host=$servername; dbname=$dbname", $username, $password);
// $post_id_location->bindValue(':post_id', $post_id);
$delete_quary = "DELETE FROM post_p WHERE id ="$_POST['post_id'];
// $delete_quary = "DELETE FROM post_p WHERE id = $post_id_location";
$conn->exec($delete_quary);
echo "Record deleted.";
}catch(PDOException $e){
echo $delete_quary."<br>".$e->getMessage();
}
$conn = null;
错误提示:未定义的变量:第10行的C:\ xampp \ htdocs \ CMS_project \ pdo.php中的post_id_location 致命错误:未捕获错误:在C:\ xampp \ htdocs \ CMS_project \ pdo.php:10中调用成员函数bindValue()上的null堆栈跟踪:#0 {main}抛出在C:\ xampp \ htdocs \ CMS_project \第10行的pdo.php
答案 0 :(得分:-1)
更改:
$delete_quary = "DELETE FROM post_p WHERE id ="$_POST['post_id'];
收件人:
$delete_quary = "DELETE FROM post_p WHERE id = " . $_POST['post_id'];
或者:
$post_id = $_POST['post_id'];
$delete_quary = "DELETE FROM post_p WHERE id = '$post_id'";
也许也考虑将“ quary”重命名为“ query”,以供将来参考。