我的Update.php
文件:
<?php ob_start();
session_start();
//declare the basic variables
$servername = "localhost";
$username = "bilal";
$password = "super";
$dbname = "coursework";
//create connection
$link = mysqli_connect($servername, $username, $password, $dbname);
//check connection
if($link->connect_error){
die ("connection failed: " . $link->connect_error);
}
//Security purpose, handiling escape string
// $crested_by = $_POST ['created_by'];
$title = $_POST['title'];
$catagory = $_POST['catagory'];
$contact = $_POST['contact'];
$comment = $_POST ['description'];
$ses = $_SESSION['email'];
$date = date('Y-m-d');
$availability = $_POST ['availability'];
$price = $_POST ['price'];
$id = $_POST['wow'];
// $created_by_id = $_SESSION['created_by_id'];
// $username = $_SESSION['firstname'];
if (isset($_POST['del'])){
$deletequery=mysqli_query($link,"DELETE FROM new_post WHERE id='$id'");
header ("Location: added_posts.php");
}
else if(isset($_POST['update'])){
$sql = "UPDATE new_post SET title='$title', contact='$contact', availability='$availability', price='$price', comment='$comment' WHERE id='$id'";
if (mysqli_query($link, $sql)){
echo "awesoke";
header("Location:added_posts.php");
}else{
echo "Error: Sign up Unsuccessfull";
}
}
$link->close();
?>
这是我的 update.php
文件,我将里面的 id
显示为 textbox
以及 id = 文本的情况字段值
。它正在删除,但最近添加的项目会被删除,就像我有 3 个帖子 id(如(4、5、6)id 一样,先删除 6,然后删除 5,然后删除 4。我正在 Bootstrap 卡中显示数据。但发布下的每个删除按钮都会删除最近添加的最近一个。
最佳答案
那么您正在尝试删除数据库中的最新记录?
你的问题是sql语句你有
"DELETE FROM new_post WHERE id='$id'"
这将删除 new_post
中的记录,其中 id 等于从表单中发布的 id。
您必须考虑到表中的最新条目将具有最大的 id。
所以使用这样的东西:
DELETE FROM new_post ORDER BY id DESC LIMIT 1
尝试 2:
您想根据 ID 删除特定行吗?
我建议您使用 prepared statements 为了安全
首先尝试这个:
$stmt = $link->prepare("DELETE FROM new_post WHERE id= ?");
$stmt->bind_param('i', $_POST['wow']); // can also use $id
$stmt->execute();
$stmt->close();
关于php - 删除不是删除所选帖子,而是删除最近添加的帖子,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33462228/