Jump to content

Recommended Posts

Hi guys, I am doing add function following Model Control View style

 

There is a hidden error in my code but I could not find out. I filled data in form below

 

11249856926_990dd63cd1_o.jpg

 

I press "Add" button to process and get message confirmation

 

11249814965_48b836a54a_o.jpg

 

However, all data are supposed to be in table category, are not avaiable

 

11249856776_42a74d9aee_o.jpg

 

 

I attached my file in the link below, please feel free to download

 

http://www.mediafire.com/download/k49usjsb1dhmn3u/admin.rar

 

Please help me out with your solution

 

I really appreciate with your helps.
 

This is the way I presented my files in folder

 

11260166566_58a0be79ae_o.png

 

database.php

<?php

$host 		= "localhost";
$user		= "root";
$password	= "";
$db			= "vmdatabase";

class database 
{
	
	private $host;
	private	$user;
	private	$pass;
	private	$db;
	
	function __construct ()
	{
		$this->host = $host;
		$this->user = $user;
		$this->pass = $password;
		$this->db   = $db;
		
		//After declaring function dbConnect, we make it run
		$this->dbConnect();
	}
	
	private function dbConnect()
	{
		// Connecting to Server
		if (!mysql_connect($this->host, $this->user, $this->pass))
		// throwing error message when it is not connected to server
			throw new Exception ("ERROR: Server is not connected");
		
		// Connecting to Database
		if (!mysql_connect($this->db))
		// throwing error message when it is not connected to Database
			throw new Exception ("ERROR: Database is not connected");
	}
	
	function dbClose () 
	{
		mysql_close();	//Closing database connection
	}
}
		
?>

mainClass.php

<?php
	//Main class includes main functions
	class mainClass 
	{
		private $conDB;
		
		function __construct ($conDB)
		{
			//Connect to database
			$this->con2DB();
		}	
		
		function con2DB ()
		{
			include "model/database.php";
			$this->conDB = new database();
		}
		
		function close () 
		{
			$this->conDB->dbClose ();
		}
	}
?>

add.php

<?php
	class addDataClass extends mainClass
	{
		private $data;
		private $tbName;
		//private $conDB;
		
		public function __construct ($data, $tbName)
		{
			//Check File
			if (is_array($data))
			{
				$this->data   = $data;		//contribute id, name, des, etc in table
				$this->tbName = $tbName;	//table name in database
			} else
			{
				throw new Exception ("Error: Data must be in array");
			}
			
			// Inherit database connection and database close function from mainClass class
			// Connect to database
			$this->con2DB();
			// insert data into table	
			$this->addData();
			// Close database connection
			$this->close();
		}
		
		function addData()
		{
			foreach ($this->data as $key => $value)
			{
				$key[] 	 = $key;	
				$value[] = $value;
			}
			
			$tbKey      = implode ($key, ",");
			$dataValues = '"'. implode ($value, '","') . '"';
			
			$query = " INSERT INTO $this->tbName ($tbKey) 
							   VALUES ($dataValues) ";
			//				   
			if ($runQuery = mysql_query($query) )
			{
				return TRUE;	
			} else
			{
				throw new Exception ("Error: SQL Query can not be executed !!!");
				return FALSE;
			}
		}	
	}
?>

categoryController.php

<?php 
if ($_POST)
{
	if(isset($_POST['submit']) && $_POST['submit'] == "Add")
	{
		$cat['CatName'] = $_POST['CatName'];
		$cat['CatDescription'] 	= $_POST['CatDescription'];
		
		try
		{
			include ("model/mainClass.php");
			include ("model/add.php");
			$addCat = new addDataClass ($cat, "category");
			
			if ($addCat == TRUE)
			{
				echo "New Category is added";	
			}
		}catch (Exception $err)
		{
			echo $err-> getMessage(); 
		}
	}
} else 
{
	include "view/addNewCategory.php";
}
?>
Edited by thientanchuong

 

addCategory.php in View folder

<link rel="stylesheet" type="text/css" href="css/form.css">
<form class="formAdd" action="" method="post">
	<div class="formtitle">Add Category</div>
	<div class="input">
		<div class="inputtext">Category Name: </div>
		<div class="inputcontent">
			<input name="CatName" type="text" />
		</div>
	</div>
	<div class="inputtextbox nobottomborder">
		<div class="inputtext">Category Description: </div>
		<div class="inputcontent">
			<textarea  name="CatDescription" class="textarea"></textarea>
		</div>
	</div>
	<div class="buttons">
		<input class="btn" type="submit" value="Add" name="submit" />
	</div>
</form>
Edited by thientanchuong

Curious that you are not using the mysqli or PDO classes.  Mysql is deprecated, and will likely be removed in future versions.

Having stated that, I would start out with putting more information in my error's.  That would most likely lead you in the right direction.  Dropping a mysql_error() in there (after the query) would be the starting point.

