Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/286.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/62.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 使用mysqli和oops创建数据库,并在数据库中插入值_Php_Mysql_Oop_Mysqli - Fatal编程技术网

Php 使用mysqli和oops创建数据库,并在数据库中插入值

Php 使用mysqli和oops创建数据库,并在数据库中插入值,php,mysql,oop,mysqli,Php,Mysql,Oop,Mysqli,我在插入时出错,所以我感到困惑。请帮助我或向我推荐oop的最佳教程 testdb.php页面 testclass.php页面 post.php 将其放在构造函数的参数中: include 'testdb.php'; public function __construct($fname, $lname, $fullname) { $this->fname = $fname; $this->lname = $lname; $this->fullname

我在插入时出错,所以我感到困惑。请帮助我或向我推荐oop的最佳教程

testdb.php页面

testclass.php页面

post.php


将其放在构造函数的参数中:

include 'testdb.php';

public function __construct($fname, $lname, $fullname) 
{
    $this->fname = $fname;
    $this->lname = $lname;
    $this->fullname = $fullname;
    $db = new DB_Class();
    $conn = $db->connect();
    // $db = new mysqli('localhost', 'username', 'password', 'database');
    $result = mysqli_query($conn, "INSERT INTO users(fname, lname, fullname) values ('$this->fname', '$this->lname','$this->fullname')") or die(mysqli_error());
    return $result;
}
然后,初始化:

$userTest = new user($_POST['fname'], $_POST['lname'], $_POST['fullname']);
旁注:既然您使用的是mysqli,为什么不使用准备好的语句呢

编辑:您忘记了
DB\u类上的
返回值

class DB_Class 
{
    public $conn;

    public function __construct() 
    {
        $connection = mysqli_connect("localhost", "root", "","test");

        if (mysqli_connect_errno()) {
            echo "Failed to connect to MySQL: " . mysqli_connect_error();
        }
        $this->conn = $connection; // return the mysqli instance
    }

    public function connect()
    {
        return $this->conn;
    }
}

获取错误。创建新用户时,对象的值将被分配并保存到db。之后,您将分配POST变量。您必须从构造函数中获取所有这些信息,或者将变量分配给构造函数。警告:mysqli_query()要求参数1为mysqli,在$result=mysqli_query行上,我使用了此方法,但在$result=mysqli_query($db)上出现错误,请插入用户(f@user3364529您确定在此文件中找到了
DB\u类吗?警告:mysqli\u query()希望参数1是mysqli,我有print print\r($DB),然后我有DB\u类对象()@user3364529是
testdb.php
包含在
testclass.php
包含“testdb.php”
致命错误:当当前类作用域在这些行上显示未定义变量时没有父项$this->fname=$fname;
include 'testdb.php';

public function __construct($fname, $lname, $fullname) 
{
    $this->fname = $fname;
    $this->lname = $lname;
    $this->fullname = $fullname;
    $db = new DB_Class();
    $conn = $db->connect();
    // $db = new mysqli('localhost', 'username', 'password', 'database');
    $result = mysqli_query($conn, "INSERT INTO users(fname, lname, fullname) values ('$this->fname', '$this->lname','$this->fullname')") or die(mysqli_error());
    return $result;
}
$userTest = new user($_POST['fname'], $_POST['lname'], $_POST['fullname']);
class DB_Class 
{
    public $conn;

    public function __construct() 
    {
        $connection = mysqli_connect("localhost", "root", "","test");

        if (mysqli_connect_errno()) {
            echo "Failed to connect to MySQL: " . mysqli_connect_error();
        }
        $this->conn = $connection; // return the mysqli instance
    }

    public function connect()
    {
        return $this->conn;
    }
}
you can try the below code in 

class user extends DB_Class{

    public $fname;
    public $lname;
    public $fullname;   
    public function __construct() 
    {
        parent::__construct(); //add this line of code 

    }
    public function insert($fname, $lname, $fullname)
    {
        $this->fname = $fname;
        $this->lname = $lname;
        $this->fullname = $fullname;
        $db = new DB_Class();
        $result = mysqli_query($db, "INSERT INTO users(fname, lname, fullname) values ('$this->fname', '$this->lname','$this->fullname')") or die(mysqli_error());
        return $result;
    }
}