Difference between revisions of "PHP112"
From mi-linux
Jump to navigationJump to searchLine 10: | Line 10: | ||
# output the results | # output the results | ||
− | == 1. A form to gather criteria ( | + | == 1. A form to gather criteria (pets-form.html) == |
− | + | <pre> | |
+ | <html> | ||
<head> | <head> | ||
<title>Part 1 - Form</title> | <title>Part 1 - Form</title> | ||
</head> | </head> | ||
<body> | <body> | ||
− | <form action=" | + | <form action="pets-search.php" method="post"> |
<p> | <p> | ||
Enter pet's name: <INPUT type="text" name="searchName"> | Enter pet's name: <INPUT type="text" name="searchName"> | ||
Line 26: | Line 27: | ||
</form> | </form> | ||
</body> | </body> | ||
− | + | </html> | |
+ | </pre> | ||
− | == 2. The PHP script ( | + | == 2. The PHP script (pets-search.php) == |
− | + | <pre> | |
− | + | <?php | |
− | + | ||
− | + | // Connect to server/database | |
− | + | $mysqli = mysqli_connect("localhost", "bdtuser", "bdtuser", "bdtheque"); | |
− | + | if (mysqli_connect_errno($mysqli)) { | |
− | + | echo "Failed to connect to MySQL: " . mysqli_connect_error(); | |
− | + | } | |
− | + | ||
− | + | // Build custom SQL query | |
− | + | $sql = "SELECT name, species, age FROM pet"; | |
− | + | ||
− | + | // Add search criteria, if provided | |
− | + | if($_POST['searchName'] != "") | |
− | + | $sql.= " WHERE name LIKE '%" . $_POST['searchName'] . "%'"; | |
− | + | ||
− | + | // Run SQL query | |
− | + | $res = mysqli_query($mysqli, $sql); | |
− | + | ||
− | + | // How many rows were returned? | |
− | + | $num_pets = mysqli_num_rows($res); | |
− | + | ||
− | + | if($num_pets == 0) | |
− | + | print("<p>No pet with that name, sorry...</p>"); | |
− | + | else { | |
− | + | print("<p>We found $num_pets pet(s) matching thay name...</p>"); | |
− | + | ||
− | + | // Loop through resultset and display each field's value | |
− | + | while($row = mysqli_fetch_assoc($res)) { | |
− | + | echo $row['name']. " - ". $row['species'] ."<br>"; | |
− | + | } | |
+ | } | ||
+ | |||
+ | ?> | ||
+ | </pre> | ||
= Deconstruction of the example = | = Deconstruction of the example = | ||
− | == | + | A lot of the code above is similar to the previous section, so let's focus on what's new: |
+ | |||
+ | == pets-form.html == | ||
− | + | This line in very important: | |
− | + | <pre> | |
− | + | <form action="pets-search.php" method="post"> | |
− | + | </pre> | |
− | + | It specifies that anything entered in the current form should be '''posted''' to a file called '''pets-search.php'''. Obviously the latter should match the name of YOUR file. | |
+ | |||
+ | On this line of code: | ||
+ | |||
+ | <pre> | ||
+ | Enter pet's name: <INPUT type="text" name="searchName"> | ||
+ | </pre> | ||
− | + | It is important to note the name of the text field ("searchName"), as we will need it in the script below. | |
− | |||
− | |||
− | + | == pets-search.php == | |
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
= Ready to move on? = | = Ready to move on? = | ||
Let's tackle this week's [[PHPEX05|mini-task]]! | Let's tackle this week's [[PHPEX05|mini-task]]! |
Revision as of 15:15, 29 June 2016
Main Page >> Web Application Development >> Workbook >> Simple HTML/PHP/MySQL example
A Simple HTML/PHP/MySQL Example
The following example will:
- use a form to gather criteria for a search
- pass the criteria to a PHP script
- build a custom query
- execute the query against a database, returning results
- output the results
1. A form to gather criteria (pets-form.html)
<html> <head> <title>Part 1 - Form</title> </head> <body> <form action="pets-search.php" method="post"> <p> Enter pet's name: <INPUT type="text" name="searchName"> </p> <p> <INPUT type="submit" value="Search"> </p> </form> </body> </html>
2. The PHP script (pets-search.php)
<?php // Connect to server/database $mysqli = mysqli_connect("localhost", "bdtuser", "bdtuser", "bdtheque"); if (mysqli_connect_errno($mysqli)) { echo "Failed to connect to MySQL: " . mysqli_connect_error(); } // Build custom SQL query $sql = "SELECT name, species, age FROM pet"; // Add search criteria, if provided if($_POST['searchName'] != "") $sql.= " WHERE name LIKE '%" . $_POST['searchName'] . "%'"; // Run SQL query $res = mysqli_query($mysqli, $sql); // How many rows were returned? $num_pets = mysqli_num_rows($res); if($num_pets == 0) print("<p>No pet with that name, sorry...</p>"); else { print("<p>We found $num_pets pet(s) matching thay name...</p>"); // Loop through resultset and display each field's value while($row = mysqli_fetch_assoc($res)) { echo $row['name']. " - ". $row['species'] ."<br>"; } } ?>
Deconstruction of the example
A lot of the code above is similar to the previous section, so let's focus on what's new:
pets-form.html
This line in very important:
<form action="pets-search.php" method="post">
It specifies that anything entered in the current form should be posted to a file called pets-search.php. Obviously the latter should match the name of YOUR file.
On this line of code:
Enter pet's name: <INPUT type="text" name="searchName">
It is important to note the name of the text field ("searchName"), as we will need it in the script below.
pets-search.php
Ready to move on?
Let's tackle this week's mini-task!