HTML, PHP, and MySQL : Putting It All Together
description
Transcript of HTML, PHP, and MySQL : Putting It All Together
![Page 1: HTML, PHP, and MySQL : Putting It All Together](https://reader034.fdocuments.net/reader034/viewer/2022052510/568146cd550346895db405ef/html5/thumbnails/1.jpg)
HTML, PHP, and MySQL:Putting It All Together
![Page 2: HTML, PHP, and MySQL : Putting It All Together](https://reader034.fdocuments.net/reader034/viewer/2022052510/568146cd550346895db405ef/html5/thumbnails/2.jpg)
Making a Form
Input tags
<input type=“text” />
Types:
“text”
“radio”
“checkboxes”
“submit”
![Page 3: HTML, PHP, and MySQL : Putting It All Together](https://reader034.fdocuments.net/reader034/viewer/2022052510/568146cd550346895db405ef/html5/thumbnails/3.jpg)
Making a Form
Put multiple “inputs” inside <form> </form> tags.
Make the last one a “submit” button.
![Page 4: HTML, PHP, and MySQL : Putting It All Together](https://reader034.fdocuments.net/reader034/viewer/2022052510/568146cd550346895db405ef/html5/thumbnails/4.jpg)
<form><dl><dt>Text: </dt><dl><input type="text" /></dl><dt>Buttons: </dt><dd><input type="radio" value="Option 1" />Option 1<input type="radio" value="Option 2" />Option 2</dd><dt>Checkboxes: </dt><dd><input type="checkbox" value="Option 1" />Option 1<input type="checkbox" value="Option 2" />Option 2</dd><dt>Submit button: </dt><dd><input type="submit" value="submit" /></dd></dl></form>
![Page 5: HTML, PHP, and MySQL : Putting It All Together](https://reader034.fdocuments.net/reader034/viewer/2022052510/568146cd550346895db405ef/html5/thumbnails/5.jpg)
Making the Form Useful
Add attributes to the form tag:
<form action=“sendToDatabase.php” method=“post”>
...
</form>
Add attributes to the input tags:
<input type=“text” name=“email” />
We will need to make a PHP file.
![Page 6: HTML, PHP, and MySQL : Putting It All Together](https://reader034.fdocuments.net/reader034/viewer/2022052510/568146cd550346895db405ef/html5/thumbnails/6.jpg)
Request a.php
Send back a.html
a.php
<?Echo “Hello”;?>
PHP Interpreter
Rendered PHPIn HTML format
How a WAMP server gets PHP code to a client browser(Windows Apache MySQL PHP Web Server)
Client Browser
Rendered PHPIn HTML format
<html><body>Hello</body></html>
My SQLDatabase
WAMP Server
![Page 7: HTML, PHP, and MySQL : Putting It All Together](https://reader034.fdocuments.net/reader034/viewer/2022052510/568146cd550346895db405ef/html5/thumbnails/7.jpg)
What our PHP file needs to do
• Connect to our database• Get that data from the form• “Escape” the data so it’s safe• Send the data to the database
![Page 8: HTML, PHP, and MySQL : Putting It All Together](https://reader034.fdocuments.net/reader034/viewer/2022052510/568146cd550346895db405ef/html5/thumbnails/8.jpg)
Beginning the PHP file
<?php
mysql_connect(“localhost”, “root”, “psswd”);
Server Address Username Password
![Page 9: HTML, PHP, and MySQL : Putting It All Together](https://reader034.fdocuments.net/reader034/viewer/2022052510/568146cd550346895db405ef/html5/thumbnails/9.jpg)
Getting the data from the form
Recall the attributes we added to the form and the inputs:<form action=“sendToDatabase.php” method=“post”><input type=“text” name=“email” />
$person = $_POST[`person`];$email = $_POST[`email`];
![Page 10: HTML, PHP, and MySQL : Putting It All Together](https://reader034.fdocuments.net/reader034/viewer/2022052510/568146cd550346895db405ef/html5/thumbnails/10.jpg)
Sending data to the database
mysql_query(“INSERT INTO `mydatabase`.`table1` (`ID`, `Name`, `Email`) VALUES (NULL, $name, $email)”);
Queries can perform many different functions, including adding to the database, reading from the database, or ...
![Page 11: HTML, PHP, and MySQL : Putting It All Together](https://reader034.fdocuments.net/reader034/viewer/2022052510/568146cd550346895db405ef/html5/thumbnails/11.jpg)
Close the Connectionmysql_close();
That’s all there is to it!
After inserting the data into the database, put in the code for the page the user should see after sending information to the database, or use the header function to navigate to a different page.
header(“Location: index.html”);
![Page 12: HTML, PHP, and MySQL : Putting It All Together](https://reader034.fdocuments.net/reader034/viewer/2022052510/568146cd550346895db405ef/html5/thumbnails/12.jpg)
A Complete PHP File<?php
mysql_connect(“localhost”, “root”, “psswd”);
$person = $_POST[`person`];$email = $_POST[`email`];
mysql_query(“INSERT INTO `mydatabase`.`table1` (`ID`, `Name`, `Email`) VALUES (NULL, ‘$name’, ‘$email’)”);
mysql_close();
header(“Location: index.html”);
?>
![Page 13: HTML, PHP, and MySQL : Putting It All Together](https://reader034.fdocuments.net/reader034/viewer/2022052510/568146cd550346895db405ef/html5/thumbnails/13.jpg)
PHP ArraysA PHP array can store pairs of pieces of information (usually numbers or strings). In each pair, one piece of information is called the “key,” and the other is called the “value.”
You can create a PHP array like this:
$myarray = array( ‘breakfast’ => ‘eggs’, ‘lunch’ => ‘sandwich’, ‘dinner’ => ‘steak’);
To retrieve one of the values, use square brackets and the correct key:
$food = $myarray[‘breakfast’]”;
echo “$food”;
![Page 14: HTML, PHP, and MySQL : Putting It All Together](https://reader034.fdocuments.net/reader034/viewer/2022052510/568146cd550346895db405ef/html5/thumbnails/14.jpg)
PHP ArraysWe used arrays earlier when using the POST method.
$person = $_POST[`person`];$email = $_POST[`email`];
PHP automatically created an array called $_POST with the information provided by the user. The ‘name’ attributes from the input tags became the keys of this array.
![Page 15: HTML, PHP, and MySQL : Putting It All Together](https://reader034.fdocuments.net/reader034/viewer/2022052510/568146cd550346895db405ef/html5/thumbnails/15.jpg)
PHP ArraysTo insert information into an array after it’s been created:
$myarray[‘snack’] = ‘chips’;
If you leave the key field blank, the keys will be integers, auto-incrementing from 0.
$myarray[] = ‘Dwayne Wade’;$myarray[] = ‘Lebron James’;$myarray[] = ‘Chris Bosh’;
$myarray[0]$myarray[1]$myarray[2]
![Page 16: HTML, PHP, and MySQL : Putting It All Together](https://reader034.fdocuments.net/reader034/viewer/2022052510/568146cd550346895db405ef/html5/thumbnails/16.jpg)
PHP ArraysYou can also omit keys when creating the array…
$myarray = array(‘knicks’, ‘heat’, ‘bulls’, ‘cavs’);
$myarray[2]
or start with an empty array…
$myarray = array()
![Page 17: HTML, PHP, and MySQL : Putting It All Together](https://reader034.fdocuments.net/reader034/viewer/2022052510/568146cd550346895db405ef/html5/thumbnails/17.jpg)
PHP For LoopsFor loops let you run the same lines of code over and over again.
Example:
<?php
$num = 2;
for($i = 0; $i < 10; $i ++){
$num = $num + 3;
}
echo ‘$num’;
?>
![Page 18: HTML, PHP, and MySQL : Putting It All Together](https://reader034.fdocuments.net/reader034/viewer/2022052510/568146cd550346895db405ef/html5/thumbnails/18.jpg)
PHP For LoopsThere are three parts to the definition of a for loop in PHP:
1)Create a variable and give it a starting value.- Usually the variable will start at 0
2)Set a condition for the variable.- i.e. $i < 10, or $i <= 10- The for loop will not run unless the condition is true.
3)Specify how to change the variable after each iteration.- $i++ is shorthand for making $i go up by 1.- It’s the same thing as $i = $i + 1
for($i = 0; $i < 10; $i ++)
![Page 19: HTML, PHP, and MySQL : Putting It All Together](https://reader034.fdocuments.net/reader034/viewer/2022052510/568146cd550346895db405ef/html5/thumbnails/19.jpg)
Getting InformationFrom the Database
Use the mysql_query() function again.
$rs = mysql_query(“SELECT * FROM `mydatabase`.`mytable`”);
Now $rs is a MySQL resource. It contains all the information we quested in our select query, but in order to use it, we need to convert it into PHP arrays.
The mysql_fetch_array() function converts the first row of the resource into an array.
$row1 = mysql_fetch_array($rs);
The next time mysql_fetch_array() is called on $rs, it will return the second row.
$row2 = mysql_fetch_array($rs);
![Page 20: HTML, PHP, and MySQL : Putting It All Together](https://reader034.fdocuments.net/reader034/viewer/2022052510/568146cd550346895db405ef/html5/thumbnails/20.jpg)
mysql_fetch_arrayThe keys of the array created by myql_fetch_arrray() can be either the column titles or the column indices (zero-indexed).
ID Name
1 Lebron James
2 Dwayne Wade
$row1 = mysql_fetch_array($rs);$row2 = mysql_fetch_array($rs);
$row1[“Name”]$row1[1]
$row2[“Name”]$row2[1]
“Lebron James”
“Dwayne Wade”
![Page 21: HTML, PHP, and MySQL : Putting It All Together](https://reader034.fdocuments.net/reader034/viewer/2022052510/568146cd550346895db405ef/html5/thumbnails/21.jpg)
Use a Loop…
for($k = 0; $k < 2; $k++){
$row = mysql_fetch_array($rs);
$name = $row[“Name”];
echo ‘$name’;
}
ID Name
1 Lebron James
2 Dwayne Wade
![Page 22: HTML, PHP, and MySQL : Putting It All Together](https://reader034.fdocuments.net/reader034/viewer/2022052510/568146cd550346895db405ef/html5/thumbnails/22.jpg)
mysql_num_rowsThe mysql_num_rows() function tells you how many rows are in your MySQL resource.
$num = mysql_num_rows($rs);
This can be especially useful when used to create a for loop:
for($i = 0; $i < $num; $i++){
$row = mysql_fetch_array($rs);
$name = $row[“Name”];
echo ‘$name’;
}
![Page 23: HTML, PHP, and MySQL : Putting It All Together](https://reader034.fdocuments.net/reader034/viewer/2022052510/568146cd550346895db405ef/html5/thumbnails/23.jpg)
A Complete PHP File<?phpmysql_connect(“localhost”,”root”,”password”);
$resource = mysql_query(“SELECT * FROM `mydatabase`.`mytable`”);
$n = mysql_num_rows($resource);
for($i = 0; $i < $n; $i++){
$row = mysql_fetch_array($rs);$name = $row[“Name”]
echo ‘$name’;}
mysql_close();
?>
![Page 24: HTML, PHP, and MySQL : Putting It All Together](https://reader034.fdocuments.net/reader034/viewer/2022052510/568146cd550346895db405ef/html5/thumbnails/24.jpg)
A Couple More Things to KnowHow to escape your inputs:
$email = mysql_real_escape_string($_POST[`email`]);
The difference between POST and GET:
GET encodes information in the URL. POST encodes information where it can’t be seen by the user. Don’t use GET if it would be a problem if the user submitted the information twice!
![Page 25: HTML, PHP, and MySQL : Putting It All Together](https://reader034.fdocuments.net/reader034/viewer/2022052510/568146cd550346895db405ef/html5/thumbnails/25.jpg)
Useful Links
PHP Arrays:http://php.net/manual/en/language.types.array.php
PHP For Loops:http://www.tizag.com/phpT/forloop.php
List of PHP MySQL-related functions:http://www.php.net/manual/en/ref.mysql.php