1. Trang chủ
  2. » Công Nghệ Thông Tin

PHP 5/MySQL Programming- P76 docx

5 141 0

Đang tải... (xem toàn văn)

THÔNG TIN TÀI LIỆU

Cấu trúc

  • PHP 5 / MySQL Programming for the Absolute Beginner

    • Cover

  • Contents

  • Introduction

  • Chapter 1: Exploring the PHP Environment

  • Chapter 2: Using Variables and Input

  • Chapter 3: Controlling Your Code with Conditions and Functions

  • Chapter 4: Loops and Arrays

  • Chapter 5: Better Arrays and String Handling

  • Chapter 6: Working with Files

  • Chapter 7: Writing Programs with Objects

  • Chapter 8: XML and Content Management Systems

  • Chapter 9: Using MySQL to Create Databases

  • Chapter 10: Connecting to Databases within PHP

  • Chapter 11: Data Normalization

  • Chapter 12: Building a Three-Tiered Data Application

  • Index

  • Team DDU

Nội dung

value = “$roomNum”> </td> </tr> <tr> <td></td> <td>$northList</td> <td></td> </tr> <tr> <td>$westList</td> <td> <textarea rows = 5 cols = 30 name = “description”>$theText</textarea> </td> <td>$eastList</td> </tr> <tr> <td></td> <td>$southList</td> <td></td> </tr> <tr> <td colspan = 3> <input type = “submit” value = “save this room”> </td> </table> </form> HERE; function makeList($dir, $current){ //make a list of all the places in the system 353 C h a p t e r 1 0 C o n n e c t i n g t o D a t a b a s e s w i t h i n P H P global $conn; $listCode = “<select name = $dir>\n”; $sql = “SELECT id, name FROM adventure”; $result = mysql_query($sql); $rowNum = 0; while ($row = mysql_fetch_assoc($result)){ $id = $row[“id”]; $placeName = $row[“name”]; $listCode .= “ <option value = $id\n”; //select this option if it’s the one indicated if ($rowNum == $current){ $listCode .= “ selected\n”; } // end if $listCode .= “>$placeName</option>\n”; $rowNum++; } // end while return $listCode; } // end makeList ?> </body> </html> Generating Variables After the standard database connection, the code creates a number of variables. Some of these variables ( $theText, $roomName, and $roomNum) are simplifications of the associative array. Another set of variables are the result of the makeList() function. This function’s job is to return an HTML list box containing the room names of every segment in the database. The list box is set up so that whatever room number is associated with the indicated field is the default. Printing the HTML Code The central part of the program consists of a large print statement that develops the HTML code. The code in this case is a large table enclosed in a form. Every field in the record has a form element associated with it. When the user submits this form, it should have all the data necessary to update a record in the database. 354 P H P 5 /M y S Q L P r o g r a m m i n g f o r t h e A b s o l u t e B e g i n n e r The one element the user should not be able to directly edit is the room number. This is stored in a hidden field. The directional room numbers are encoded in the list boxes. All other data is in appropriately named text boxes. Creating the List Boxes The list boxes require a little bit of thought to construct. The makeList() function expects two parameters. The $dir parameter holds the direction field name of the current list box. The $current parameter holds infor- mation about which room is currently selected for this particular field of the cur- rent record. The data connection handler $conn is the only global variable. The variable $listCode holds the actual HTML code of the list box returned to the main program. The function makes a query to the database to request all the room names. Each name is added to the list box code at the appropriate time with the correspond- ing numeric value. Whenever the record number corresponds to the current value of the record, HTML code specifies that this should be the selected item in the list box. Committing Changes to the Database One more program is necessary. The editSegment.php program allows the user to edit the data. When finished he submits the form, which calls the saveRoom.php program. I won’t repeat the screen shot for this program, because the visuals are unimportant. However, this program actually updates the database with what- ever values the user has chosen. <head> <title>SaveRoom.php</title> </head> <body> <? //Once a room has been edited by editSegment, this program //updates the database accordingly. //connect to database $conn = mysql_connect(“localhost”, “”, “”); $select = mysql_select_db(“chapter7”, $conn); 355 C h a p t e r 1 0 C o n n e c t i n g t o D a t a b a s e s w i t h i n P H P $sql = <<<HERE UPDATE adventure SET name = ‘$name’, description = ‘$description’, north = $north, east = $east, south = $south, west = $west WHERE id = $id HERE; //print $sql; $result = mysql_query($sql); if ($result){ print “<h3>$name room updated successfully</h3>\n”; print “<a href = \”listSegments.php\”>view the rooms</a>\n”; } else { print “<h3>There was a problem with the database</h3>\n”; } // end if ?> </body> </html> This program begins with standard data connections. It then constructs an UPDATE SQL statement. The statement is quite simple, because all the work is done in the previous program. I then simply applied the query to the database and checked the result. An UPDATE statement won’t return a recordset like a SELECT statement. Instead, it will return the value FALSE if it was unable to process the command. If the update request was successful, I let the user know and provide a link to the listSegments program. If there was a problem, I provide some (not very helpful) feedback to the user. Summary In this chapter you begin using external programs to manage data. You learn how MySQL can interpret basic SQL statements for defining and manipulating data. 356 P H P 5 /M y S Q L P r o g r a m m i n g f o r t h e A b s o l u t e B e g i n n e r You create a database directly in the MySQL console, and you also learn how to build and manipulate databases with SQLyog. You combine these skills to create an interesting and expandable game. 357 C h a p t e r 1 0 C o n n e c t i n g t o D a t a b a s e s w i t h i n P H P CHALLENGES 1. Add a new room command to the adventure generator. Hint: Think about how I created a new test in the quiz machine program from chapter 6. 2. Write PHP programs to view, add, and edit records in the phone list. 3. Write a program that asks a user’s name and searches the database for that user. 4. Create a front end for another simple database. . more program is necessary. The editSegment .php program allows the user to edit the data. When finished he submits the form, which calls the saveRoom .php program. I won’t repeat the screen shot. updates the database with what- ever values the user has chosen. <head> <title>SaveRoom .php& lt;/title> </head> <body> <? //Once a room has been edited by editSegment,. ($result){ print “<h3>$name room updated successfully</h3> ”; print “<a href = ”listSegments .php ”>view the rooms</a> ”; } else { print “<h3>There was a problem with the database</h3> ”; }

Ngày đăng: 07/07/2014, 02:20

TỪ KHÓA LIÊN QUAN

TÀI LIỆU CÙNG NGƯỜI DÙNG

  • Đang cập nhật ...

TÀI LIỆU LIÊN QUAN