PHP类将数据插入表中不起作用

时间:2016-04-14 19:40:57

标签: php class mysqli

使用下面的脚本,我尝试将新用户添加到用户表。 我是一名处理课程的新人,到目前为止,我无法让它工作。我的服务器正在运行PHP7。

的config.php

<?php
/**
 * Database config variables
 */
define("DB_HOST", "localhost");
define("DB_USER", "username");
define("DB_PASSWORD", "12345");
define("DB_DATABASE", "databasename");
?>

connect.php

<?php

class DB_Connect {

    // constructor
    function __construct() {

    }

    // destructor
    function __destruct() {
        // $this->close();
    }

    // Connecting to database
    public function connect() {
        require_once '/folder/folder/folder/config.php';
        // connecting to mysql
        $conn = new mysqli(DB_HOST, DB_USER, DB_PASSWORD, DB_DATABASE);

        // return database handler
        return $conn;
    }

    // Closing database connection
    public function close() {
        $conn->close();
    }

} 
?>

如果我将connect.php中的connect函数更改为下面的函数,我会收到成功消息(已成功连接),所以我认为没有数据库连接问题:

public function connect() {
    require_once '/folder/folder/folder/config.php';
    // connecting to mysql
    $conn = new mysqli(DB_HOST, DB_USER, DB_PASSWORD, DB_DATABASE);

    // Check connection
    if ($conn->connect_error) {
       die("Connection failed: " . $conn->connect_error);
    } 
    echo "Connected successfully";
}

的functions.php

<?php

class DB_Functions {

    private $db;

    //put your code here
    // constructor
    function __construct() {
        include_once './connect.php';
        // connecting to database
        $this->db = new DB_Connect();
        $this->db->connect();
    }

    // destructor
    function __destruct() {

    }

    /**
     * Storing new device
     */
    public function storeUser($name, $userid, $club) {
        echo;
        // insert user into database
        $result = "INSERT INTO users(name, user_id, club_id, created_at) VALUES('$name', $userid, $club, NOW())";
        // check for successful store
        if ($result) {
            // succes
            return "Device is added";
        } else {
            return "Error: Device is not added!" ;
        }
    }

    /**
     * Getting all users
     */
    public function getAllUsers() {
        $result = "select * FROM users";
        return $result;
    }

}

?>


我在下面的脚本作为测试脚本在浏览器中运行以添加测试用户:

<?php

$name = "1234567890";
$userid = "1234";
$club = "09876";

include_once './functions.php';

$db = new DB_Functions();

$res = $db->storeUser($name, $userid, $club);

echo $res;

?>

2 个答案:

答案 0 :(得分:0)

如果不执行insert语句和连接源,它将无法正常工作。试试这个:

$result = $this->db->connect()->query("INSERT INTO `users`(`name`, `user_id`, `club_id`, `created_at`) 
                 VALUES('$name', '$userid', '$club', NOW())");

并检查是否插入了数据:

if($this->db->connect()->affected_rows){
   return 'Device is added';
}

函数connect()不返回任何连接:

您必须放置return $conn;,而不是回应连接成功。

答案 1 :(得分:0)

您没有从数据库实例发送任何查询。

<强> connect.php

class DB_Connect {
    private static $_instance;

    // destructor
    function __destruct() {
        $this->close();
    }

    // Connecting to database
    public function connect() {
        require_once '/folder/folder/folder/config.php';

        if (self::$_instance === null) {
            // connecting to mysql
            self::$_instance = new mysqli(DB_HOST, DB_USER, DB_PASSWORD, DB_DATABASE);
        }
        // return database handler
        return self::$_instance;
    }

    // Closing database connection
    public function close() {
        if (self::$_instance !== null) {
            self::$_instance->close();
        }
    }

}

<强> functions.php.php

public function storeUser($name, $userid, $club) {
    // insert user into database
    $query = "INSERT INTO users(name, user_id, club_id, created_at) VALUES({$name}, {$userid}, {$club}, NOW())";
    $result = this->db->query($query); // <--- you missed out this syntax...
    // check for successful store
    if ($result) {
        // succes
        return "Device is added";
    } else {
        return "Error: Device is not added!" ;
    }
}