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

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 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;
        }
    
    }
    ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥18 深度学习tensorflow1,ssdv1,coco数据集训练一个模型
  • ¥100 关于注册表摄像头和麦克风的问题
  • ¥30 代码本地运行正常,但是TOMCAT部署时闪退
  • ¥15 关于#python#的问题
  • ¥15 主机可以ping通路由器但是连不上网怎么办
  • ¥15 数据库一张以时间排好序的表中,找出多次相邻的那些行
  • ¥50 关于DynamoRIO处理多线程程序时候的问题
  • ¥15 kubeadm部署k8s出错
  • ¥15 Abaqus打不开cae文件怎么办?
  • ¥15 小程序准备上线,软件开发公司需要提供哪些资料给甲方