Validating and Saving a New Member (Advance PHP)
Submitted by GeePee on Thursday, April 16, 2015 - 23:13.
In this tutorial, will be focusing on using advance PHP through Object-Oriented Programming approach. Using OOP approach it provides a clear modular structure for programs which it good for defining abstract data types and it makes easy to modify existing code as new objects can be created with small differences to existing ones.
This time let’s create a new PHP file named “member.php” and save it inside includes folder. Then add the following code:
Next, open our register.php file then the code now will look like as shown below.
The code below, will initialize our the database connection and other database objects.
- <?php
- /**
- * Description: This is a class for member.
- * Author: Joken Villanueva
- * Date Created: Nov. 2, 2013
- * Revised By:
- */
- require_once(LIB_PATH.DS.'database.php');
- class member {
- protected static $tbl_name = "user_info";
- function db_fields(){
- global $mydb;
- return $mydb->getFieldsOnOneTable(self::$tbl_name);
- }
- function listOfautonumber(){
- global $mydb;
- $mydb->setQuery("Select * from ".self::$tbl_name);
- $cur = $mydb->loadResultList();
- return $cur;
- }
- static function bPrimary($id=0){
- global $mydb;
- $mydb->setQuery("SELECT * FROM ".self::$tbl_name." WHERE auto_id={$id} LIMIT 1");
- $row = $mydb->loadSingleResult();
- $s = $row->autostart + $row->incval;
- $a = $row->appenchar;
- return $a.$s;
- }
- static function bPrimaryUpdate($id=0){
- global $mydb;
- $mydb->setQuery("SELECT * FROM ".self::$tbl_name." WHERE auto_id={$id} LIMIT 1");
- $row = $mydb->loadSingleResult();
- $s = $row->autostart + $row->incval;
- return $s;
- }
- /*---Instantiation of Object dynamically---*/
- static function instantiate($record) {
- $object = new self;
- foreach($record as $attribute=>$value){
- if($object->has_attribute($attribute)) {
- $object->$attribute = $value;
- }
- }
- return $object;
- }
- /*--Cleaning the raw data before submitting to Database--*/
- private function has_attribute($attribute) {
- // We don't care about the value, we just want to know if the key exists
- // Will return true or false
- }
- protected function attributes() {
- // return an array of attribute names and their values
- global $mydb;
- foreach($this->db_fields() as $field) {
- if(property_exists($this, $field)) {
- $attributes[$field] = $this->$field;
- }
- }
- return $attributes;
- }
- protected function sanitized_attributes() {
- global $mydb;
- // sanitize the values before submitting
- // Note: does not alter the actual value of each attribute
- foreach($this->attributes() as $key => $value){
- $clean_attributes[$key] = $mydb->escape_value($value);
- }
- return $clean_attributes;
- }
- /*--Create,Update and Delete methods--*/
- public function save() {
- // A new record won't have an id yet.
- }
- public function create() {
- global $mydb;
- // Don't forget your SQL syntax and good habits:
- // - INSERT INTO table (key, key) VALUES ('value', 'value')
- // - single-quotes around all values
- // - escape all values to prevent SQL injection
- $attributes = $this->sanitized_attributes();
- $sql = "INSERT INTO ".self::$tbl_name." (";
- $sql .= ") VALUES ('";
- $sql .= "')";
- echo $mydb->setQuery($sql);
- if($mydb->executeQuery()) {
- $this->id = $mydb->insert_id();
- return true;
- } else {
- return false;
- }
- }
- public function update($id=0) {
- global $mydb;
- $attributes = $this->sanitized_attributes();
- foreach($attributes as $key => $value) {
- $attribute_pairs[] = "{$key}='{$value}'";
- }
- $sql = "UPDATE ".self::$tbl_name." SET ";
- $sql .= " WHERE auto_id=". $id;
- $mydb->setQuery($sql);
- if(!$mydb->executeQuery()) return false;
- }
- public function delete($id=0) {
- global $mydb;
- $sql = "DELETE FROM ".self::$tbl_name;
- $sql .= " WHERE auto_id=". $id;
- $sql .= " LIMIT 1 ";
- $mydb->setQuery($sql);
- if(!$mydb->executeQuery()) return false;
- }
- }
- ?>
- <?php
- require_once("includes/initialize.php");
- $f_name = $_POST['fName'];
- $l_name = $_POST['lName'];
- $email = $_POST['email'];
- $month = $_POST['month'];
- $day = $_POST['day'];
- $yr = $_POST['yr'];
- $gender = $_POST['gender'];
- $member = new member();
- $member->fName = $f_name;
- $member->lName = $l_name;
- $member->email = $email;
- $member->pword = $password;
- $member->mm = $month;
- $member->dd = $day;
- $member->yy = $yr;
- $member->gender = $gender;
- $member->create();
- ?>
- <script type="text/javascript">
- alert("New member added successfully.");
- window.location = "index.php"
- </script>
Comments
Add new comment
- Add new comment
- 97 views