5. How to save data to the database?

Before you begin, please review to the following topics: 
How to display a webpage using MVC?
How to create database table using PHPMyAdmin?
1.In jobseeker_signup_view.php, type the following:

<html>
<head>
            <title>Jobseeker Signup | BizjobFinder</title>
            <link href='/bizjobfinder.com/css/jobseeker_signup.css' rel='stylesheet' type='text/css'>
</head>
<body>
<div><h4>Free Signup</h4></div>
<hr>
<form action="jobseekers.php?model=jobseeker-save" method="POST">
<div>
            <?php
                        if(isset($_SESSION['user']['msg'])){
                                    echo $_SESSION['user']['msg'];
                                    unset($_SESSION['user']['msg']);          
                        }
            ?>
</div>
<div class='form-row'>
            <span class='label'>Firstname:</span>
            <input type='text' name='firstname'>
</div>
<div class='form-row'>
            <span class='label'>MI:</span>
            <input type='text' name='mi'>
</div>
<div class='form-row'>
            <span class='label'>Lastname:</span>
            <input type='text' name='lastname'>
</div>
<div class='form-row'>
            <span class='label'>Street/Barangay:</span>
            <input type='text' name='street_barangay'>
</div>
<div class='form-row'>
            <span class='label'>Town/City:</span>
            <input type='text' name='town_city'>
</div>
<div class='form-row'>
            <span class='label'>Province:</span>
            <input type='text' name='province'>
</div>
<div class='form-row'>
            <span class='label'>&nbsp;</span>
            <input type='submit' name='submit' value='Signup'>
</div>
</form>
</body>
</html>


2. In jobseeker_signup.css, please type the following:

.form-row {padding:5px;}
.label {display:block;width:100px;float:left;text-align:right;margin-right:5px;}


3. In jobseekers.php, please type the following:

<?php  session_start();
/*
*Filename:jobseekers.php
*Projectname:BizjobFinder.com
*Date created:November 18, 2011
*Created by:Mario T. Silvano
*/
?>
<?php
            class jobseekers{                     
                        //views---------------
                        public function signup(){
                        return 'views/jobseeker_signup_view';
                        }
                       
                        //models--------------
                        public function save(){
                        //save....
                        require_once($_SERVER['DOCUMENT_ROOT'].
                        '/bizjobfinder.com/application/models/jobseeker_model.php');
                        $jobseeker=new jobseeker_model();
                        $jobseeker->firstname=$_POST['firstname'];
                        $jobseeker->mi=$_POST['mi'];
                        $jobseeker->lastname=$_POST['lastname'];
                        $jobseeker->street_barangay=$_POST['street_barangay'];
                        $jobseeker->town_city=$_POST['town_city'];
                        $jobseeker->province=$_POST['province'];
                        //saving part....
                        return $jobseeker->save($jobseeker);
                        }
            }
           
            $obj=new jobseekers();
            $view='';
            $model='';
            //views---------------------
            if(!empty($_GET['view'])){
                        $view=$_GET['view'];
                        switch($view){
                                    case 'jobseeker-signup':
                                    $page=$obj->signup();
                                    break;
                        }
                        include $_SERVER['DOCUMENT_ROOT'].'/bizjobfinder.com/application/'.$page.'.php';   
            }
            else{
            //models-----------------     
                        if(!empty($_GET['model'])){
                                    $model=$_GET['model'];
                                    switch($model){
                                                case 'jobseeker-save':
                                                            $result=$obj->save();
                                                            if($result)
                                                                        $msg="Successfully Save...";
                                                            else
                                                                        $msg="Error Save...";
                                                            $_SESSION['user']['msg']=$msg;          
                                                            header('Location:jobseekers.php?view=jobseeker-signup');
                                                break;
                                    }
                        }
            }
/*
*End of file: jobseekers.php
*Location: /application/controllers/jobseekers.php
*/


4. In jobseeker_model.php, please type the following:

<?php
/*
*Filename:jobseeker_model.php
*Project:Bizjobfinder.com
*Date Created: Nov. 25, 2011
*Created By: Mario T. Silvano a.k.a strikermode
*/
?>
<?php
define("DB_SERVER","localhost");
define("DB_USER","root");
define("DB_PASS","");
define("DB_NAME","bizjobfinder_db");
class jobseeker_model{
            public $id;        
            public $firstname;         
            public $mi;       
            public $lastname;         
            public $street_barangay;           
            public $town_city;
            public $province;
           
            function __construct(){}
           
            public function save($jobseeker){
            //sql statement...
            $sql="INSERT INTO jobseekers
                                    (
                                    firstname,
                                    mi,
                                    lastname,
                                    street_barangay,
                                    town_city,
                                    province
                                    )
                                    VALUES
                                    (
                                    '{$jobseeker->firstname}',
                                    '{$jobseeker->mi}',
                                    '{$jobseeker->lastname}',
                                    '{$jobseeker->street_barangay}',
                                    '{$jobseeker->town_city}',
                                    '{$jobseeker->province}'
                                    )";
            $db=$this->_open_connection();           
            $result=mysql_query($sql,$db);
            return $this->_confirm_query($result);
                                                                                               
            }
            private function _open_connection(){
                        $con=mysql_connect(DB_SERVER,DB_USER,DB_PASS);
                        if(!$con)die('Error Connection: '.mysql_error());
                        $db_select=mysql_select_db(DB_NAME,$con);
                        if(!$db_select)die('Error Selection: '.mysql_error());
                        return $con;
            }
            private function  _confirm_query($result){
                        if(!$result)die('Error Query:'.mysql_error());
                        return $result;
            }
}
/*
*End of file jobseeker_model.php
*Location:../application/models/jobseeker_model.php
*/


5. On index.php please type the following:
<html>
<head>
            <title>Home | BizjobFinder</title>
</head>
<body>
<a href='application/controllers/jobseekers.php?view=jobseeker-signup'>Jobseeker Signup</a>
</body>
</html>


6. On your browser, type the following:
http://localhost/bizjobfinder.com/






6 comments:

  1. Hello,
    I am a student of PHP programming, and I am making a user profile page. To do this I need some of your code, I've been testing all your explanation of MVC, but I can not run your example.
    I do not understand exactly your way to create the CONTROLLER, or how you call the VIEWS.
    Please, could you send me all the project that you put as an example? I guess it's the only way, and that I can not run or the explanation number 3 ...


    Thanks for everything
    Oriol

    ReplyDelete
    Replies
    1. Oriol,
      The CONTROLLER is your page to call the VIEW and the MODEL. If you look at it, your VIEW page is called by the CONTROLLER (notice this code: include $_SERVER['DOCUMENT_ROOT'] .'/bizjobfinder.com/application/'.$page.'.php';)
      You are actually embedding the VIEW as you call it.

      Delete
  2. This comment has been removed by the author.

    ReplyDelete
  3. Awesome Turorial Codes Working Nice.........

    ReplyDelete
  4. This comment has been removed by the author.

    ReplyDelete
  5. How Use Custom Validation like jquerey or jscript jobseekers page

    ReplyDelete