douhui3330 2017-01-04 06:01
浏览 104
已采纳

if语句中的两个变量值一个需要加上一个或两个其他变量值

I have been working on an old app mostly dealing with upgrading from mysql to mysqli and removing depreciated functions. While debugging the mess I would occasionally have an error with in line hyperlink to edit or delete products. Examples

php?act=del&cat_id=5&bc=654321&ds=&src=app

php?act=del&cat_id=5&bc=&ds=ds12345&src=app

php?act=del&cat_id=5&bc=654321&ds=ds12345&src=app

Some had one part some had two But then discovered that items in a category could have 2 different identifiers ‘ds’ or ‘bc’ or both and the links to edit or delete these items could have one or the other or both neither. Or could have different errors! This Code is what they pointed to.

// Original code
if ($_GET['act'] == 'del') {
  $cat_id = $_GET['cat_id'];
  $ds = $_GET['ds'];
  $bc = $_GET['bc'];

  if($cat_id == '' && $ds == '' || $bc == '') {
    echo 'Error Cannot identify item for action!';
  }
  else {
  //$db = mysqli_connect($db_host, $db_login, $db_pwd, $db_name);
  // Do some SQL Stuff in cat_id that match ds or bc
}

I tried dozens of variations of the original if($cat_id == '' && $ds == '' || $bc == '') using || && equal to or not equal to and one way or another could never get one to be correct in all cases. Including the one suggested by trincot

if ($cat_id == '' && ($ds == '' || $bc == '' ))

Which works for most cases but allows db connect with ds=’’ and bc =’’

The code below I wrote works as needed for all cases of bad query strings But what I was hoping is someone might have a better way to do it.

// From GET -- Just to fix if($cat_id == '' && $ds == '' || $bc == '') {

$cat_id = 3;
$ds = 'bbb';
$bc = '';
// define some vars
$c = 'NO'; $d = 0; $b = 0; $t = 0; $s = 0;
if (is_numeric($cat_id))  {
    $c = 'OK'; }
if ($ds != '') {
    $d = 1;
}
if ($bc != '') {
    $b = 1;
}
$t = $d + $b;
if ( $s == $t || $c != 'OK') {
    echo 'Error Cannot identify item for action! ';
    }
    else {
    echo ' OK two out three aint bad as long as one is cat ';
    //$db = mysqli_connect($db_host, $db_login, $db_pwd, $db_name);
    // Do some SQL Stuff in cat_id that match ds or bc
    }

This is a working sample of what I came up with Thanks for looking any help would be greatly appreciated.

http://sandbox.onlinephpfunctions.com/code/fd32ad87ff42836660a59f826b8f08fa0f8d16f0

Here are the different examples I want to test against. And desired result I know I need to update other parts of the original code but for now I am just looking for an easier way to trigger My Error! before db connect!

example 1:
$cat_id = 3;
$ds = '';
$bc = '';

Result: Error Cannot identify item for action! \\ No Part number either ds or bc

example 2:
$cat_id = 'a';
$ds = 'ds-195062';
$bc = '654321';

Result : Error Cannot identify item for action! \\ wrong cat_id

example 3:
$cat_id = '';
$ds = 'ds-195062';
$bc = '654321';

Result : Error Cannot identify item for action! \
o cat id

example 4:
$cat_id = '5';
$ds = '';
$bc = '654321';

Result :  OK two out three aint bad as long as one is cat  
\\ have a bc part number and a catagory

example 5:
$cat_id = '5';
$ds = 'ds-195062';
$bc = '654321';

Result :  OK two out three aint bad as long as one is cat  
\\ have a bc and ds part number and a catagory

example 6:
$cat_id = '5';
$ds = 'ds-195062';
$bc = '';

Result :  OK two out three aint bad as long as one is cat  
\\ have a ds part number and a catagory

This is an edit to the original question hoping it is more clear now

Thank You for looking

Ended up with this Thanks @trincot for explaining this to me

if ($_GET['act'] == 'del') {
  $cat_id = ( isset( $_GET['cat_id'] ) && is_numeric( $_GET['cat_id'] ) ) ? intval( $_GET['cat_id'] ) : 0;
  $ds = isset($_GET['ds']) ? $_GET['ds'] : '';
  $bc = isset($_GET['bc']) ? $_GET['bc'] : '';  
  if ( $ds == '' ) { $ds = 'n'; }
  if ( $bc == '' ) { $bc = 'n'; }

  if($cat_id == 0 || ($ds == 'n' && $bc == 'n')) {

    echo 'Error! ';
    echo 'Result: value of ds ' . $ds .' value of bc '. $bc .' cat id is '. $cat_id;
  }
  else {
    echo 'Good To Go ';
    echo 'Result: value of ds ' . $ds .' value of bc '. $bc .' cat id is '. $cat_id; 
  }
}
  • 写回答

1条回答 默认 最新

  • dttl3933 2017-01-04 06:12
    关注

    You have the && and || operators in the wrong sense. Change this:

    if($cat_id == '' && $ds == '' || $bc == '') {
        echo 'Error Cannot identify item for action!';
    }
    

    to this:

    if($cat_id == '' || ($ds == '' && $bc == '')) {
        echo 'Error Cannot identify item for action!';
    }
    

    Note that the extra parentheses are not needed because && has precedence over ||, but it does not hurt to be clear.

    Also, unless your else block contains all other code, you need to exit the script execution when this error occurs (maybe after doing some other handling first):

    if($cat_id == '' || ($ds == '' && $bc == '')) {
        echo 'Error Cannot identify item for action!';
        // Some other handling/rendering could come here, but then exit:
        exit();     
    }
    

    Now, the variables will not be '' when the parameters are not passed at all. It would be good to cover for that as well (edit: and the numeric check which you had only in the second code block), and first do:

    $cat_id = (isset($_GET['cat_id']) && is_numeric($cat_id)) ? $_GET['cat_id'] : '';
    $ds = isset($_GET['ds']) ? $_GET['ds'] : '';
    $bc = isset($_GET['bc']) ? $_GET['bc'] : '';
    // ... and then:
    if($cat_id == '' || ($ds == '' && $bc == '')) {
        echo 'Error Cannot identify item for action!';
        // Some other handling/rendering could come here, but then exit:
        exit();     
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 树莓派与pix飞控通信
  • ¥15 自动转发微信群信息到另外一个微信群
  • ¥15 outlook无法配置成功
  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题