You are on page 1of 10

Ramon Magsaysay Memorial College

LABORATORY EXERCISE
____ SEMESTER: AY: _________

Name: Marjun C. Paghid Schedule: Wednesday-Thursday Score: ________


Subject: Fundamentals of Database System Instructor: _Jim Jamero________ Date: _________
LABORATORY EXERCISE 6
LOGIN

Learning Objectives
 To visualize and understand how to use SQL statement for user login.
 To determine what SQL script use in implementing login or access form.

Prerequisite student experiences and knowledge


Database management has evolved from a specialized computer application to a central
component of a modern computing environment. As a result, knowledge about database
systems has become an essential part of computer science.

Background
SQL (Structured Query Language) is a nonprocedural language, and you specify what
you want, not how to get it. A block-structured format of English keywords uses in this Query
language. It has the following components. The SQL DDL (Data Definition Language) provides a
command for defining relation schemas, deleting relations, and modifying relation schema.
DML (Data Manipulation Language) includes commands to insert tuples into, delete tuples
from and modify tuples in the Database. Embedded and Dynamic SQL define how SQL
statements can embed within general-purpose programming languages, such as C, C++, JAVA,
COBOL, Pascal, and Fortran.

Materials/Resources
 PC/Internet
 Pen
 Programming Application (Visual Studio, Netbeans)
 MySQL (Xampp, Wampp & MySQL)
 Web Browser (Internet Explorer, Mozilla, Google Chrome, Etc.)
 Word-processing program

Laboratory Activity
Instructions: Perform the following steps.
1. Open the program Xampp
2. Click the Start for Apache and MySQL
3. Then, click MySQL Admin
4. Select Database, then click the Database created on laboratory exercise 1/2
5. Select the SQL menu, then
6. Type
CREATE TABLE Users (
User_ID int(15) primary,

FUNDAMENTALS OF DATABASE SYSTEMS 2 | Jim S. Jamero, MIT 1


Ramon Magsaysay Memorial College
LABORATORY EXERCISE
____ SEMESTER: AY: _________

Name: Marjun C. Paghid Schedule: Wednesday-Thursday Score: ________


Subject: Fundamentals of Database System Instructor: _Jim Jamero________ Date: _________
name char(128),
username char(64),
password char(64));
7. Insert data like the sample below:

User_ID Name Username Password


1 Xavier Royce Admin Admin101
2 Chi Tui Tuichi Manager101
3 Girl In Mabho MabhoInGirl Manager102
4 Charles Sagittarius SagiCha Manager103
5 Jesso Celestial AngBu Manager104

8. Then, open software for programming to create a form login

9. Then build a syntax for the user can log in to the form
10. If the user login on the form, if the username and password inputted by the user
have existed in the table using the message will appear "Welcome!", hence incorrect
the message will appear "Username/Password Invalid."
11. Display the result.
12. Save and Print the result in the page provided.

FUNDAMENTALS OF DATABASE SYSTEMS 2 | Jim S. Jamero, MIT 2


Ramon Magsaysay Memorial College
LABORATORY EXERCISE
____ SEMESTER: AY: _________

Name: Marjun C. Paghid Schedule: Wednesday-Thursday Score: ________


Subject: Fundamentals of Database System Instructor: _Jim Jamero________ Date: _________
QUESTIONS

1. What method did you use to connect to the Database?


- “database' => 'databaseactivity1.,” I just insert in the name of the database in the config-
>database.php and call the database table in the models “$query = $this->db-
>get('users');”. It automatically gets the data from the database and compare to the
inputted data from the user if the data they input is in the database, if not it will display
"Username/Password Invalid.".

2. What Error or problem did you encounter? And how did you resolve the issue?
- The error I encountered first is I forget to create a function to display the conditions
so when I got wrong input, there’s no indication of wrong username/password. The
solution I made was I just created a condition where if the input username or
password is correct so it will return true and redirect to the other link page and
display Welcome! Else, it will display username/password invalid.

3. Indicate the full syntax and explain it?


login_view.php
<!DOCTYPE html>  
<html lang="en">  
<head>
<meta charset="utf-8">
<title>Login Page</title>
</head>  
<body>  
<h1>Login</h1>  
<?php  
echo form_open('sample/login_action');  // it gets and validatates the data
echo validation_errors();  
// inputs the username/password
echo "<p>Username: ";  
echo form_input('username', $this->input->post('username'));  
echo "</p>";  

  echo "<p>Password: ";  


echo form_password('password');  

FUNDAMENTALS OF DATABASE SYSTEMS 2 | Jim S. Jamero, MIT 3


Ramon Magsaysay Memorial College
LABORATORY EXERCISE
____ SEMESTER: AY: _________

Name: Marjun C. Paghid Schedule: Wednesday-Thursday Score: ________


Subject: Fundamentals of Database System Instructor: _Jim Jamero________ Date: _________
echo "</p>";  
echo "</p>";  

   echo form_submit('login_submit', 'Login');  


echo "</p>";  

  echo form_close();  

  ?>  
</body>  

