Добавил:
Upload Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
PHP Programming With MySQL Second Edition.doc
Скачиваний:
0
Добавлен:
01.05.2025
Размер:
43.07 Mб
Скачать

Create a Web page that stores airline surveys in a MySQL database.

Include fields for the date and time of the flight, flight number, and

other fields you consider appropriate for identifying a particular

flight. Also, include groups of radio buttons that allow the user to rate

the airline on the following criteria:

• Friendliness of customer staff

• Space for luggage storage

• Comfort of seating

• Cleanliness of aircraft

• Noise level of aircraft


Discovery Projects

The radio buttons for each question should have the following

options: No Opinion, Poor, Fair, Good, and Excellent. Include a View

Past Survey Results button on the main survey page that displays a

list of past survey results.

Discovery Projects

The Chinese Zodiac site is a comprehensive project that will be

updated in the Discovery Projects in each chapter. All files for the

Chinese Zodiac site will be saved in a folder named ChineseZodiac

in the root Web folder on the server, and all database tables will be

stored in the chinese_zodiac database.

493

Discovery Project 8-1

This Discovery Project will build upon Discovery Project 7-2, in

which you created a zodiacfeedback table to store the date, time,

sender, message content, and display status of user feedback about the

Chinese Zodiac Web site.

Now you will create the user interface—a Web form that contains

an appropriate title, subtitle, and instructions. Include form inputs

to enter the sender’s name (a text box for the sender field), message

(a text area for the message field), whether the message should be

publicly displayed (a check box for the public_message field), and a

Submit button to transfer the data to the processing script, process_

zodiac_feedback.php, which you will create in Discovery Project 8-2.

Save the file as zodiac_feedback.html and upload the file to the

ChineseZodiac directory on the server. Open the Web form in the

browser to verify that all the input fields are displayed properly.

When you

were intro-

duced to Web

forms in

Chapter 4,

you learned that the best

way to validate user input

was to provide sample

input and/or restrict the

values that could be

entered in a form (like the

check box used to

restrict the value of the

display_message field to

‘Y’ or ‘N’).

Discovery Project 8-2

In this project, you will create the process_ zodiac_feedback.php

script to process the form information submitted with the zodiac_

feedback.html Web form created in Project 8-1, and to store the

information in the zodiacfeedback table in the chinese_zodiac

database.

Open a blank document in the text editor and insert the following

script, replacing host with the name of the host of the MySQL server,

and user and password with your user name and password.

<?php

$db_name="chinese_zodiac";

//assign the connection and selected database to a variable

$DBConnect = mysql_connect("host", "user", "password ");


CHAPTER 8

Manipulating MySQL Databases with PHP

if ($DBConnect === FALSE)

echo "<p>Unable to connect to the database server.</p>"

. "<p>Error code " . mysql_errno()

. ": " . mysql_error() . "</p>";

else {

//select the database

$db = mysql_select_db($db_name, $DBConnect);

if ($db === FALSE) {

echo "<p>Unable to connect to the database

server.</p>"

. "<p>Error code " . mysql_errno()

. ": " . mysql_error() . "</p>";

mysql_close($DBConnect);

$DBConnect = FALSE;

}

}

?>

494

Because the

inc_connect.

php file con-

tains informa-

tion about the

database server and login

information, it would nor-

mally be stored in a direc-

tory outside of the

Web-accessible file struc-

ture, so that it would not

be directly accessible

from a Web browser. To

simplify this exercise, you

will store the inc_con-

nect.php file in the same

directory as the other

include files.

Save the file as inc_connect.php and upload the file to the Includes

subdirectory of the ChineseZodiac directory on the Web server.

Once all fields have been validated, you will “include” the inc_con-

nect.php file to connect to the server and select the chinese_zodiac

database. Assign the table name to a variable $db_table.

Use the INSERT and VALUES keywords with the mysql_query() func-

tion to insert the values from the form into the appropriate fields in

the zodiacfeedback table. Be sure to store the system date and time

in the message_date and message_time fields. Include a message that

thanks the user for entering a comment and reports that the com-

ment was successfully added.

Save the form as process_zodiac_feedback.php and upload the file

to the ChineseZodiac directory on the server. Test the script by open-

ing zodiac_feedback.html in the browser and completing and submit-

ting the form four times. Be sure to select ‘Y’ and ‘N’ alternately, so

that some messages will be public and others will be private.

Open phpMyAdmin and use the Browse command to verify that four

rows have been successfully written to the zodiacfeedback table.

Discovery Project 8-3

Create a new PHP script in your text editor to select all rows in the

zodiacfeedback table that contain a public_message value of ‘Y’ and

save the resultset to a $QueryResult variable. Remember that you

must include the inc_connect.php file.

Соседние файлы в предмете [НЕСОРТИРОВАННОЕ]