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.