dongmangzong8006 2013-12-24 01:27
浏览 91

PDO if语句验证

What am I doing wrong here? All the data was subitting until I added the last part of the code, the IF(){} statement. I want to make it where if the information was submitted correctly, for it to say THANK YOU!, else, display the error i set.

<form method="POST">
    <input type="text" name="username" placeholder="username"><br />
    <input type="password" name="password" placeholder="password"><br />
    <input type="submit">
</form>

<?php
    if(isset($_POST['username'], $_POST['password'])){
        require 'core/db.php';

        $conn = dbConnect()->prepare("INSERT INTO users (username, password) VALUES (?,?)");

        $conn->bindParam(1, $_POST['username']);
        $conn->bindParam(2, $_POST['password']);
        $conn->execute();

        if($conn === true){
            echo 'Thanks!';
            header('Location: index.php');
            exit;
        } else{
            echo 'something went wrong';
        }
    }
?>
  • 写回答

2条回答 默认 最新

  • dtz88967 2013-12-24 01:31
    关注

    It's not the $conn that you should checking for true against. $conn is instance of PDOStatement. You should instead be checking the return value of $conn->execute().

    if($conn->execute() === true){
      header('Location: index.php');
      exit;
    } else{
      echo 'something went wrong';
    }
    

    As @mjayt pointed you should not output content before header call. You could definitely output using output buffering ob_start()...ob_end_flush(), but I do not assume the echo is really necessary here.

    评论

报告相同问题?

悬赏问题

  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建
  • ¥15 数据可视化Python
  • ¥15 要给毕业设计添加扫码登录的功能!!有偿
  • ¥15 kafka 分区副本增加会导致消息丢失或者不可用吗?
  • ¥15 微信公众号自制会员卡没有收款渠道啊
  • ¥15 stable diffusion
  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