Вы находитесь на странице: 1из 31

PHP & MySQL

For Dynamic Websites

PREPARED BY: MAIZUN MOHD NOOR


Training Outline
Introduction
01
Comments, variables, strings, numbers, contants, single and
double quotation marks.

02 Programming with PHP


Create a form, magic quotes, conditional and operators,
validating data.

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 */

// enhancement: need to customize date output

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'];

}
?>

<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>
<tr>
<td>Age</td>
<td>
<select name="age">
<option value="">Please select</option>
<option value="15" <?php if($age=='15'){ echo 'selected';}?>>15</option>
<option value="16" <?php if($age=='16'){ echo 'selected';}?>>16</option>
<option value="17" <?php if($age=='17'){ echo 'selected';}?>>17</option>
</select>
</td>
</tr>
<tr>
<td>Gender</td>
<td>
<input type="radio" name="gender" value="f" <?php if($gender=='f'){ echo 'checked';}?>>Female
<input type="radio" name="gender" value="m" <?php if($gender=='m'){ echo 'checked';}?>>Male
</td>
</tr>
<tr>
<td>Remarks</td>
<td><textarea name="remarks" rows="3" cols="50"><?php echo $remarks;?></textarea></td>
</tr>
<tr>
<td></td>
<td><input type="checkbox" name="declaration" value="1" <?php if($declaration=='1'){ echo 'checked';}?>> 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>
Magic Quotes
Used to escape single and double quotation marks in the values of retrieved variables.

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;

if($total < 20){


$new_total = $total - ($total * 0.20);
}elseif($total >= 21){
$new_total = $total - ($total * 0.30);
}

echo "Discounted Price = ".number_format($new_total,2);


?>
“ By using conditionals and operators, customize registration
form to display
“Good day, Sir.” if the applicant is a male,
and display “Good day, Madam.” if the applicant is a
female.

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">

<input type="email" name="email">

Try these too:


<input type="range" name="points" min="0" max="10">
MySQL
Connecting to Tool
1. heidiSQL / MySQL Workbench
Add connection by setting the host, username and password.
Test connection.

2. PhpMyAdmin
Login with username & password.
Default:
Username: root
Password:

3. Checking database connection via command prompt


Go to web server path.
Cmd:
mysql –uusername –ppassword
Create Database and Tables
Create Database
folder path> : mysql –uusername –ppassword
mysql> : show databases;
mysql> : create database sms_db;

Create Table
folder path> : mysql –uusername –ppassword
mysql> : show databases;
mysql> : use sms_db;

mysql> : CREATE TABLE sample_table (id INT(10) NOT NULL AUTO_INCREMENT,


username VARCHAR(100) NOT NULL,
submission_date DATE, PRIMARY KEY (id) );
“ Create table to store registration data by using any
database tools.

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.',
‘amira@gmail.com',
‘2001-01-30',
‘2',
'".CURDATE()."');
Update Statement
Update 1 field:
mysql> : UPDATE registration SET
firstname = ‘Amira Binti Ahmad’
WHERE
id=1;

Update multiple fields:


mysql> : UPDATE registration SET
firstname = ‘Amira Binti Ahmad’,
age = ‘17’
WHERE
id=1;
Delete Statement
Delete 1 record:
mysql> : DELETE FROM registration
WHERE
id=1;

Delete all records: (


mysql> : DELETE FROM test;
Select Statement
Select specific field:
mysql> : SELECT firstname, age FROM registration;

Select specific field and record:


mysql> : SELECT firstname, age FROM registration
WHERE
id=1;

Use LIKE to select uncertain record:


mysql> : SELECT * FROM registration
WHERE
name LIKE ‘%a’;
Backup and Restore a Database
Backup:
Folder path> : mysqldump –uusername –ppassword databasename > backupfile.sql
Example:
Folder path> : mysqldump –uroot –ppassword sms_db > sms_db_20171001.sql

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

Вам также может понравиться