PHP MySQL Training v1
PHP MySQL Training v1
03 MySQL
Tools, creating database and tables, select, insert, update and
delete statements,functions, backup and restore.
04 Dynamic Websites
Functions, sticky forms, CSS, Javascripting.
05 Debugging
Coding and database debugging.
“ Every one should know how to program a computer,
because it teaches you how to think.
“
- Steve Jobs -
Installation
Check and install.
Web Browser
01 Chrome / Mozilla firefox.
Web Server
02 Xampp / Wamp / AppServ
Database Tool
03 HeidiSQL / MySQL Workbench
Development Tool
04 Notepad++ / Dreamweaver
Windows Grep
Logic-based creativity
begins…
Introduction
Comments
Viewable in the source code but not in the browser.
PHP
/* Created by: myself
Created on: 21/01/2017 */
HTML
<!– coding end -->
Variables
Used to temporary store values.
Must start with $.
1st character cannot be a number.
Can be assigned to values using =
<?php <?php
$text = $_GET[‘text’]; $firstname= $_POST[‘firstname’];
echo $firstname;
echo $text; ?>
?> <form method=“post”>
<input type=“text” name=“firstname” value=“type your name
Instructions: here”>
1. Save as variable1.php <input type=“submit” value=“save” name=“submit”>
2. Open a web browser </form>
3. Type in
http://localhost/my_folder/varia Instructions:
ble1.php?text=hello 1. Save as variable2.php
2. Open a web browser
3. Type in http://localhost/my_folder/variable2.php
Strings
Chunk of letters, numbers, spaces etc.
Used to display defined text, i.e. error message.
<?php
$text = ‘The famous Hello World code.’;
echo $text;
?>
Instructions:
1. Save as string.php
2. Open a web browser
3. Type in http://localhost/my_folder/string.php
Numbers
Able to display both integer and decimal number.
<?php
$quantity = 3;
$price = 11.99;
$gst= 0.06;
//calculate total
$total = $quantity * $price;
$total = $total + ($total * $gst);
echo “Total =“. $total;
?>
Instructions:
1. Save as string.php
2. Open a web browser
3. Type in http://localhost/my_folder/string.php
Constants
Specific data unlike variables.
Retain the initial value throughout the script.
Cannot change the value once it has been set.
<?php <?php
define('sitename','School Management include ('config.php');
System'); $quantity = 3;
?> $price = 11.99;
$gst= 0.06;
Instructions:
1. Save as config.php //calculate total
2. Edit number.php $total = $quantity * $price;
3. Open a web browser $total = $total + ($total * $gst);
4. Type in echo “Total =“. $total;
http://localhost/my_folder/number ?>
.php <html>
<title><?php echo sitename;?></title>
</html>
Single and Double Quote
Both can be used.
Let’s see the different to further understand them.
<?php <?php
$var = ‘Hello’; $var = 'Hello';
echo $var; echo $var;
echo “var is equal to $var”; echo 'var is equal to $var';
?> ?>
Instructions: Instructions:
1. Save as doublecodequote.php 1. Save as singlequote.php
2. Edit number.php 2. Edit number.php
3. Open a web browser 3. Open a web browser
4. Type in 4. Type in
http://localhost/my_folder/doublequote.php http://localhost/my_folder/singlequote.php
Programming with PHP
Form
Basic form can be created in HTML format.
<form action="#" id="form" method="post" name="form">
<table width="90%" align="center" class="table" border="0">
<tr><td colspan="2"><b>Registration</b></td></tr>
<tr>
<td width="15%">Name</td>
<td width="85%"><input type="text" name="firstname" size="20"></td>
</tr>
<tr>
<td>Age</td>
<td>
<select name="age">
<option value="">Please select</option>
<option value="15">15</option>
<option value="16">16</option>
<option value="17">17</option>
</select>
</td>
</tr>
<tr>
<td>Gender</td>
<td>
<input type="radio" name="gender" value="f">Female
<input type="radio" name="gender" value="m">Male
</td>
</tr>
<tr>
<td>Remarks</td>
<td><textarea name="remarks" rows="3" cols="50"></textarea></td>
</tr>
<tr>
<td></td>
<td><input type="checkbox" name=“declaration" value="1"> I hereby declare that the information given above are
correct.</td>
</tr>
<tr>
<td></td>
<td><input type="submit" name="submit" value="Submit"></td>
</tr>
</table>
</form>
Instructions:
1. Save as registration_form.php
2. Open a web browser
3. Type in http://localhost/my_folder/registration_form.php
Handling Form
Get the submission value.
Instructions:
1. Edit registration_form.php
2. Edit and save the file.
3. Open a web browser
4. Type in http://localhost/my_folder/registration_form.php
<?php
if($_POST['submit']){
$name = $_POST['name'];
$age = $_POST['age'];
$gender = $_POST['gender'];
$remarks = $_POST['remarks'];
$declaration = $_POST['declaration'];
}
?>
Handling Form
Display the submission value.
<?php
if($_POST['submit']){
$name = $_POST['name'];
$age = $_POST['age'];
$gender = $_POST['gender'];
$remarks = $_POST['remarks'];
$declaration = $_POST['declaration'];
}
?>
Instructions:
1. Edit registration_form.php
2. Edit and save the file.
3. Open a web browser
4. Type in http://localhost/my_folder/registration_form.php
<?php
if($_POST['submit']){
$name = stripslashes($_POST['name']);
$age = $_POST['age'];
$gender = $_POST['gender'];
$remarks = $_POST['remarks'];
$declaration = $_POST['declaration'];
}
?>
<form action="#" id="form" method="post" name="form">
<table width="90%" align="center" class="table" border="0">
<tr><td colspan="2"><b>Registration</b></td></tr>
<tr>
<td width="15%">Name</td>
<td width="85%"><input type="text" name="firstname" size="20" value="<?php echo $name;?>"></td>
</tr>
Conditionals and Operators
Used to alter scripts behavior based on set criteria.
Instructions:
1. Edit number.php
2. Edit and save the file.
3. Open a web browser
4. Type in http://localhost/my_folder/number.php
<?php
$quantity = 3;
$price = 11.99;
$gst= 0.06;
//calculate total
$total = $quantity * $price;
$total = $total + ($total * $gst);
//echo "Total = ". $total;
TUTORIAL 1
Validating Form
Set compulsory fields
Prevent malicious or inappropriate input by restricting input.
Instructions: Instructions:
1. Edit registration_form.php 1. Edit registration_form.php by adding the following input.
2. Edit and save the file.
3. In each input field, add in ‘required’. <input type="number" name=“siblings" min="1" max=“10“>
4. Open a web browser
5. Type in http://registration_form.php <input type="date" name="birthday">
2. PhpMyAdmin
Login with username & password.
Default:
Username: root
Password:
Create Table
folder path> : mysql –uusername –ppassword
mysql> : show databases;
mysql> : use sms_db;
TUTORIAL 2
Insert Statement
Insert at least 3 records.
mysql> : INSERT INTO registration (
firstname,
age,
gender,
remarks,
email,
birthday,
siblings,
submission_date)
VALUES (
‘Amira Ahmad',
‘15',
‘f',
‘None.',
‘[email protected]',
‘2001-01-30',
‘2',
'".CURDATE()."');
Update Statement
Update 1 field:
mysql> : UPDATE registration SET
firstname = ‘Amira Binti Ahmad’
WHERE
id=1;
Restore:
Folder path> : mysql –uusername –ppassword databasename < backupfile.sql
Example:
Folder path> : mysql –uroot –ppassword sms_db2 < sms_db_20171001.sql
“ Create database and tables for our mini project which
includes login, registration and able to generate reports
based on age and gender. “
TUTORIAL 3
Thank you