</html>

Data.php
<!DOCTYPE html>  
<html>  
<head>
<title></title>
</head>  
<body>  

   <h1>Welcome!</h1>  //displays welcome when the input data are matched to the
database or correct input data

  <?php
echo "<pre>";  
echo print_r($this->session->all_userdata());  
echo "</pre>";  
?>  

  <a href='<?php echo base_url()."index.php/sample/logout"; ?>'>Logout</a>  


</body>  

</html>  

FUNDAMENTALS OF DATABASE SYSTEMS 2 | Jim S. Jamero, MIT 4


Ramon Magsaysay Memorial College
LABORATORY EXERCISE
____ SEMESTER: AY: _________

Name: Marjun C. Paghid Schedule: Wednesday-Thursday Score: ________


Subject: Fundamentals of Database System Instructor: _Jim Jamero________ Date: _________
Sample.php
<?php

   class sample extends CI_Controller {

      public function index()  

  { 

        $this->login();  

  } 

    public function login()  

  { 

        $this->load->view('login_view');  

  } 

    public function data()  

  { 

        if ($this->session->userdata('currently_logged_in'))   //display the data information

    { 

            $this->load->view('data');  

        } else {  

            redirect('sample/invalid');  

    } 

  } 

    public function invalid()  

  { 

        $this->load->view('invalid');  

  } 

    public function login_action()  

  { 

FUNDAMENTALS OF DATABASE SYSTEMS 2 | Jim S. Jamero, MIT 5


Ramon Magsaysay Memorial College
LABORATORY EXERCISE
____ SEMESTER: AY: _________

Name: Marjun C. Paghid Schedule: Wednesday-Thursday Score: ________


Subject: Fundamentals of Database System Instructor: _Jim Jamero________ Date: _________
        $this->load->helper('security');  

        $this->load->library('form_validation');  

        $this->form_validation->set_rules('username', 'Username:', 'required|trim|xss_clean|


callback_validation');  

        $this->form_validation->set_rules('password', 'Password:', 'required|trim');  

        if ($this->form_validation->run())  

    { 

            $data = array(  

                'username' => $this->input->post('username'),  

                'currently_logged_in' => 1  

                );    

                    $this->session->set_userdata($data);  

                redirect('sample/data');  

    } 

        else {  

            $this->load->view('login_view');  

    } 

  } 

    public function validation()  

  { 

        $this->load->model('sample_model');  

        if ($this->sample_model->log_in_correctly())  

    { 

FUNDAMENTALS OF DATABASE SYSTEMS 2 | Jim S. Jamero, MIT 6


Ramon Magsaysay Memorial College
LABORATORY EXERCISE
____ SEMESTER: AY: _________

Name: Marjun C. Paghid Schedule: Wednesday-Thursday Score: ________


Subject: Fundamentals of Database System Instructor: _Jim Jamero________ Date: _________
            return true;  

        } else {  

            $this->form_validation->set_message('validation', 'Username/Password Invalid.');  

            return false;  

    } 

  } 

    public function logout()  

  { 

        $this->session->sess_destroy();  

        redirect('sample/login');  

  } 

?>  

Sample_model.php
<?php

class sample_model extends CI_Model{

    /* Function to require set of data and save to the users table in database*/

    public function log_in_correctly()

  {

        $this->db->where('username', $this->input->post('username'));  

        $this->db->where('password', $this->input->post('password'));  

        $query = $this->db->get('users');  

        if ($query->num_rows() == 1)  

    { 

            return true;  

        } else {  

FUNDAMENTALS OF DATABASE SYSTEMS 2 | Jim S. Jamero, MIT 7


Ramon Magsaysay Memorial College
LABORATORY EXERCISE
____ SEMESTER: AY: _________

Name: Marjun C. Paghid Schedule: Wednesday-Thursday Score: ________


Subject: Fundamentals of Database System Instructor: _Jim Jamero________ Date: _________
            return false;  

    } 

  }

FUNDAMENTALS OF DATABASE SYSTEMS 2 | Jim S. Jamero, MIT 8


Ramon Magsaysay Memorial College
LABORATORY EXERCISE
____ SEMESTER: AY: _________

Name: Marjun C. Paghid Schedule: Wednesday-Thursday Score: ________


Subject: Fundamentals of Database System Instructor: _Jim Jamero________ Date: _________
Output / Results

FUNDAMENTALS OF DATABASE SYSTEMS 2 | Jim S. Jamero, MIT 9


Ramon Magsaysay Memorial College
LABORATORY EXERCISE
____ SEMESTER: AY: _________

Name: Marjun C. Paghid Schedule: Wednesday-Thursday Score: ________


Subject: Fundamentals of Database System Instructor: _Jim Jamero________ Date: _________
Conclusion

- We can possibly connect different database in a html login form type using different
techniques on how we call the database to connect the data and save/or get the
information to display. Database is important when we want to store various data and when
can use it for different purposes.

FUNDAMENTALS OF DATABASE SYSTEMS 2 | Jim S. Jamero, MIT 10

You might also like