PHP
HEllo world, i need some help here plz.

i want to make an addressbook and want to have an index file that
includes/requires all the other files in the include folder(the include folder contains all the functionality) so that i dont have to manually include files in other files.

The code is an example, of what i want i to be like, for instance
db_access.include.php includes db_connect.include.php which i dont want... i just want somehow to include db_connect.include.php in the index file and have it accessible every where.

sorry for my english..
regards

db_connect.include.php is given below
<?php
	$server="localhost";
	$user="root";
	$pass="";
	
	global $connection;
	$connection = mysql_pconnect($server,$user,$pass) or die("Error connecting to the server");
	
	$database = "addressbook";
	
	mysql_select_db($database) or die("No such database exists");
	
	print("Connected to the database");
	print("<br />");
?>

db_access.include.php is given below
<?php
include '/include/db_connect.include.php';
function insert($data,$table, $connection){
$query="insert into $table ('name','phone','email','website','notes') values('$data[name]','$data[phone]','$data[email]','$data[websites]','$data[notes]')";
var_dump("Inside db_access.include.php" . $connection);
if (!mysql_query($query,$connection))
  {
  die('Error: ' . mysql_error());
  }
print('1 record added');

}
?>

Recommended Answers

All 5 Replies

thanks for the reply... the __autoload function requires one argument, isnt there any other way just to load all the files, i mean why cant i just include or require all the include files and then be able to access their functionality on other pages???

thanks for the reply... the __autoload function requires one argument, isnt there any other way just to load all the files, i mean why cant i just include or require all the include files and then be able to access their functionality on other pages???

You never call the __autoload function, it is automatically called when you try to use a class. So all you have to do is include the file that defines the __autoload function and any time you try to instantiate an undefined class it will call the autoload function.

if you are that lazy that you dont want to type out each include/require, or use autoload, then write a function that

Reads the directory http://php.net/manual/en/function.readdir.php
include each file that has the extension you want http://us.php.net/manual/en/function.require-once.php

which is basically what autoload does

I think im not explaining it correctly what im looking for or i dont understand php that much... So below is the index.php that loads initially when i type .... http://localhost/addressbook

index.php

<?php
	
	require_once("include/db_connect.include.php");
	require_once("include/db_access.include.php");
	require_once("addressbook.php");
		
?>

include/db_connect.include.php

<?php
	$server="localhost";
	$user="root";
	$pass="";
	
	global $connection;
	$connection = mysql_pconnect($server,$user,$pass) or die("Error connecting to the server");
	
	$database = "addressbook";
	
	mysql_select_db($database) or die("No such database exists");
	
	print("Connected to the database");
	print("<br />");
?>

include/db_access.include.php

<?php

function insert($data,$table, $connection){
$query="insert into $table ('name','phone','email','website','notes') values('$data[name]','$data[phone]','$data[email]','$data[websites]','$data[notes]')";
var_dump("Inside db_access.include.php" . $connection);
if (!mysql_query($query,$connection))
  {
  die('Error: ' . mysql_error());
  }
print('1 record added');
}
?>

addressbook.php

<?php
	
	if(!empty($_POST)){// only go in this code when the user enters some thing in it.
		
		$name = $_POST['name'];
		$phone = $_POST['phone'];
		$email = $_POST['email'];
		$websites = $_POST['websites'];
		$notes = $_POST['notes'];
		
		// create an associative array to pass it to the insert function
		$data = array("name"=>$name, "phone"=>$phone, "email"=>$email, "websites"=>$websites, "notes"=>$notes);
		$tablename = "contact";
		
		insert($data,$tablename, $connection);
	}
	
	
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>AddressBook</title>
<link href="oneColElsCtrHdr.css" rel="stylesheet" type="text/css" />
</head>

<body class="oneColElsCtrHdr">

    <div id="container">
      <div id="header">
       <!-- end #header --></div>
      
          <div id="mainContent">
            <div id="form_box">
              <h1 align="center">Address Book</h1>
                                        
                          <form id="myform" name='myform' method="post" action="addressbook.php">
                                <!--<input type="button" onclick="ajaxFunction()" value="Query MySQL" style="position:relative; top:250px; left:200px;"/> -->
                                <div id="name" class="myformdiv">
                                    <label for="name">Name</label>
                                    <input type="text" name="name" id="name">
                                </ div>
                                <br />
                                <br />
                               <div id="phone" class="myformdiv">
	                                <label for="phone">Phone</label>
    	                            <input type="text" name="phone" id="phone">
                               </div> 
                               <br />
                               <br />
                               <div id="email" class="myformdiv">
	                                <label for="email">Email</label>
    	                            <input type="text" name="email" id="email">
                               </div> 
                               <br />
                               <br />
                               <div id="websites" class="myformdiv">
	                                <label for="websites">Websites</label>
    	                            <input type="text" name="websites" id="websites">
                               </div> 
                               <br />
                               <br />
                               <div id="notes" class="myformdiv">
	                                <label for="notes">Notes</label>
    	                            <input type="text" name="notes" id="notes">
                               </div>
                               
                               
	             <input id="sumbit" class="buttonn" type="submit" value="Sumbit" /> <input id="reset" class="buttonn" type="reset" value="Reset" />
                            
                          </form>
                 </div> 
                
         </div>
       
                
    </div>
</body>
</html>

This code generates an error
Fatal error: Call to undefined function insert() in C:\xampp\xampp\htdocs\addressbook\addressbook.php on line 15

because it cant find the insert function because i havent included db_connect.include.php in db_access.include.php,, but i dont want that i just want to include db_connect.include.php in index.php and make the insert function work,, could anyone plz explain me how that would be..

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.