【问题标题】:Problem in a php if conditionphp if 条件中的问题
【发布时间】:2011-03-08 03:57:56
【问题描述】:

我有一个小问题。我通过 foro.php?id=74&mode=add 或 foro.php?id=74&mode=edit 访问该站点,它工作正常。但是当我在 foro.php?id=74&mode 中添加冒号、分号(; 或 :) =添加到编辑选项中

foro.php?id=74&mode=add;
foro.php?id=74&mode=add:
foro.php?id=74&mode=add'

下面是我的代码

<?php 
$numb=mysql_real_escape_string($_GET['id']);

  if ($_GET['mode']=='add') {

    $sql1="select * from cello where number='".mysql_real_escape_string($numb)."' LIMIT 1";
    $result1=mysql_query($sql1) or die(mysql_error());
    while ($row=mysql_fetch_array($result1)) {

        $name=$row['name'];
        echo $name;
    }
  }


elseif ($_GET['mode']='edit') {

$sql="select * from cello account_number='".mysql_real_escape_string($numb)."' limit 1";
$result=mysql_query($sql) or die(mysql_error());

    while ($row=mysql_fetch_array($result)) {

 $acnumb=$row['number'];
$name=$row['name'];
$address=$row['address'];

echo $acnumb;
echo $name;
echo $address;

     }
     }
 else {echo "error!!";}
     ?>

有什么方法可以预防吗?

【问题讨论】:

    标签: php conditional-statements if-statement


    【解决方案1】:

    您使用了赋值运算符=,而不是相等运算符==

    尝试改变这个:

    elseif ($_GET['mode']='edit') {
    

    到这里:

    elseif ($_GET['mode']=='edit') {
    

    【讨论】:

      【解决方案2】:

      问题在于,在以下几行中,在 if 语句中,您不是在比较,而是为 GET 数组中的模式元素赋值:

      ...
      elseif ($_GET['mode']='edit') {
      
      $sql="select * from cello account_number='".mysql_real_escape_string($numb)."' limit 1";
      $result=mysql_query($sql) or die(mysql_error());
      ...
      

      该操作返回 true,第一次比较为 false,这就是它进入编辑部分的原因。

      【讨论】:

        【解决方案3】:

        解决方案:

        问题肯定出在elseif ($_GET['mode']='edit') { 行;那里的= 运算符将$_GET['mode'] 设置为'edit'(其始终计算为true)。一个很好但在词汇上令人困惑的做法是编写如下条件句:

        if (5 == $some_var)
        

        如果不包含第二个=,则会立即出错。

        建议:

        您可能想要实现一个switch 控件来组织您的代码:

        <?php
        switch ($_GET['mode']) {
            case 'add':
        
                $sql1="select * from cello where number='".mysql_real_escape_string($numb)."' LIMIT 1";
                $result1=mysql_query($sql1) or die(mysql_error());
                while ($row=mysql_fetch_array($result1)) {
        
                    $name=$row['name'];
                    echo $name;
                }
        
                break;
        
            case 'edit':
                $sql="select * from cello account_number='".mysql_real_escape_string($numb)."' limit 1";
                $result=mysql_query($sql) or die(mysql_error());
        
                while ($row=mysql_fetch_array($result)) {
        
                    $acnumb=$row['number'];
                    $name=$row['name'];
                    $address=$row['address'];
        
                    echo $acnumb;
                    echo $name;
                    echo $address;
        
                }
        
                break;
        
            default:
                echo "error!!";
        }
        

        【讨论】:

        • 感谢您的建议.. 我之前没想到
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2015-09-02
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-11-08
        • 2018-09-17
        相关资源
        最近更新 更多