Reputation: 3033
I have a following main files:
index.php:
<?php
include_once("inc/config.php");
$view = $_GET["view"];
include_once "template.php";
?>
config.php:
define("HOST","localhost");
define("USER","root");
define("PASS","*******");
define("DATABASE","abc");
$mysqli = new mysqli(HOST,USER,PASS,DATABASE);
if (mysqli_connect_errno()) {
printf("Connect failed: %s\n", mysqli_connect_error());
exit();
}
view/create_account.php:
<?php
include_once(CLASS_PATH."create_account.php");
$acc = new account();
if((isset($_POST)) and ($_REQUEST['mode']=="insert"))
{
$acc->insert_orginfo();
}
?>
class/create_account.php:
<?php
class account
{
function __construct () {
}
/* ---- insert organization info into table ---- */
function insert_orginfo()
{
extract($_POST);
$query = "insert into `organisationinfo`
(`org_name`, `addr1`, `addr2`, `city`, `state`, `country`, `pin`, `tax_number`, `url`)
values
('$org_name', '$add1', '$add2', '$city', '$state', '$country', '$pincode', '$tax_no', '$url')";
$mysqli->query($query);
if (mysqli_connect_errno()) {
printf("Connect failed: %s\n", mysqli_connect_error());
exit();
}
return $mysqli->insert_id;
}
}
?>
index.php
file include config.php
and template.php
file and using template.php
file i include view file(exa. view/create_account.php). using view file i call the function of class.
now, the problem is data is not inserted.if i add config.php
file in class/create_account.php
then it is working.But this class file and view file is finally included in index.php
and in index.php
file config file is included.
means index.php file include template.php file. template.php file include view/create_account.php file and view/create_account.php file include class/create_account.php
so what is the problem here?
Thanks in advance.
Upvotes: 0
Views: 101
Reputation: 1007
try this also
/* ---- insert organization info into table ---- */
function insert_orginfo()
{
extract($_POST);
global $mysqli;
$query = "insert into organisationinfo (org_name, addr1, addr2, city, state, country, pin, tax_number, url) values ('$org_name', '$add1', '$add2', '$city', '$state', '$country', '$pincode', '$tax_no', '$url')";
$mysqli->query($query);
if (mysqli_connect_errno()) {
printf("Connect failed: %s\n", mysqli_connect_error());
exit();
}
return $mysqli->insert_id;
}
Upvotes: 0
Reputation: 17710
This is a scope question. mysqli is not "in scope" in the insert function.
Fastest solution is to make it "global". http://php.net/manual/en/language.variables.scope.php - see the section on the "global" keyword.
function insert_orginfo()
{
$query = "insert into `organisationinfo` (`org_name`, `addr1`, `addr2`, `city`, `state`, `country`, `pin`, `tax_number`, `url`) values ('$org_name', '$add1', '$add2', '$city', '$state', '$country', '$pincode', '$tax_no', '$url')";
global $mysqli; // bring MySQL into scope.
$mysqli->query($query);
if (mysqli_connect_errno()) {
printf("Connect failed: %s\n", mysqli_connect_error());
exit();
}
return $mysqli->insert_id;
Upvotes: 1