PDO准备语句不会用数据

时间:2016-08-17 07:44:47

标签: php mysql pdo

我正在使用PDO创建一些基本的登录注册表单,但是当我查询$ var_dump()时,无法在数据库中插入数据,我看到在$ query->执行语句中没有值不会执行。什么时候我发现var_dump($ query);

**

  

object(PDOStatement)#4(1){[“queryString”] => string(92)“INSERT INTO   用户(用户名,密码,姓名,电子邮件,网站)VALUES(?,?,?,?,?)“}   注册成功。点击这里登录

**

我遇到了这个问题并尝试了我所知道的几乎所有内容。请看代码帮我找到问题。

数据库格式如 数据库名称:oopreg 表名:用户

id          int(11)       primary auto_increment
username    varchar(30)
password    varchar(30)
name        varchar(50) 
email       varchar(100)
website     varchar(100)

的config.php

<?php
class databaseConnection{

    public function __construct(){
        GLOBAL $pdo;
        try{
    $pdo = new  PDO('mysql:host:localhost;dbname=oopreg','root','');
    echo "Connected";
        }catch(PDOException $e){

            echo "DataBase connection Error";

        } 
    }

}

的functions.php

<?php

require 'config.php';

   class loginRegistration{

      function  __construct(){
        $database = new databaseConnection();
      } 
     public function registerUser($username,$password,$name,$email,$website){
        global $pdo;
        $query= $pdo->prepare("SELECT id FROM users WHERE username=? AND email=?");
        $query->execute(array($username,$email));
        $num= $query->rowCount();
        if($num==0){
          $query= $pdo->prepare("INSERT INTO users(username,password,name,email,website) VALUES
                 (?,?,?,?,?)");
          $query->execute(array($username,$password,$name,$email,$website));  
        var_dump($query);
         return true;

        }else{
             print "<span style='color:red'>Error...Username/Email alreay exists</span>";
        }
     }

   }


  ?>

register.php

<?php

require_once "functions.php";
$user= new loginRegistration();
?>

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Registration Page</title>
    <link rel="stylesheet" type="text/css" href="style.css">
</head>
<body>
<div class="wrapper">
    <div class="header">
        <h3>PHP OOP Login Register System</h3>
    </div><!--End header Section-->
    <div class="mainmenu">
        <ul>
            <li><a href="index.php">Home</a></li>
            <li><a href="profile.php">Show Profile</a></li>
            <li><a href="changePassword.php">Change Password</a></li>
            <li><a href="logout.php">Logout</a></li>
            <li><a href="login.php">Login</a></li>
            <li><a href="register.php">Register</a></li>
        </ul>
    </div><!--End Manin menu section-->

    <div class="content">
        <h2>Register</h2>

    <p class="msg">

       <!--PHP area start--> 
        <?php 
        if ($_SERVER['REQUEST_METHOD']=='POST') {
            $username=$_POST['username'];
            $password=$_POST['password'];
            $name=$_POST['name'];
            $email=$_POST['email'];
            $website=$_POST['website'];

           if(empty($username) or empty($password) or empty($name) or empty($email) or empty($website)) {

                echo "<span style='color:red;'> Field must not be empty</span>";
           }else{
                 $password=md5($password);
               $register=$user->registerUser($username,$password,$name,$email,$website);
               if($register){
                echo "<span style='color:green'>Registration successfull.<a href='login.php'>Click here</a> for login</span>";
               }else{
                echo "<span style='color:red'>UserName or email already exists</span>";
               }
           }

        }
         ?>
      </p>
    <div class="login_reg">
        <form action="" method="post"> 
            <table>
                <tr>
                    <td>Username:</td>
                    <td><input type="text" name="username" placeholder="Enter your username..."/></td>
                </tr>
                <tr>
                    <td>Password:</td>
                    <td><input type="password" name="password" placeholder="Enter your password..."/></td>
                </tr>
                <tr>
                    <td>Name:</td>
                    <td><input type="text" name="name" placeholder="Enter your name..."/></td>
                </tr>
                <tr>
                    <td>Email:</td>
                    <td><input type="email" name="email" placeholder="Enter your email..."/></td>
                </tr>
                <tr>
                    <td>Website:</td>
                    <td><input type="text" name="website" placeholder="Enter your website..."/></td>
                </tr>
                <tr>
                    <td colspan="2">
                        <input type="reset" name="" value="Reset"/>
                        <input type="submit" name="register" value="Register"/>

                    </td>
                </tr>
            </table>
        </form>
        <?php //var_dump($_POST); ?>
    </div><!--End Login_reg page-->
    <div class="back">
        <a href="">Back</a>

    </div>
    </div><!--End Content section-->
    <div class="footer">
        <h3>Training with live project</h3>
    </div>

</div><!--End Wrapper section-->
</body>
</html>

2 个答案:

答案 0 :(得分:0)

  

我发现在$ query-&gt;执行语句中不会执行任何值。请参阅下面的内容,当我发现var_dump($ query);

没关系,PDO以这种方式工作。它不会用queryString属性中的实际数据替换问号。

要查看查询无法正常工作的真正原因,您必须正确配置PDO ,如我的文章PDO tutorial. Connecting. DSN中所述:

public function __construct(){
    $opt = [
        PDO::ATTR_ERRMODE            => PDO::ERRMODE_EXCEPTION,
        PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,
        PDO::ATTR_EMULATE_PREPARES   => false,
    ];
    $pdo = new  PDO('mysql:host:localhost;dbname=oopreg','root','', $opt);

之后,如果出现错误,将向您报告。

答案 1 :(得分:0)

您尚未从查询中的值中删除单引号。 你可能用过 值(':aa',... 代替 值(:a,...

希望这可以解决问题。我也遇到了类似的挑战