thientanchuong Posted December 7, 2013 Share Posted December 7, 2013 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 I press "Add" button to process and get message confirmation However, all data are supposed to be in table category, are not avaiable 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. Quote Link to comment https://forums.phpfreaks.com/topic/284604-help-hidden-error-when-insert-data-into-database/ Share on other sites More sharing options...
jazzman1 Posted December 7, 2013 Share Posted December 7, 2013 Well, there is no data in this database/table. Can you post out your scripts here ( I don't use the rar archiver). Quote Link to comment https://forums.phpfreaks.com/topic/284604-help-hidden-error-when-insert-data-into-database/#findComment-1461567 Share on other sites More sharing options...
jcbones Posted December 7, 2013 Share Posted December 7, 2013 You need to post your query string and logic surrounding it, and your database structure. Quote Link to comment https://forums.phpfreaks.com/topic/284604-help-hidden-error-when-insert-data-into-database/#findComment-1461574 Share on other sites More sharing options...
thientanchuong Posted December 7, 2013 Author Share Posted December 7, 2013 (edited) This is the way I presented my files in folder 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 December 8, 2013 by thientanchuong Quote Link to comment https://forums.phpfreaks.com/topic/284604-help-hidden-error-when-insert-data-into-database/#findComment-1461621 Share on other sites More sharing options...
thientanchuong Posted December 8, 2013 Author Share Posted December 8, 2013 (edited) 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 December 8, 2013 by thientanchuong Quote Link to comment https://forums.phpfreaks.com/topic/284604-help-hidden-error-when-insert-data-into-database/#findComment-1461622 Share on other sites More sharing options...
jcbones Posted December 8, 2013 Share Posted December 8, 2013 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. Quote Link to comment https://forums.phpfreaks.com/topic/284604-help-hidden-error-when-insert-data-into-database/#findComment-1461627 Share on other sites More sharing options...
thientanchuong Posted December 8, 2013 Author Share Posted December 8, 2013 Thank you for your advise. What is the difference between mysqli and PDO ? at starter level , which one should I start first ? Quote Link to comment https://forums.phpfreaks.com/topic/284604-help-hidden-error-when-insert-data-into-database/#findComment-1461635 Share on other sites More sharing options...
thientanchuong Posted December 8, 2013 Author Share Posted December 8, 2013 (edited) 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 10Notice: Undefined variable: filename in C:\xampp\htdocs\RegistrationPage\admin\model\dbTest.php on line 12Error !!! do not know whatever error is (@_@) Edited December 8, 2013 by thientanchuong Quote Link to comment https://forums.phpfreaks.com/topic/284604-help-hidden-error-when-insert-data-into-database/#findComment-1461636 Share on other sites More sharing options...
maxxd Posted December 8, 2013 Share Posted December 8, 2013 Database::__construct() requires a string parameter named $filename that you don't pass in from mainClass::con2DB(). Quote Link to comment https://forums.phpfreaks.com/topic/284604-help-hidden-error-when-insert-data-into-database/#findComment-1461649 Share on other sites More sharing options...
jcbones Posted December 9, 2013 Share Posted December 9, 2013 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. Quote Link to comment https://forums.phpfreaks.com/topic/284604-help-hidden-error-when-insert-data-into-database/#findComment-1461884 Share on other sites More sharing options...
thientanchuong Posted December 10, 2013 Author Share Posted December 10, 2013 (edited) @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 December 10, 2013 by thientanchuong Quote Link to comment https://forums.phpfreaks.com/topic/284604-help-hidden-error-when-insert-data-into-database/#findComment-1461893 Share on other sites More sharing options...
jazzman1 Posted December 10, 2013 Share Posted December 10, 2013 Your paths to the directories/files are not correct according the print screen file attached to post #4. Quote Link to comment https://forums.phpfreaks.com/topic/284604-help-hidden-error-when-insert-data-into-database/#findComment-1461894 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.