douzhanglun4482 2015-06-17 07:35
浏览 23

PDO选择$ _POST [关闭]

this works just fine

$db = new PDO(<connection data>);
    $username = $_POST['username'];
    $password = $_POST['password'];
    if(isset($_POST['username']) && isset($_POST['password'])){
        $username = "name";
        $pw = $db->prepare('SELECT * FROM table WHERE column1 = ?');
        $pw->execute(array($username));
        $result = $pw->fetchAll();
        var_dump($result);
    }

If i use $_POST['username'] after form submission i get array(0) { } for var_dump($result);

$db = new PDO(<connection data>);
    $username = $_POST['username'];
    $password = $_POST['password'];
    if(isset($_POST['username']) && isset($_POST['password'])){
        $username = $_POST['username'];
        //var_dump($username); --> string(7) " name"
        $pw = $db->prepare('SELECT * FROM table WHERE column1 = ?');
        $pw->execute(array($username));
        $result = $pw->fetchAll();
        var_dump($result);
    }

Do you see where i made my mistake(s)?

  • 写回答

1条回答 默认 最新

  • drf97973 2015-06-17 07:41
    关注

    In the first test, with hard-coded value, you have

    $username = "name";
    

    then, when using post value, it becomes

    $username = $_POST['username']; // --> " name"
    

    as you see, there is an extra space. If such space should not be there, you could then try trimming the value, like:

    $username = trim($_POST['username']);
    

    However, if having spaces in username is an error, you should not "correct" it yourself, and just report the error to the user (I think this is more a UX aspect than a programming one).

    评论

报告相同问题?

悬赏问题

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