Lab 7 Introduction to MySQL

Similar documents
By the end of this section of the practical, the students should be able to:

ITS331 IT Laboratory I: (Laboratory #11) Session Handling

home.php 1/1 lectures/6/src/ include.php 1/1 lectures/6/src/

Database Connectivity using PHP Some Points to Remember:

WEB PROGRAMMING SCV1223. PHP : Authentication Example. Dr. Md Sah bin Hj Salam En. Jumail bin Taliba

PHP Tutorial 6(a) Using PHP with MySQL

COM1004 Web and Internet Technology

Introductory workshop on PHP-MySQL

COMP519: Web Programming Autumn 2015

LAMP Apps. Overview. Learning Outcomes: At the completion of the lab you should be able to:

Lecture 6 Session Control and User Authentication. INLS 760 Web Databases Spring 2013 Rob Capra

Enter Grade Report. No Place Like Gilbert Duenas. California State University San Marcos. Johnny Koons. CS 441 Software Engineering

Hello everyone! Page 1. Your folder should look like this. To start with Run your XAMPP app and start your Apache and MySQL.

Retrieving Query Results

PHP: Cookies, Sessions, Databases. CS174. Chris Pollett. Sep 24, 2008.

What is MySQL? [Document provides the fundamental operations of PHP-MySQL connectivity]

Introduction to relational databases and MySQL

PHP Development - Introduction

Chapter 3. Introduction to relational databases and MySQL. 2010, Mike Murach & Associates, Inc. Murach's PHP and MySQL, C3

Executing Simple Queries

IELM 511 Information Systems Design Labs 5 and 6. DB creation and Population

Programmer s Guide for PHP

c360 Web Connect Configuration Guide Microsoft Dynamics CRM 2011 compatible c360 Solutions, Inc. c360 Solutions

Chapter 6 Part2: Manipulating MySQL Databases with PHP

Create Basic Databases and Integrate with a Website Lesson 3

Using htmlarea & a Database to Maintain Content on a Website

Understanding Basic SQL Injection

User authentication, passwords

More loops. Control structures / flow control. while loops. Loops / Iteration / doing things over and over and over and over...

EXPERIMENT- 9. Login.html

SMS GATEWAY API INTEGRATION GUIDE

Daniel Pittman October 17, 2011

Copyright 2011 Sakun Sharma

Create Basic Databases and Integrate with a Website Lesson 5

Web Programming. Dr Walid M. Aly. Lecture 10 PHP. lec10. Web Programming CS433/CS614 22:32. Dr Walid M. Aly

PHP Introduction. Some info on MySQL which we will cover in the next workshop...

Mastering phpmyadmiri 3.4 for

Technical Guide Login Page Customization

A SQL Injection : Internal Investigation of Injection, Detection and Prevention of SQL Injection Attacks

A340 Laboratory Session #17

Advanced Web Programming Practice Exam II

Lab 4: Basic PHP Tutorial, Part 2

A QUICK GUIDE TO PROGRAMMING FOR THE WEB. ssh (then type your UBIT password when prompted)

ABSOLUTE FORM PROCESSOR ADMINISTRATION OPTIONS

Options. Real SQL Programming 1. Stored Procedures. Embedded SQL

Introduction to PHP. Fulvio Corno, Laura Farinetti, Dario Bonino, Marco Aime 06/11/08

School of Information and Computer Technology Sirindhorn International Institute of Technology Thammasat University

HTML Tables and Forms. Outline. Review. Review. Example Demo/ Walkthrough. CS 418/518 Web Programming Spring Tables to Display Data"

Member registration and searching(2)

Lab # 2 Hands-On. DDL Basic SQL Statements Institute of Computer Science, University of Tartu, Estonia

n A m I B I A U n I V ER SI TY OF SCIEnCE AnD TECHnOLOGY

Secure Web-Based Systems Fall Test 1

Data Modelling and Databases. Exercise Session 7: Integrity Constraints

// 範例 4-1: 連結資料庫 (connectdb.php) <?php mysql_connect("localhost", "student", "Asia2013"); mysql_select_db("student");?>

If you do not specify any custom parameters, we will deliver the message using the default names.

COSC344 Database Theory and Applications PHP & SQL. Lecture 14

CREATE A SERVLET PROGRAM TO DISPLAY THE STUDENTS MARKS. To create a servlet program to display the students marks

PHP 5 if...else...elseif Statements

HTML Forms IT WS I - Lecture 11

Web Systems Nov. 2, 2017

CSC 405 Computer Security. Web Security

blink.html 1/1 lectures/6/src/ form.html 1/1 lectures/6/src/

CSC 405 Computer Security. Web Security

LAMPIRAN. Universitas Sumatera Utara

PHP: Hypertext Preprocessor. A tutorial Introduction

Accessing databases in Java using JDBC

Relational databases and SQL

Locate your Advanced Tools and Applications

Department of Computer Science University of Cyprus. EPL342 Databases. Lab 2

Mount Saint Mary College, Newburgh, NY Internet Programming III - CIT310

This program is a self-contained web server and interface for MediaPortal TV Engine edition.

Chapter 3 Introduction to relational databases and MySQL

SQL. Often times, in order for us to build the most functional website we can, we depend on a database to store information.

More MySQL ELEVEN Walkthrough examples Walkthrough 1: Bulk loading SESSION

Implementing Table Operations Using Structured Query Language (SQL) Using Multiple Operations. SQL: Structured Query Language

SQL language. Jaroslav Porubän, Miroslav Biňas, Milan Nosáľ (c)

SynApp2 Walk through No. 1

SCRIPTING, DATABASES, SYSTEM ARCHITECTURE

Building an E-mini Trading System Using PHP and Advanced MySQL Queries ( ) - Contributed by Codex-M

CICS 515 b Internet Programming Week 2. Mike Feeley

Princess Nourah bint Abdulrahman University. Computer Sciences Department

Use of PHP for DB Connection. Middle and Information Tier. Middle and Information Tier

Using MySQL on the Winthrop Linux Systems

Chapter4: HTML Table and Script page, HTML5 new forms. Asst. Prof. Dr. Supakit Nootyaskool Information Technology, KMITL

IS 2150 / TEL 2810 Introduction to Security

DIGITAL STORYTELLING. Ntombesisa Mateyisi. A Project submitted in partial fulfillment of the requirements for the degree of.

Spring 2014 Interim. HTML forms

Database Systems. phpmyadmin Tutorial

The connection has timed out

How to use SQL to create a database

CSE 127: Computer Security SQL Injection. Vector Li

ゼミ Wiki の再構築について 資料編 加納さおり

Jackson State University Department of Computer Science CSC / Advanced Information Security Spring 2013 Lab Project # 3

Server Side Scripting Report

CMSC 330: Organization of Programming Languages. Markup & Query Languages

CMSC 330: Organization of Programming Languages

CITS1231 Web Technologies. PHP s, Cookies and Session Control

"Charting the Course... Intermediate PHP & MySQL Course Summary

Using PHP with MYSQL

Web accessible Databases PHP

Transcription:

Lab 7 Introduction to MySQL Objectives: During this lab session, you will - Learn how to access the MySQL Server - Get hand-on experience on data manipulation and some PHP-to-MySQL technique that is often required for developing a E-Business website - Exercise on Page 5 and 12 Laboratory Practice: Use MySQL Query Browser to Access the MySQL Server A. You can download the MySQL query browser at the following. MySQL Query Browser allows you to execute SQL commands for manipulating tables and records. MySQL official web site: http://www.mysql.com/ B. You can find the MySQL query in our lab machine as shown in following. MySQL Query Browser is available from nalwin32 in our LAB PC. It is located under "COMP" -> "Network Application Packages" -> "Database" -> "MySQL" -> "MySQLQBrowser" Page: 1

C. Please use/type the following information to login your account: a. Server Host: mysql.comp.polyu.edu.hk b. Port: 3306 c. Username: <Your student ID> d. Password:<MySQL Password> e. Default Schema: <Your student ID> D. Press OK to connect to the database. Working with some basic SQL statements E. Use/Copy the following SQL query and paste it into the tab named Resultset 1 in MySQL Query Browser, and press Execute to create a table. CREATE TABLE students ( id int(5) NOT NULL auto_increment, student_id text NOT NULL, name text NOT NULL, gender char(1) NOT NULL, major text NOT NULL, nationality text NOT NULL, active tinyint(1) NOT NULL default '1', create_date datetime NOT NULL default '0000-00-0000:00:00', last_update_date datetime NOT NULL default '0000-00-00 00:00:00', PRIMARY KEY (id) ) Engine = innodb; i. Next, use/copy the following query and press Execute to add three new records in the table. insert into students (student_id, name, gender, major, nationality) values ('S0001', 'Peter', 'M', 'CS', 'Chinese'); insert into students (student_id, name, gender, major, nationality) values ('S0002', 'Mary', 'F', 'BA', 'Chinese'); insert into students (student_id, name, gender, major, nationality) values ('S0003', 'Paul', 'M', 'EIE', 'Chinese'); Page: 2

ii. To see all the records in the table, you can go back to Resultset 1 tab and type the following query SELECT * FROM students; iii. To view the records only if the students are taking major in BA SELECT * FROM students WHERE major= BA ; iv. To view the whole student list with sorted order in student ID SELECT * FROM students ORDER BY student_id ASC; SELECT * FROM students ORDER BY student_id DESC; v. To update the information for particular record UPDATE students SET gender= F where student_id= S0001 ; SELECT * FROM students; vi. To delete a particular record, type the following query DELETE FROM students WHERE student_id= S0003 ; SELECT * FROM students; PHP to MySQL 1. Now, we have to create a PHP page to access the data stored in COMP MySQL database. The following PHP page retrieve records from students table in the MySQL database server, and list all records in HTML table form. Please rename the file as display_comp.php and perform the testing. <html> <head> <meta http-equiv="content-type" content="text/html; charset=utf-8" /> <title>display Page</title> </head> <body> <h1>display Page</h1> <table width="400" border="0" cellspacing="0" cellpadding="0"> <p> </p> mysql_connect("mysql.comp.polyu.edu.hk","your Login","Your Password"); mysql_select_db("your db (Student ID)"); $result=mysql_query("select * from students"); while($row=mysql_fetch_array($result)) { echo "<tr>"; echo "<td>"; echo $row[1]; echo "</td><td>"; echo $row[2]; echo "</td><td>"; echo $row[3]; echo "</td><td>"; Page: 3

echo $row[4]; echo "</td><td>"; echo $row[5]; echo "</td></tr>"; mysql_free_result($result); </table> </body> </html> 2. Now, we will create a HTML form to add a new student record. create_comp.php Insert_comp.php MySQL <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="content-type" content="text/html; charset=utf-8" /> <title>create Page</title> </head> <body> <h1>create Page</h1> <form id="form1" name="form1" method="get" action="insert_comp.php"> { echo "ID : <input name=stid type=text id=stid></input><br>"; echo "Name :<input name=stname type=text id=stname></input></br>"; echo "Sex : <input name=stsex type=text id=stsex></input></br>"; echo "Major : <input name=stmajor type=text id=stmajor></input></br>"; echo "Nationality : <input name=stnationality type=text id=stnationality></input></br>"; <input type="submit" name="create" id="create" value="create" /> </form> </body> </html> Page: 4

3. Then, we create PHP with insert statement. <title>insert</title> $stid=$_get["stid"]; $stname=$_get["stname"]; $stsex=$_get["stsex"]; $stmajor=$_get["stmajor"]; $stnationality=$_get["stnationality"]; mysql_connect("mysql.comp.polyu.edu.hk","your Student ID","Your Password"); mysql_select_db("your Database (Student ID)"); $sql="insert into students (student_id, name, gender, major, nationality) values ('$stid', '$stname', '$stsex', '$stmajor', '$stnationality')"; mysql_query($sql); Exercise 1. You are required to create a search.html, edit.php and update.php as shown in the following figure. search.html is used to capture the student id. edit.php is used to display the current information. update.php is used to stored the data to the DB. serch.html edit.php update.php MySQL Page: 5

Objective After this lab session, you will - Learn how to access the MySQL Server - Learn the basic syntax of SQL query - Get hand-on experience on PHP session handling - Get hand-on experience on data manipulation and some PHP-to-MySQL techniques Laboratory Use with MySQL in XAMPP I. Run XAMPP II. Go to Admin page in MySQL as shown III. MySQL default setting is shown as follows a. Server Host: 127.0.0.1 b. Port: 3306 c. Username: root d. Password: <no password > e. Default Schema: root Page: 6

IV. To modify the default, please click Privileges Page V. In COMP server, you can create multiple tables but there is only one database (The database name is your student id). VI. You can create multiple databases in your server. To create new database, please click Database Page. Page: 7

1. User Registration with MySQL VII. Create a table named users. If you are using XAMPP, please also create a DB (comp3421) to store the table. The table is used for storing user information. The following is the DDL statement to create the table Create table users ( username varchar(15) primary key, fullname text not null, password varchar(15) not null, email varchar(50) not null); VIII. Create a PHP user registration page named register.php with the following code. The user information will be stored in the MySQL database. session_start(); <html><head><style> h1 {color:red </style></head> <body> $username = $_POST['username']; $fullname = $_POST['fullname']; $password = $_POST['password']; $email = $_POST['email']; function insertrecord($username, $fullname, $password, $email) { $conn = mysql_connect("localhost", "root", ""); mysql_selectdb("comp3421", $conn); $query = "insert into users values ('". $username. "', '". $fullname. "', '". $password. "', '". $email. "')"; mysql_query($query, $conn); if (mysql_error()!= "") { echo "<h1>the user name is already used by another user. ". " Please use another one.</h1>"; echo "<input type=\"button\" ". " onclick=\"javascript: history.go(-1)\" value=\"back\"/>"; else { $_SESSION['conn'] = $conn; Page: 8

$_SESSION['username'] = $username; $_SESSION['fullname'] = $fullname; echo "<h1>registration is completed successfully</h1>"; mysql_close($conn); if (isset($username) && trim($username)!= "") { insertrecord(trim($username), trim($fullname), trim($password), trim($email)); else { <form action=" echo $_SERVER['PHP_SELF']; " method="post"> User name: <input type="text" name="username" /><br/> Full Name: <input type="text" name="fullname" /><br/> Password: <input type="password" name="password" /><br/> Email: <input type="text" name="email" /><br/> <input type="submit" value="sign In" /><br/> </form> </body></html> IX. Update the following lines with your MySQL account information: $conn = mysql_connect("localhost", "root", ""); mysql_selectdb("comp3421", $conn); X. Upload the PHP page to the web server and see the result in the browser 2. Login with MySQL I. Create a PHP user login page named login.php with the following code. The page retrieves user name and password from the MySQL database and does the login checking. session_start(); function printloginform() { <form action=" echo $_SERVER['PHP_SELF']; " method="post"> <h1>system Login</h1> User name: <input type="text" name="username" /><br/> Password: <input type="password" name="password" /><br/> <input type="submit" /> Page: 9

</form> function printwelcomescreen($name) { echo "<h1>welcome, $name</h1>"; echo "<a href=\"edit_user.php\">edit Personal Information</a><br/>"; echo "<a href=\"logout.php\">logout</a>"; if (isset($_session['username'])) { $username = $_SESSION['username']; $fullname = $_SESSION['fullname']; printwelcomescreen($fullname); die(); if (!isset($_post['username'])!isset($_post['password'])) { printloginform(); die(); $username = $_POST['username']; $password = $_POST['password']; $conn = mysql_connect("localhost", "root", ""); mysql_select_db("comp3421", $conn); $query = "select fullname from users ". "where username='$username' and password='$password'"; $resultset = mysql_query($query, $conn); if (mysql_num_rows($resultset) > 0) { $record = mysql_fetch_row($resultset); $_SESSION['username'] = $username; $_SESSION['fullname'] = $record[0]; printwelcomescreen($record[0]); else { printloginform(); mysql_close($conn); II. Update the following lines with your MySQL account information: $conn = mysql_connect("localhost", "root", ""); mysql_selectdb("comp3421", $conn); Page: 10

III. Upload the PHP page to the web server and see the result in the browser 3. Logout Page I. Create a PHP logout page named logout.php with the following code. session_start(); if (isset($_session['username'])) { echo "<h1>good bye, ". $_SESSION['fullname']. "</h1>"; echo "<a href=\"login.php\">login</a>"; unset($_session['username']); unset($_session['fullname']); else { echo "<h1>you have not logged in yet</h1>"; echo "<a href=\"login.php\">login</a>"; II. Upload the PHP page to the web server and see the result in the browser 4. Exercise I. Create a PHP page named edit_user.php. The page is used for updating personal information. You need to complete the page using the following code. session_start(); function printeditform($name) { <form action=" echo $_SERVER['PHP_SELF']; " method="post"> <h1>personal Info Update</h1> <h3> echo $name; </h3> Full name: <input type="text" name="fullname" /><br/> Password: <input type="password" name="password" /><br/> Email: <input type="text" name="email" /><br/> <input type="submit" name="submit" /> </form> Page: 11

if (!isset($_session['username'])) { echo "<h1>you have not logged in yet</h1>"; echo "<a href=\"login.php\">login</a>"; die(); if (isset($_post['submit'])) { $username = $_SESSION['username']; $_SESSION['fullname'] = $fullname = $_POST['fullname']; $password = $_POST['password']; $email = $_POST['email']; else { // Add your code here to complete this PHP page printeditform($_session['fullname']); II. III. Update the following lines with your MySQL account information: $conn = mysql_connect("localhost", "root", ""); mysql_selectdb("comp3421", $conn); Upload the PHP page to the web server and see the result in the browser ~ End ~ Page: 12