为什么不删帖?

时间:2014-03-24 14:53:43

标签: php mysql database key sql-delete

我写了这段代码来删除每次点击数据库表中的帖子' community_posts'。当我点击按钮时,它没有删除它,他获得了do=delete&key=THEID的网址,但它不起作用,为什么?请有人帮助我!

// PHP CODE
if($do == "delete" && is_numeric($key)){
        $check = mysql_query("SELECT id FROM community_posts WHERE id = '".$key."' LIMIT 1") or die(mysql_error());

        if(mysql_num_rows($check) > 0){
                mysql_query("DELETE FROM community_posts WHERE id = '".$key."' LIMIT 1") or die(mysql_error());
                $msg = "<center>Neuigkeit wurde erfolgreich gelöscht...</center>"; } else { $msg = "<center>Neuigkeit konnte nicht entfernt werden, versuche es erneut...</center>";
        }
}

// HTML CODE UND AUSGABE DER DATENBANK
<form action='' method='post'>
<?php $getComments = mysql_query("SELECT * FROM community_posts ORDER by id DESC LIMIT 100"); ?>
<?php while($row = mysql_fetch_array($getComments)){
$getUserInfo = mysql_query("SELECT * FROM users WHERE id = '".$row['userid']."'");
$roww = mysql_fetch_array($getUserInfo); ?>

<div class="feedOne fade ptr">
        <div class="ph20">
                <div class="inner-1 lt" style=""><p style="color:#aeaeae;font-size:13px;"><a href="/community/userprofile/<?php echo $roww['username']; ?>" style="color:orange;text-decoration:none;font-weight:normal;"><?php echo $roww['vorname']; ?></a> &raquo; Open Community</p>
                <p class=lt style="color:#aeaeae;font-size:11px;"><?php echo $row['posted_on']; ?></p><br />
                </div>
                <div class="rt inner-2">
                <p style="color:#333;font-size:13px;"><?php echo $row['story']; ?></p>
                </div>
                <div class=cl></div>
        </div>

<!-- BEI KLICK SOLL HIERMIT DER ENTSPRECHENDE BEITRAG GELÖSCHT WERDEN -->
<a href='<?php echo $path_admin; ?>/openfeed?do=delete&key=<?php echo $row['id']; ?>'><img src="/assets/data/images/icons/delete_round.png" alt="delete" /></a>
<!---->

</div> 

<?php } ?>
</form>

3 个答案:

答案 0 :(得分:2)

URL标头中的变量存储在$_GET数组中。相反,你的第一行应该是:

if($_GET['do'] == "delete" && is_numeric($_GET['key'])){

编辑:您可能想要使用isset()方法检查变量是否已预先设置。

答案 1 :(得分:0)

在对它们运行if语句之前,需要分配这两个变量:

// PHP CODE

if (isset($_GET['do'])){
    $do = $_GET['do'];
}
if (isset($_GET['key'])){
    $key = $GET['key'];
}

if($do == "delete" && is_numeric($key)){

答案 2 :(得分:0)

首先,我看不到在哪里为$do$key变量赋值。这是你要做的:

<?php
$do = !empty($_GET['do']) ? $_GET['do'] : '';
//Prevent SQL injection
$key = !empty($_GET['key']) ? (int)$_GET['key'] : '';
// ALL OTHER PHP CODE GOES HERE

?>