XHTML, PHP & MySQL:
Putting it all together |
|
| documentation |
XHTML
tags PHP Documentation
MySQL
Documentation
PHP Documentation
regarding MySQL |
Putting it all
together |
How Web Database Architecture Works:
characters:
cslab103.cs.edinboro.edu - web server
form.htm - web (HTML) form (file containing form)
processForm.php - php file to process submitted data
from form.htm
An end-user's web browser issues an HTTP request to a web server ("cslab103.cs.edinboro.edu")
for a particular web page ("form.htm").
The web server ("cslab103.cs.edinboro.edu") sends the web page ("form.htm")
to the end-user's web browser and the browser interprets the HTML codes to
display the page in the browser window.
The end-user fills in the form fields and submits the form issuing an
HTTP request to a web server ("cslab103.cs.edinboro.edu") to run a
CGI script ("processForm.php").
The web server ("cslab103.cs.edinboro.edu") starts the CGI script
("processForm.php").
The CGI script ("processForm.php") opens a connection with the
MySQL server and sends the MySQL commands.
The MySQL server receives the MySQL commands, processes them, and sends
results back to the CGI script ("processForm.php").
The CGI script ("processForm.php") interprets the results and
creates an HTML document to submit to the web server ("cslab103.cs.edinboro.edu").
The web server ("cslab103.cs.edinboro.edu") sends the CGI
generated web page to the end-user's web browser and the browser interprets
the HTML codes to display the response page in the browser window.

|
accessing a db from
a CGI script |
- Check and filter the data coming from the end-user
- Set up a connection to the appropriate database
- Determine the database command to execute
- Execute the commands
- Retrieve the results
- Prepare retrieve data for XHTML processing
- Present the results back to the end-user as an XHTML web page
|
Check and Filter data
|
Prepare submitted data for the database: trim($variable) - removes all whitespace in front and after value
contained in $variable
if (!$variable1 || !$variable2 ...) - checks whether the variables
have a value
$variable = addslashes($variable) - "escapes" any special
characters for MySQL (ex: " (double quote) becomes \" )
Prepare data retrieved from the database to be put into XHTML:
$variable = stripslashes($variable) - removes slashes used by MySQL for
display in HTML ( \" becomes " )
$variable = htmlspecialcharacters($variable) - "encodes" special
characters for use in HTML ( < becomes < )
|
connecting to mysql
from php |
@ $link = mysql_connect( "<host>", "<username>",
"<password>");
//connects to MySQL
if (!$link)
{
print( " ERROR connecting to
db. Please try again later. <br>");
exit;
}
mysql_select_db(<database
name>); // connects to a db
myql_close( $link);
Example:
@ $link=mysql_connect(
"cslab103.cs.edinboro.edu",
"zimmer",
"mypw");
//connects to MySQL
if (!$link)
{
print( " ERROR connecting to
db.<br>");
exit;
}
mysql_select_db("test"); // connects to a db
myql_close( $link);
|
executing a mysql command
from php |
$query = <valid MySQL query>;
$results = mysql_query($query);
// queries the
open database
or
$results = mysql_db_query($db, $query, $link);
// don't need to select the db from above
Example:
$query ="SELECT * FROM courses";
$results = mysql_query($query);
or
$query = "INSERT INTO courses VALUES
('csci125', 2,'Bennett', 3)";
$results = mysql_query($query);
|
Process a simple
db query |
$results = mysql_query($query);
// queries the
open database
$num_results = mysql_num_rows($results);
for ($i=0; $i<$num_results; $i++)
{
// process the results
$row = mysql_fetch_array($results);
//assigns a row to $row
print ("<br>
$row["<fieldname>"]...<br>");
}
Example:
$query ="SELECT * FROM courses";
$results = mysql_query($query);
$num_results = mysql_num_rows($results);
for ($i=0; $i<$num_results; $i++)
{
$row = mysql_fetch_array($results);
print ("<br>
$row[num] $row[instr]...<br>");
}
|
Process results from
adding to a db table |
$results = mysql_query($query);
// queries the
open database
$num_results = mysql_affected_rows();
if ($results)
{
print("<br>$num_results
record was added.<br>\n");
}
Example:
$query = "INSERT INTO courses VALUES
('csci125', 2,'Bennett', 3)";
$results = mysql_query($query);
$num_results = mysql_affected_rows( );
if ($results)
{
print("<br>$num_results
rows added.<br>\n");
}
|
| |
|
Try This: Example 1
Create a PHP script (faculty_ex1.php) that requests the data from the
faculty table and displays it.
answer -> click here |
|
| |
|
Try This: Example 2
Create an XHTML form (faculty_ex2.htm) that requests
data for the faculty table (have an entry for each field- use the
correct field element). Submit the data to a php file named
faculty_ex2.php. Here is what the table looks like and some sample
data:
|
name |
bdate |
subj |
gender |
ext |
|
Hillman |
1950-01-13 |
CS |
F |
2760 |
|
Tucker |
1965-06-30 |
CS |
M |
2550 |
|
Weening |
1962-03-04 |
MATH |
M |
1187 |
answer -> click here
Write a PHP script (faculty_ex2.php) that
updates the faculty table with the submitted data from the form.
answer -> click here
Once you have added a few new records, use the
faculty_ex1.php to display the entire faculty table
|
|