I changed and added some line of code
 

dbTest.php in model folder
 

<?php
   
class Database 
{
    private $host;
    private $user;
    private $pass;
    private $db;
    
    public function __construct($filename) 
    {
		if (is_file($filename))
		{
			include "./include/hostData.php";	
		} else {
			throw new Exception ("Error: file is not found");
		}
        $this->host = $host;
        $this->user = $user;
        $this->pass = $pass;
        $this->db   = $db;
        
        //After seting up function con2DB, construct the function
        $this->dbCon();
    }
    
    private function dbCon()
    {
        //Connect to Server
        $conServer = mysqli_connect($this->host, $this->user, $this->pass);
        //If there is a problem with server connection, error message is called
        if ($conServer)
        {            
            return TRUE;
        } else {
            throw new Exception("Error: Server is not connected");
            echo mysql_errno($conServer) . ": " . mysql_error($conServer). "\n";
        }
        //Connect to Database
        $conDatabase = mysqli_select_db($this->db);               
        //If there is a problem, with database connection, error message is called
        if ($conDatabase)
		{
			return TRUE;
		} else
        {
            throw new Exception("Error: Database is not connected");
            echo mysql_errno($conDatabase) . ": " . mysql_error($conDatabase). "\n";
        }
    }
    
    function dbClose ()
    {
        mysql_close();
    }
}
?>

add.php in model folder

<?php
	class addDataClass extends mainClass
	{
		private $data;
		private $tbName;
		
		public function __construct ($data, $tbName)
		{
			//Check File
			if (is_array($data))
			{
				$this->data   = $data;		//contribute id, name, des, etc in table
				$this->tbName = $tbName;	//table name in database
			} else
			{
				throw new Exception ("Error: Data must be in array");
			}
			
			// Inherit database connection and database close function from mainClass class
			// Connect to database
			$this->con2DB();
			// insert data into table	
			$this->addData();
			// Close database connection
			$this->close();
		}
		
		function addData()
		{
			foreach ($this->data as $key => $value)
			{
				$key[] 	 = $key;	
				$value[] = $value;
			}
			
			$tbKey      = implode ($key, ",");
			$dataValues = '"'. implode ($value, '","') . '"';
			
			$query = " INSERT INTO $this->tbName ($tbKey) 
							   VALUES ($dataValues) ";
			//				   
			if ($runQuery = mysqli_query($query) )
			{
				return TRUE;	
			} else
			{
				echo mysql_errno($runQuery) . ": " . mysql_error($runQuery) . "\n";
				return FALSE;
			}
		}	
	}
?>

mainClass.php in model folder

<?php
	//Main class includes main functions
	class mainClass 
	{
		private $conDB;
		
		function __construct ($conDB)
		{
			//Connect to database
			$this->con2DB();
		}	
		
		function con2DB ()
		{
			include "model/dbTest.php";
			include "./include/hostData.php";	
			$this->conDB = new database();
		}
		
		function close () 
		{
			$this->conDB->dbClose ();
		}
	}
?>

hostData.php in include folder

<?php
$host       = "localhost";
$user       = "root";
$pass       = "";
$db         = "vmdatabase";
?>

categoryController.php in controller folder

<?php 
if ($_POST)
{
	if(isset($_POST['submit']) && $_POST['submit'] == "Add")
	{
		$cat['CatName'] = $_POST['CatName'];
		$cat['CatDescription'] 	= $_POST['CatDescription'];
		
		try
		{
			include ("model/mainClass.php");
			include ("model/add.php");
			//$cat gets data values
			//"category" is a name of table in database
			$addCat = new addDataClass ($cat, "category"); 
			
			if ($addCat == TRUE)
			{
				echo "New Category is added";	
			}
		}catch (Exception $err)
		{
			echo $err-> getMessage(); 
		}
	}
} else 
{
	include "view/addNewCategory.php";
}
?>

and Errors that I got after press button "Add" are

 

 

Warning: Missing argument 1 for Database::__construct(), called in C:\xampp\htdocs\RegistrationPage\admin\model\mainClass.php on line 17 and defined in C:\xampp\htdocs\RegistrationPage\admin\model\dbTest.php on line 10

Notice: Undefined variable: filename in C:\xampp\htdocs\RegistrationPage\admin\model\dbTest.php on line 12
Error !!! do not know whatever error is (@_@)

Edited by thientanchuong

 

 

Thank you for your advise. What is the difference between mysqli and PDO ? at starter level , which one should I start first ?

 

Mysqli is used for MySQL databases only.

PDO can be used for most any database.
 

Deciding which one to use is up to the user.  If you use different databases, then of course get comfortable with PDO.

@jcbones thanks for your infor,

 

@maxxd thanks for your help, could you help me with a solution please? I tried to remove $filename and keep "./include/dbTest.php" but it does work.

Edited by thientanchuong
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.