dpowt82802 2011-04-11 19:04
浏览 29
已采纳

三元运算符变量和mysql?

Will this work?

$username = mysql_real_escape_string(isset($_POST['username'])) ? $_POST['username'] : '';

I know the ternary operator will work but what about the real escape? And how can I check for future reference if the real escape is successful?

Thanks

  • 写回答

2条回答 默认 最新

  • dongwu5318 2011-04-11 19:06
    关注

    Your code is escaping the conditional of the ternary expression not the result.

    $username = mysql_real_escape_string(isset($_POST['username']) ? $_POST['username'] : '');
    

    Move the bracket to the end of the expression then you will escape whatever the result of the ternary expression is. You could also use

    $username = isset($_POST['username']) ? mysql_real_escape_string($_POST['username']) : '';
    

    A better option is to use a function for this instead of having a bunch of huge lines of code around ie.

    function escape_val($arr, $key, $default = ''){
        return mysql_real_escape_string(isset($arr[$key]) ? $arr[$key] : $default);
    }
    
    $username = escape_val($_POST, 'username');
    $some_other = escape_val($_POST,'key','default_value');
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度