Event ID: ".$events-6ren">
gpt4 book ai didi

PHP 删除事件 ID

转载 作者:行者123 更新时间:2023-11-30 01:07:37 27 4
gpt4 key购买 nike

我已经制作了一个事件日历,但现在当用户删除事件时我遇到了问题。

截图:http://i.stack.imgur.com/LBLJ9.png

<h2>Date <?php echo "$day"."/"."$month"."/"."$year"."<br>"; ?></h2>
<?php

// event weer gegeven
while ($events = mysql_fetch_array($resultEvents)){
echo "<strong>Event ID:</strong> ".$events['id']."<br>";
echo "Added: ".$events['added']."<br>";
echo "Title: ".$events['titel']."<br>";
echo "Detail: ".$events['content']."<br>";
?>

<form method="POST" action="<?php $_SERVER['PHP_SELF']; ?>">
<input type="submit" name="delete" value="Delete"><br >
<input type="submit" name="edit" value="Edit"><br >
</form>
<?php

if(isset($_POST['delete'])){

$user = $_POST['delete'];
$delet_query = mysql_query("DELETE FROM kalender_contents WHERE `id` = '$events'") or die(mysql_error());

if($delet_query) {
echo "event deleted";
echo $events;
}
}
}
}
}
?>

但是 WHEREid= '$events' 不起作用。如何在按钮单击时指定 ID。

最佳答案

尝试将其相关的事件ID放入如下形式

<form method="POST" action="<?php $_SERVER['PHP_SELF']; ?>">
<input type="hidden" name="event_id" id="event_id" value="<?php echo $events['id'];?>">
<input type="submit" name="delete" value="Delete"><br >
<input type="submit" name="edit" value="Edit"><br >
</form>

并在您提交查询中使用类似

$delet_query = mysql_query("DELETE FROM kalender_contents 
WHERE `id` = $_POST['event_id']");

每当您提交表单时,都会传递其事件 ID,并且您需要将该 post 变量传递给删除查询。

此外,您需要对 id 进行转义,以防止在查询中使用 mysql_real_escape_string 进行 sql 注入(inject),例如

$delet_query = mysql_query("DELETE FROM kalender_contents 
WHERE `id` = ".mysql_real_escape_string($_POST['event_id']));

关于PHP 删除事件 ID,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19634823/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com