Jump to content

thientanchuong

Members
  • Posts

    14
  • Joined

  • Last visited

Profile Information

  • Gender
    Not Telling

thientanchuong's Achievements

Newbie

Newbie (1/5)

0

Reputation

  1. @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.
  2. 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
  3. Thank you for your advise. What is the difference between mysqli and PDO ? at starter level , which one should I start first ?
  4. 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>
  5. 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"; } ?>
  6. 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.
  7. Sorry, I do not know why it does not load the two photos in previous post and there is no edit function so people can see my photos of coding problem overhere
  8. Hi guys, I am studying on how to code php n mysql by Model Control View concept First of all, I created some files and some folder containers as the picture below My main point to add category data into my database so I have database.php in model folder categoryController.php in controller folder addCategory.php in categoryController.php, I got first problem to open a stream to where I stated in code then I even remove triple dots in line 4 but It does not work. Could any one help me out with the solution please ? Kind Regards
×
×
  • 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.