PHP删除事件ID

时间:2013-10-28 12:28:03

标签: php mysql

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

屏幕截图: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;
            }
            }    
    }
}
}
?>

WHERE ID = '$events'不起作用。如何在按钮单击上指定ID。

2 个答案:

答案 0 :(得分:0)

尝试将相关的event 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以阻止查询sql injection mysql_real_escape_string

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

答案 1 :(得分:0)

创建一个值为event id的隐藏字段。

<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="hidden" name="event_id" value="<?php echo $events['id'];?>">
<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;
            }
            }    
    }
}
}
?>