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