两个查询和我得到一个完全空白的屏幕,错误报告显示什么

时间:2014-05-11 19:18:40

标签: php database debugging select pdo

我已经看到了关于这个问题的一些其他问题,但我相信我的独特之处。我正在处理一个表单,对于PHP和PDO来说相对较新,所以没有最好的调试技巧。我相信我的查询是错误的,因为当我按下表单上的提交按钮时,它会抛出一个没有错误消息的空白页面(即使我有错误报告)。

我开始在MySqli中写这个,但后来不得不废弃它,因为它不起作用,我需要在Insert语句中轻松拥有变量。这是我的代码

error_reporting(-1);    

       require 'scripts/PasswordHash.php';
       $username = $name = $email = $password = $gender = $age = '';
       $query = FALSE;
       $dbh = new PDO('mysql:host=localhost;dbname=some_database;charset=utf8', 'some_user', 'some_password');

       if ($_SERVER['REQUEST_METHOD'] == 'POST') {
           //Logic
            if (empty($_POST['age'])) {
               $errors[] = 'Please enter your age';
           }

           if (empty($_POST['gender'])) {
               $errors[] = 'Please select your gender';
           }

           if ($_POST['password'] != $_POST['passwordRep']) {
               $errors[] = 'You did not repeat your password properly';
           }

           if (empty($_POST['password'])) {
               $errors[] = 'Please enter a password';
           }

           if (empty($_POST['email'])) {
               $errors[] = 'Please enter your email in this format : xxxxx@xxx.xxx or xxxxx@xxx.xx.xx';
           }

           if (empty($_POST['name'])) {
               $errors[] = 'Please enter your name';
           }
           if (check("username", $_POST['username']) == TRUE) {
               $errors[] = 'That username already exists';
           }
           if (check("email", $_POST['email']) == TRUE) {
               $errors[] = 'That username already exists';
           }

           if (empty($_POST['username'])) {
                $errors[] = 'Please enter a username';
           }

           if (empty($errors)) {
               $username = trim($_POST['username']);
               $name = trim($_POST['name']);
               $email = trim($_POST['email']);
               $password = PassHash::hash($_POST['password']);
               $gender = trim($_POST['gender']);
               $age = trim($_POST['age']);
               $date = date("Y-m-d H:i:s");
               $hash = md5( rand(0,1000) );
               insert();
           }
       }


       function insert() {
           try {
                //connect as appropriate as above
                $sql= "INSERT INTO users VALUES(:username, :password, :name, :email, :date, :age, :gender, :hash)"; 
                $stmt = $dbh->prepare($sql);
                $stmt->bindParam(':username', $username, PDO::PARAM_STR);
                $stmt->bindParam(':password', $password, PDO::PARAM_STR);
                $stmt->bindParam(':name', $name, PDO::PARAM_STR);
                $stmt->bindParam(':email', $email, PDO::PARAM_STR);
                $stmt->bindParam(':date', $date, PDO::PARAM_STR);
                $stmt->bindParam(':age', $age, PDO::PARAM_STR);
                $stmt->bindParam(':gender', $gender, PDO::PARAM_STR);
                $stmt->bindParam(':hash', $hash, PDO::PARAM_STR);
                $stmt->execute();
                $query = TRUE;
                require 'scripts/sendEmailVerify.php';
            } catch(PDOException $ex) {
                echo "An Error occured!"; //user friendly message
                echo ($ex->getMessage());
            }
       }

       function check($field, $input) {
           try {
                $sql= "SELECT :field FROM users WHERE :field = :input"; 
                $stmt = $dbh->prepare($sql);
                $stmt->bindParam(':field', $field, PDO::PARAM_STR);
                $stmt->bindParam(':input', $input, PDO::PARAM_STR); 
                $stmt->execute();

                $row_count = $stmt->rowCount();
                if ($row_count > 0) {
                    return TRUE;
                } else {
                    return FALSE;
                }

            } catch(PDOException $ex) {
                echo "An Error occured!"; //user friendly message
                echo ($ex->getMessage());
            }
       }

我认为可能是我在函数中发生了查询(虽然我不知道)。我已经通过PHP代码检查器(http://phpcodechecker.com/)运行此代码,它很好,所以我不认为这是我的语法!谢谢,

尼克

编辑:由于有关调试的链接,我找到了错误!

1 个答案:

答案 0 :(得分:0)

我认为您不能使用参数作为字段标识符,因为您正在使用检查功能。