为什么不删除帖子呢?

时间:2022-07-05 21:21:19

I wrote this code to delete per click a post out of the database table 'community_posts'. When I click on the Button, it doesn't delete it, he get the URL with do=delete&key=THEID but it doesn't work, why? Can someone help me, please!

我写这段代码是为了删除数据库表“community_posts”中的一个帖子。当我点击这个按钮时,它不会删除它,他会用do=delete&key=THEID来获取URL,但它不起作用,为什么?有人能帮我吗?

// 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 个解决方案

#1


2  

Variables in the URL header are stored in the $_GET array. Instead your first line should be :

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

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

EDIT : You might want to check if the variable is set beforehand using the isset() method.

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

#2


0  

First, I do not see where do you assign values to the $do and $key variables. Here is what you have to do:

首先,我不知道您在哪里为$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

?>

#3


0  

You need to assign these two variables before you run an if statement on them:

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

// PHP CODE

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

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

#1


2  

Variables in the URL header are stored in the $_GET array. Instead your first line should be :

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

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

EDIT : You might want to check if the variable is set beforehand using the isset() method.

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

#2


0  

First, I do not see where do you assign values to the $do and $key variables. Here is what you have to do:

首先,我不知道您在哪里为$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

?>

#3


0  

You need to assign these two variables before you run an if statement on them:

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

// PHP CODE

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

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