通过类访问dbconfig文件 - PHP

I would like to insert data to the database for which I have a class called InsertTodb. But I am unable to access $dbc variable (which is in dbconfig .php) through class. Here is the code

My Class File

<?php
require("dbconfig.php");
class InsertTodb {
    public $tableNme;
    public $data1;
    public $data2;
    public $data3;
    public $arg1;
    public $arg2;
    public $arg3;


    function insertData($tableName, $data1, $data2, $data3, $val1, $val2, $val3) {
        $this->tableNme = $tableName;       
        $this->data1 = $data1;
        $this->data2 = $data2;
        $this->data3 = $data3;
        $this->arg1 = $val1;        
        $this->arg2 = $val2;        
        $this->arg3 = $val3;        

        $insquery = "insert into ".$this->tableNme."(".$this->data1.", ".$this->data2.", ".$this->data3.") values('".$this->arg1."', '".$this->arg2."',' ".$this->arg3."')";
        echo $insquery; 

        if(mysqli_query($dbc, $insquery)) {                 
        $success = "Product added successfully."; 
        echo $success; 
        }
        else {
        $failed = "Error adding product."; 
        echo $failed; 
        }
}
}
?>

My dbconfig file

<?php
$db_hostname = 'localhost';
$db_username = 'root';
$db_password = '';
$db_name = 'oop';

$dbc1 = mysqli_connect ($db_hostname,$db_username, $db_password,$db_name);

if (mysqli_connect_errno()) {
echo "Could not establish database connection!";
exit();
}
?>

My code

<?php

include "InsertTOdb.php";

$ins = new InsertTodb();

$ins->insertData("tableone", "name", "age", "desig",  "Guru", "25", "Accountant");

?>

When I run the above program, it shows the error "Notice: Undefined variable: dbc" and ..." Warning: mysqli_query() expects parameter 1 to be mysqli, null given in ...". I am new to OOP. Please help to fix it.

You should refactor your code a little. First of all,

<?php
include "dbconfig.php"; // Add this first.
include "InsertTOdb.php";
$ins = new InsertTodb($dbc1); // Feed the connector to your class.
$ins->insertData("tableone", "name", "age", "desig",  "Guru", "25", "Accountant");
?>

Now change the InsertTOdb class a little:

class InsertTodb {
    private $dbc;
    public function __construct($dbc) {
        $this->dbc = $dbc;
    }

    // ... the rest of class's properties ...
    function insertData($tableName, $data1, $data2, $data3, $val1, $val2, $val3) {
        ...
        // Change $dbc to $this->dbc.
        if(mysqli_query($this->dbc, $insquery)) {
            ...
        }
        ...
    }
    ...
}

Your insertData() looks a bit clunky with all these $data1, $data2 etc. values (you should pass them all at once as arrays or objects), but this should be enough for now.

You could add the database handle to the constructor of you class like this:

class InsertTodb {
  // Define property to store $dbc
  protected $dbc;

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

  // ...
}

Inside of your InsertTodb class access the database handle via $this->dbc, e.g. mysqli_query($this->dbc, $insquery).

In your code you have to pass $dbc1to your newly created object:

$ins = new InsertTodb($dbc1);