PDO不在数据库中插入数据

时间:2015-03-07 13:19:34

标签: php mysql pdo

我正在尝试使用PDO和OOP创建注册页面。我花了3个小时但找不到我的代码有什么问题。一切正常(我已经调试过)但数据没有插入数据库。

Db1.php

<?php
class Db1
{
    private $host = "localhost";
    private $user = "root";
    private $db = "website";
    private $pass = "";
    public $conn ;

    public function __construct()
    {
        $this->conn = new PDO("mysql:host=".$this->host.";dbname=".$this->db,$this->user,$this->pass);
    }
    public function getconn()
    {
        return $this->conn;
    }
}
?>

users.php

<?php
include_once("Db1.php");
class users extends Db1
{
    private $id;
    private $name;
    private $email;
    private $password;
    private $member_since;
    private $last_seen;
    private $country;
    private $city;
    private $reputation;
    private $status;

    public function insertData($name,$email,$password,$country="",$city="")
    {
         $sql = "INSERT INTO users (name,email,password,member_since,last_seen,country,points,city,status) VALUES (name=:name,email=:email,password=:password,member_since=:member_since,last_seen=:last_seen,country=:country,reputation=:reputation,city=:city,status=:status";
        // i have tried this also.
        //$sql = "INSERT INTO users SET name=:name,email=:email,password=:password,country=:country,city=:city,reputation=:reputation,member_since=:member_since,last_seen=:last_seen,status=:status";
        $q = parent::getconn()->prepare($sql);
        $q->execute(array(':name'=>$name,':email'=>$email,':password'=>$password,':country'=>$country,':city'=>$city,':reputation'=>3,':member_since'=>date("y-m-d h:i:s"),':last_seen'=>date("y-m-d h:i:s"),':status'=>'a'));
        return true;
    }

}
?>

signup.php

<?php
if(isset($_POST['signup'])){

    include_once("classes/users.php");
    $obj = new users;

    if($obj->insertData($_POST['name'],$_POST['email'],$_POST['password'])){
        echo"data inserted!";
    }
}
?>
<section id="create_account">
        <div class="container">

            <div class="center">
                <div class="row">
                    <h2>Create Account</h2>
                </div>
            </div>
            <div class="row">
                <div class="col-md-4">

                </div>
                <div class="col-md-4">
                    <form method="POST" action="signup.php">
                        <div class="form-group">
                            <label>Name *</label>
                            <input type="text" name="name" class="form-control" required="required">
                        </div>
                        <div class="form-group">
                            <label>Email *</label>
                            <input type="email" name="email" class="form-control" required="required">
                        </div>
                        <div class="form-group">
                            <label>Password *</label>
                            <input type="password" name="password" class="form-control" required="required">
                        </div>
                        <div class="form-group">
                            <label>Confirm Password *</label>
                            <input type="password" name="cpassword" class="form-control" required="required">
                        </div>
                        <input type="submit" class="btn btn-primary btn-lg" name="signup" value="Create Account"/>

                    </form>
                </div>
                <div class="col-md-4">

                </div>
            </div>

        </div>
    </section>

2 个答案:

答案 0 :(得分:2)

请尝试此操作..查询中缺少结束圆括号

<?php
include_once("Db1.php");
class users extends Db1
{
    private $id;
    private $name;
    private $email;
    private $password;
    private $member_since;
    private $last_seen;
    private $country;
    private $city;
    private $reputation;
    private $status;

    public function insertData($name,$email,$password,$country="",$city="")
    {
         $sql = "INSERT INTO users (name,email,password,member_since,last_seen,country,points,city,status) VALUES (:name,:email,:password,:member_since,:last_seen,:country,:reputation,:city,:status)";


        $q = parent::getconn()->prepare($sql);
        $q->execute(array(':name'=>$name,':email'=>$email,':password'=>$password,':country'=>$country,':city'=>$city,':reputation'=>3,':member_since'=>date("y-m-d h:i:s"),':last_seen'=>date("y-m-d h:i:s"),':status'=>'a'));
        return true;
    }

}
?>

答案 1 :(得分:0)

您错过了PHP.NET的准备声明:

$sql = "INSERT INTO REGISTRY (name, value) VALUES (:name, :value)";

但你试图做

$sql = "... VALUES (name=:name,email=:email,password=:password,member_since=:member_since,last_seen=:last_seen,country=:country,reputation=:reputation,city=:city,status=:status";

你错过了结束括号^^