无法使用PDO进行PHP数据插入 [英] PHP data insertion not working using PDO

查看:345
本文介绍了无法使用PDO进行PHP数据插入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用PDO将数据插入数据库.连接已成功建立,但是准备功能无法正常工作.这给我一个错误:

I am trying to insert data into database using PDO.Connection established successfully,but prepare function isn't working.It's giving me an error:

致命错误:在null上调用成员函数query() E:\ xammp \ htdocs \ OOP \ Project \ classes \ insert.php在第8行

Fatal error: Call to a member function query() on null E:\xammp\htdocs\OOP\Project\classes\insert.php on line 8

我的 index.php 页面:

<?php

    function __autoload($cl){

        require_once "classes/$cl.php";

    }

    $connection = new database();

    if(isset($_POST['submit']))
    {
         $name = $_POST['name'];
         $email = $_POST['email'];
         $ph = $_POST['phone'];

        try
        {
            $ins = new insert();

        }
        catch(PDOEXCEPTION $e)
        {
            echo $this->con_error . $e->getmessage();
        }
    }

classes/database.php:

<?php

class database{

    public $db;
    public $con_error;

    protected function connection(){

        try
        {
            $this->db = new PDO("mysql:host=localhost;dbname=oop","root","");
        }
        catch(PDOException $e)
        {
            echo $this->con_error="An error in connection" . $e->getmessage();
        }

    }
    public function __construct(){

        return $this->connection();

    }



}

classes/insert.php:

<?php

class insert extends database{
    public $stmt; 

    public function __construct(){

        $stmt = $this->db->prepare("INSERT INTO `users` (`name`,`email`,`phone`) VALUES (:name,:email,:phone)");
        $stmt->bindParam(":name",$name);
        $stmt->bindParam(":email",$email);
        $stmt->bindParam(":phone",$ph);
        $stmt->execute();

    }

}

推荐答案

insert不应是database的子级.如果对每个操作都这样做,最终将得到很多类,每个类都有自己的数据库连接.您应该使它们将连接作为参数,以便它们都可以使用相同的连接.

insert shouldn't be a child of database. If you do that for each operation, you'll end up with lots of classes that each have their own database connection. You should make them take the connection as a parameter, so they can all use the same connection.

此外,您的函数正在使用变量$name$email$ph,但是没有将它们作为参数传递给函数.您不应该在构造函数中执行插入操作,而应使用常规方法进行插入.构造函数仅用于初始化类对象.

Also, your function is using the variables $name, $email, and $ph, but they were not passed as arguments to the function. YOu shouldn't be performing the insert in the construtor, that should be done in a normal method. The constructor is just for initializing the class object.

class insert {
    private $db;

    public function __construct($db) {
        $this->db = $db;
    }

    public function insertUser($name, $email, $ph) {
        $stmt = $this->db->db->prepare("INSERT INTO `users` (`name`,`email`,`phone`) VALUES (:name,:email,:phone)");
        $stmt->bindParam(":name",$name);
        $stmt->bindParam(":email",$email);
        $stmt->bindParam(":phone",$ph);
        $stmt->execute();

    }

}          

然后您的主要代码如下:

Then your main code would be like:

$database = new database;

if(isset($_POST['submit']))
{
     $name = $_POST['name'];
     $email = $_POST['email'];
     $ph = $_POST['phone'];

    try
    {
        $ins = new insert($database);
        $ins->insertUser($name, $email, $ph);

    }
    catch(PDOEXCEPTION $e)
    {
        echo $this->con_error . $e->getmessage();
    }
}  

这篇关于无法使用PDO进行PHP数据插入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