2014-12-03 34 views
0

表名 - breaking_news 字段名 - 状态PHP代码中的if else条件不能正常工作

我创建了一个单页,并从链接点击通过ID。

现在我检查,如果状态不为空和地位平等为无效 然后更新状态=有效否则更新状态无效

,但它不能正常工作。

它仅适用于如果条件。

代码的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; 
    } 
    ?> 
+2

print_r($ row)的输出是什么? – 2014-12-03 11:56:12

+1

从DOC - '返回一个字符串的关联数组对应于提取行,或FALSE如果没有更多的rows'所以你'if'应该是'如果($行==假&& $行['sstatus! “] ==‘无效’)' – folibis 2014-12-03 12:04:16

+0

$行越来越活跃,如果状态数据库中其他有效状态是不活动的数据库,然后它获取无效 – ashish 2014-12-03 12:04:20

回答

0

试试这个代码 “>

<?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; 
} 
?> 

你刚才问,如果$行是有更多与否,是事业mysql_fetch_assoc返回false,如果它是空看本手册:mysql_fetch_assoc PHP

你刚才不使用empty()

+0

这也是不工作... – ashish 2014-12-03 12:21:28

+0

好的,什么是错误信息? – 2014-12-03 12:23:10

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; 
} 
0

如果你还没有找到一排,没有任何更新...

你不妨做这样的事情:

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

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

编辑
@ toto21我没有足够的声望评论你的答案,但是没有,你的回答是错误的。正如我在顶部提到的那样 - 如果没有获取的行,那么db中没有任何内容供您更新,因此您的else语句没有任何意义。