PHP代码如果其他条件不正常工作

时间:2014-12-03 11:54:15

标签: php

表名 - breaking_news 字段名称 - 状态

我创建了一个页面,并通过链接点击传递了ID。

现在我检查状态是否为空且状态是否为非活动 然后更新状态= Active Else更新状态无效

但效果不正常。

仅适用于条件。

代码的ELSE条件不起作用。 PLZ建议我如何正确写入...

<td><a href="activate_status.php?status_active=<?php echo $row['id']; ?>"><img src="img/active.png" width="24" height="24"  border="0" title="Active" /></a></td>

     <?php        
    $Admin = new admins;
    $sql = "SELECT status FROM breaking_news WHERE id=".mysql_real_escape_string($_GET['status_active']);
    $result = mysql_query($sql);
    $row = mysql_fetch_assoc($result);
    if(!empty($row) && $row['status']=='Inactive') 
    { 
        mysql_query("Update breaking_news SET status='Active' WHERE id=".mysql_real_escape_string($_GET['status_active']));
        $_SESSION['message'] = "Status Activated Successfully";
        header("Location:breaking_news.php");
        exit;
    }
    else
    {       
        mysql_query("Update breaking_news SET status='Inactive' WHERE id=".mysql_real_escape_string($_GET['status_inactive'])); 
        $_SESSION['message'] = "Status De-Activated Successfully";
        header("Location:breaking_news.php");
        exit;
    }
    ?>

3 个答案:

答案 0 :(得分:0)

试试这段代码     “&GT;

 <?php        
$Admin = new admins;
$sql = "SELECT status FROM breaking_news WHERE id=".mysql_real_escape_string($_GET['status_active']);
$result = mysql_query($sql);
$row = mysql_fetch_assoc($result);
if(!$row && $row['status']=='Inactive') 
{ 
    mysql_query("Update breaking_news SET status='Active' WHERE id=".mysql_real_escape_string($_GET['status_active']));
    $_SESSION['message'] = "Status Activated Successfully";
    header("Location:breaking_news.php");
    exit;
}
else
{       
    mysql_query("Update breaking_news SET status='Inactive' WHERE id=".mysql_real_escape_string($_GET['status_inactive'])); 
    $_SESSION['message'] = "Status De-Activated Successfully";
    header("Location:breaking_news.php");
    exit;
}
?>

你只需询问$ row是否有更多,因为mysql_fetch_assoc如果它是空的则返回false:mysql_fetch_assoc PHP

您刚刚不使用empty()

答案 1 :(得分:0)

或者那样:

if($row = mysql_fetch_assoc($result))
{
    if($row['status']=='Inactive') 
    { 
        mysql_query("Update breaking_news SET status='Active' WHERE id=".mysql_real_escape_string($_GET['status_active']));
        $_SESSION['message'] = "Status Activated Successfully";
        header("Location:breaking_news.php");
        exit;
    }
}
else
{       
        mysql_query("Update breaking_news SET status='Inactive' WHERE id=".mysql_real_escape_string($_GET['status_inactive'])); 
        $_SESSION['message'] = "Status De-Activated Successfully";
        header("Location:breaking_news.php");
        exit;
}

答案 2 :(得分:0)

如果您没有找到行,则无需更新......

您可能希望这样做:

    <?php
    if (!empty($row)) {
        if ($row['status'] == 'Inactive') {
            //update to active
        }
        else if ($row['status'] == 'Active') {
            //update to inactive
        }
    }

你在$ _GET数组的第二次调用中也遇到了错字:$ _GET [&#39; status_inactive&#39;]。您应该更新同一行但具有不同的状态值。

EDIT
@ toto21我没有足够的声誉评论你的答案,但不,你的答案是错的。正如我在顶部提到的 - 如果没有提取行,则数据库中没有任何内容供您更新,因此您的else语句没有任何意义。