CSE134A LECTURE NOTES

October 2, 2002
 
 

ANNOUNCEMENTS

Today I will give an introduction to PHP forms and communicating with MySQL.  For the practical details on how to use MySQL in the first project, see these section notes on Using PHP with MySQL.
 
 

FORMS IN HTML

Here's an example of a web page including a form in HTML, and PHP code for processing information submitted via the form

       <HTML>
       <FORM>
            Please type your name here:
            <INPUT TYPE=TEXT NAME=username>
            <INPUT TYPE=SUBMIT VALUE="Submit data">
       </FORM>
       <P>
       <?php
              print("You typed $username.\n");
       ?>
       </HTML>

When you load this page the first time, you will see an empty input box, a button containing the label Submit data, and the text You typed . Then if you fill in Joe Smith and click the button, the page will be redisplayed with the text You typed Joe Smith.

When the page containing the form is submitted, each PHP variable with the same name as one of the NAME= fields in the form is initialized with the value placed in the form by the client.

Therefore, the printing after the form is always of what the user entered before the last time s/he clicked the submit button.  You must remember this when designing your PHP code, to avoid making the user confused.

You can make the result of submitting a form be a completely different web page by writing <FORM ACTION="process.php"> for example.
 
 

PROCESSING FORMS

The URL sent to the server when the submission button is clicked is

       http://ieng9.ucsd.edu/~cs134s/test.php?username=Joe+Smith

The browser packages the information typed into the form as name/value pairs in the URL.  To avoid unprintable characters, there is a convention where blanks are changed to + symbols and other characters are also encoded in special ways.  On the server, PHP reverses this encoding automatically before initializing variables with the right names.

Browsers treat URLs that include GET information in the same way as URLs that designate static web pages.  So, the browser may cache the value of these URLs.  This can be incorrect if the server wants to return different information in response to the same form input, for example because time has passed.

If you write <FORM METHOD="POST"> then the browser sends the field values to the server not as part of the URL, but as separate messages in the HTTP protocol.  In this case the field values sent are not visible to the human user, and the web page returned by the server is never cached.
 
 

PHP VERSION 4.2.0

With the newest version of PHP, HTTP 'GET' and 'POST' variables do not automatically get inserted  into the PHP namespace.

See http://www.php.net/manual/en/language.variables.predefined.php

Instead of $username, you need to write $_GET['username']

Alternatively, you can use the PHP function named import_request_variables
 
 

THREE-TIER ARCHITECTURE

Most web sites have the same basic three-tier architecture: In general, the db server and the web server are different computers.  They communicate via a network, and specifically via Unix sockets.

The db client is a PHP script.  The script sends commands written in the SQL language.  The db server sends responses.  A response is often a record containing data.
 
 

CONNECTING TO THE MYSQL SERVER

// MySQL database connection information
$username = "php";
$password = "pw";
$hostname = "ieng8.ucsd.edu";
$dbname = "mydb";

if (!($link=mysql_connect($hostname, $username, $password)))
        printerror("cannot connect to $hostname by $username");
if (!mysql_select_db($dbname, $link))
        printerror("cannot select $dbname database");

Do not use pconnect.  It is supposed to be more efficient but it has some bugs that are only visible when many scripts are making thousands of connections to the database server.
 
 

TABLES AND RECORDS

Tables are divided into rows and columns.  A row is also called a record or tuple.  A column is also called a field or attribute.  A table is also called a relation.

All database operations are specified using a language of commands called SQL.  For example:

drop table if exists temp;
A query allows data to be retrieved based on its content, not its storage location.  For example:

       select author, date from messages where date > '2000-11-11'
 
 



Copyright (c) by Charles Elkan, 2002.