dongwalun2507
dongwalun2507
2015-03-07 13:19
浏览 59
已采纳

PDO不在数据库中插入数据

I am trying to create signup page using PDO and OOP. I have spent 3 hours but cannot find what the problem with my code.Everything is working fine (i have debugged it) but data is not inserted in database.

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条回答 默认 最新

  • duanguane1670
    duanguane1670 2015-03-07 13:26
    已采纳

    please try this.. a closing round bracket is missing in your query

    <?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;
        }
    
    }
    ?>
    
    点赞 评论
  • duanjuda5789
    duanjuda5789 2015-03-07 13:28

    You missed your prepare statement, from PHP.NET :

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

    But you attempt to do

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

    And you missed the closing bracket ^^

    点赞 评论

相关推荐