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

Tài liệu PHP and MySQL by Example- P16 docx

50 598 0

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

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

THÔNG TIN TÀI LIỆU

Thông tin cơ bản

Định dạng
Số trang 50
Dung lượng 1,38 MB

Nội dung

: !LCHD(HOCDEGC@AP(CMH(DHGFD>B(Q@CM(CMH(mysql_fetch_assoc()(LSAGC@FAU(E(CETJH(@B( >@BIJEKH>(Q@CM(J@AVB(CF(HEGM(EDC@BCU(M@B(FD(MHD(IMFAH(AS?THD(EA>(H3?E@J(E>>DHBBU( EA>(E(%HJHCH(MKIHDJ@AV)(&L(CMH(SBHD(GJ@GVB(CMH(MKIHDJ@AV(Q@CM(CMH(EDC@BC^B(AE?HU(CMH( EDC@BC^B(id(Q@JJ(TH(IEBBH>(CF(CMH(admin_artist_edit.php(BGD@IC(N@E(E(+.,)(_/MH( NEJSH(FL(CMH(id(@B(EIIHA>H>(CF(CMH(?(ELCHD(CMH(J@AV)` 5 /MH(%HJHCH(J@AVU(QMHA(GJ@GVH>U(IEBBHB(CMH(action(NEJSH(EA>(CMH(id(FL(CMH(EDC@BC(CF( CMH("2"(BGD@IC(_J@AH([`) Figure A.1. The admin_artlist.php page. ( ( Updating an Artist in the Gallery (admin_artist_edit.php) To get to the admin_artist_edit.php page, the user clicked the hyperlink of an artist listed in the previous Figure A.1. Line 4 of Example A.1 shows that the hyperlink is sending the id for that artist via a URL. The admin_artist_edit.php page (Example A.2) uses the id to determine which artist to update. (You can see the value of the id in the URL because the GET method is being used). The id for the artist is stored in a hidden field (see Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark. line 6 in Example A.2). If this is the first time the page is displayed, a SQL SELECT statement (see line 5) is executed to retrieve all the data for that artist from the database. The user is presented with a self-processing HTML form containing the artist information, such as the name, e-mail address, phone, and so on, for the particular artist he or she selected for update. Once the user has updated the information in the form, it is passed into the PHP portion of the page and extracted from the $_REQUEST superglobal array (see line 1), The variables resulting from the extract() function are used to “SET” the new values for the artist in the SQL UPDATE command on line 3. (Be sure when you test this script that you update line 2 and provide the correct server, username, and password to connect to your version of MySQL.) Example A.2. 6F>H(Y@HQZ( ((( <?php 1 extract($_REQUEST); 2 mysql_connect("localhost", "root", "password") or die(mysql_error()); mysql_select_db("test") or die(mysql_error()); if( isset($submit) ) { // If the form has been submitted $status = ""; if( $name=="" ) { // If the user left the name field blank $status = "Please enter the artist's name.<br>"; } else { // Connect to the database and insert the new artist 3 $sql = "UPDATE Artist SET name='$name', " . "email='$email', phone='$phone', bio='$bio'" . "WHERE id='$id'"; mysql_query( $sql ) or die(mysql_error()); 4 $status = "SUCCESSFULLY updated $name"; } } else { 5 $sql = "SELECT * from Artist WHERE id='$id'"; $resultset = mysql_query( $sql )or die(mysql_error()); $row = mysql_fetch_assoc( $resultset ); extract( $row ); // Retrieve/extract all info on artist by ID } ?> <? include("admin_header.php") ?> <h1>Artist Update Screen</h1> <form action="<?=$_SERVER['PHP_SELF']?>" method="GET"> 6 <input type="hidden" name="id" value="<?=$id?>"> <table> <? if (isset($status)) {?> <tr><td colspan="2"><b><?=$status?></b><br><br></td></tr> <? } ?> <tr><td>Name</td><td><input type="text" name="name" value="<?=$name?>" /></td></tr> <tr><td>Email</td><td><input type="text" name="email" value="<?=$email?>" /></td></tr> <tr><td>Phone</td><td><input type="text" name="phone" value="<?=$phone?>" /></td></tr> <tr><td>Bio</td><td><textarea rows="15" cols="60" name="bio" value="<?=$bio?>" ><?=$bio?> </textarea></td></tr> Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark. <tr><td>&nbsp;</td><td><input type="submit" name="submit" value="Submit"/></td></tr> </table> </form> <? include("admin_footer.php") ?> Explanation [ !LCHD(CMH(J@AV(Q@CM(CMH(EDC@BC^B(AE?H(_@A(R@PSDH(!)[`(@B(GJ@GVH>U("2"(Q@JJ(HOCDEGC( CMH(SBHD(@AISC) 1 /MH(4K9X,(GFAAHGC@FA(@B(?E>H(CF(CMH(JFGEJMFBC(EA>(CMH(\CHBC](>ECETEBH(@B( FIHAH>) 8 &L(CMH(LFD?(BMFQA(@A(R@PSDH(!)1(MEB(THHA(BST?@CCH>U(CM@B(9X,(BCECH?HAC(Q@JJ( SI>ECH(CMH(\!DC@BC](CETJH(LFD(EA(EDC@BC(BHJHGCH>(TK(M@B(FD(MHD(id(IEBBH>(@A(CMH(+.,( _#OE?IJH(!)[U(J@AH(:`) : &L(CMH(SI>ECH(QEB(BSGGHBBLSJU(CMH($status(NED@ETJH(@B(EBB@PAH>(CF(CMH(BCD@AP( "SUCCESSFULLY updated $name") 5 &L(CMH(LFD?(MEBA^C(THHA(BST?@CCH>U(EJJ(DFQB(EDH(DHCD@HNH>(LDF?(CMH(4K9X,(\!DC@BC]( CETJH(TEBH>(FA(CMH(id(FL(CMH(EDC@BC(BHJHGCH>(TK(CMH(SBHD(@A(R@PSDH(!)[)(/MH( extract()(LSAGC@FA(Q@JJ(GDHECH(AE?H>(NED@ETJHB(LFD(HEGM(GFJS?A(@A(CMH(DFQ(LFD( CMH(BHJHGCH>(EDC@BCU(BSGM(EB($name, $emailU(EA>(BF(FAU(SBH>(EB(NEJSHB(LFD(CMH( LFD?^B(@AISC(>HN@GHB)(/MH(M@>>HA(L@HJ>(EBB@PAB(CMH(NEJSH(FL(CMH(EDC@BC^B(id(_IEBBH>( @A(CMH(+.,`(CF(@CB(name(ECCD@TSCHU(EJBF(GEJJH>(id) Figure A.2. The admin_artlist_edit.php page updates the information on a particular artist. ( Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark. Adding a New Artist to the Gallery (admin_artist_insert.php) The page for adding a new artist to the database, admin_artist_insert.php, is basically the same as the admin_artist_edit.php page except that it performs a SQL INSERT statement (see line 1 in Example A.3) instead of an UPDATE and does not require an ID to be passed into the page via a URL. The user will enter the name of the new artist and all the requested information in a simple HTML form presented as a table (see line 5). An ID, primary key, will automatically be generated by the database for each new artist that is added. (When testing this page, make sure to enter the correct information when connecting to the database; that is, host, username, and password are supplied for the mysql_connect() function on line 2.) Example A.3. 6F>H(Y@HQZ(( (( <?php extract($_REQUEST); if( isset($submit) ) { $status = ""; if( $name=="" ) { $status = "Please enter the artist's name.<br>"; } else { // Connect to the database and insert the new artist 1 $sql = "INSERT INTO Artist (name, email, phone, bio)" . "VALUES ('$name', '$email', '$phone', '$bio')"; 2 mysql_connect("localhost", "root", "password") or die(mysql_error()); mysql_select_db("test") or die(mysql_error()); 3 mysql_query( $sql ) or die(mysql_error()); $status = "SUCCESSFULLY inserted $name"; } } ?> <? include("admin_header.php") ?> <h1>Artist Insert Screen</h1> 4 <form action="<?=$_SERVER['PHP_SELF']?>" method="GET"> 5 <table> <? if (isset($status)) {?> <tr><td colspan="2"><b><?=$status?></b><br><br></td></tr> <? } ?> <tr><td>Name</td><td><input type="text" name="name"/></td></tr> <tr><td>Email</td><td><input type="text" name="email"/></td></tr> <tr><td>Phone</td><td><input type="text" name="phone"/></td></tr> <tr><td>Bio</td><td><textarea rows="15" cols="60" name="bio"></textarea></td></tr> <tr><td colspan="2"><input type="submit" name="submit" value="Submit"/></td></tr> </table> </form> <? include("admin_footer.php") ?>(( ((( Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark. Explanation [ /M@B(9X,(BCECH?HAC(@B(SBH>(CF(@ABHDC(E(AHQ(EDC@BC(@ACF(CMH(\!DC@BC](CETJH) 1 4EVH(BSDH(CMH(MFBCU(SBHDAE?HU(EA>(IEBBQFD>(EDH(BSIIJ@H>(LFD(CMH(mysql_connect()( LSAGC@FA) 8 /MH(mysql_query()(LSAGC@FA(BHA>B(CMH(9X,(BCECH?HAC(CF(CMH(>ECETEBHU(EA>(@L(@C(LE@JBU( >@BIJEKB(CMH(4K9X,(HDDFD(EA>(>@HB)(;CMHDQ@BHU(CMH(AHQ( DHGFD>( MEB(THHA(@ABHDCH>) : R@PSDH(!)8(>@BIJEKB(CMH(admin_artist_insert.php(LFD?) ( Figure A.3. The admin_artist_insert.php page that will allow a new artist to be added to the Gallery. Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark. Adding and/or Editing a Piece of Art (admin_art_edit.php) The page for adding a new art piece is slightly different from the pages previously shown where one page added a new artist and another page updated an existing artist. The process of inserting and updating a new piece of art is handled on a single page. A drop-down menu will present the user with a list of artists from which to choose. The art is linked to the artist who owns it by the artist’s ID, id in the “Artist” table and a foreign key in the “Art” table, artist_id, a one-to- many relationship in which one artist can have multiple pieces of art. The drop-down menu displays a list of artists by name and the value of the option is the artist’s ID (see Example A.4). Example A.4. (Section of the page source to view the drop-down menu after an artist, Laura Blair, was checked) <table> <tr><td colspan="2"><b>Please enter the art title.<br></b><br><br></td></tr> <tr><td>Title</td><td><input type="text" name="title" value="" /></td></tr> <tr><td>Artist</td> <td><select name="artist_id"> <option value="">Please Select</option> <option value="10" >Todd Brown</option> <option value="11" >stuART Sheldon</option> <option value="12" >Elliott Easterling</option> <option value="13" selected="SELECTED" >Laura Blair</option> </select></td> </tr> After selecting an artist from the drop-down list (line 5 in Example A.5), the page simply checks whether the art ID has been set. If it has, the matching art record is selected for update (line 1). Otherwise, the script inserts a new record (line 2) into the database and the database determines the new ID. Example A.5. 6F>H(Y@HQZ( (The admin_art_edit.php page) <?php extract($_REQUEST); mysql_connect("localhost", "root", "password") or die(mysql_error()); mysql_select_db("test") or die(mysql_error()); if( isset($submit) ) { // If form has been submitted $status = ""; if( $title=="" ) { // Must provide an art title $status = "Please enter the art title.<br>"; } else { // Connect to the database and insert the new art if( isset( $id) && $id!="" ) { // Update art for artist 1 $sql = "UPDATE Art SET title='$title', " . "price='$price', description='$description', image='$image', artist_id='$artist_id'" . "WHERE id='$id'"; } else { // Insert new art for artist 2 $sql = "INSERT INTO Art (title, price, description, image, artist_id ) ". "VALUES ('$title', '$price', '$description', '$image', '$artist_id' )"; Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark. } mysql_query( $sql ) or die(mysql_error()); $status = "SUCCESSFULLY updated $title"; } } elseif ( isset($id) ) { // If id is set, but form has not // been submitted $sql = "SELECT Art.title, Art.description, Art.price, Art.image, Art.artist_id 3 FROM Art, Artist WHERE Art.artist_id=Artist.id AND Art.id='$id'"; $resultset = mysql_query( $sql ) // Get all art for an artist or die(mysql_error()); $row = mysql_fetch_assoc( $resultset ); extract( $row ); } else { $id=""; $title=""; $description=""; $price=""; $image=""; $artist_id=0; } ?> <? include("admin_header.php") ?> <h1>Art Update Screen</h1> 4 <form action="<?=$_SERVER['PHP_SELF']?>" method="get"> <input type="hidden" name="id" value="<?=$id?>"> <table> <? if (isset($status)) {?> <tr><td colspan="2"><b><?=$status?></b><br><br></td></tr> <? } ?> <tr><td>Title</td><td><input type="text" name="title" value="<?=$title?>" /></td></tr> <tr><td>Artist</td> 5 <td><select name="artist_id"> <option value="">Please Select</option> <?php 6 $sql = "SELECT * FROM Artist"; $resultset = mysql_query( $sql )or die(mysql_error()); while( $artist = mysql_fetch_assoc( $resultset ) ) { 7 if ( $artist['id'] == $artist_id ) $check=" selected=\"SELECTED\" "; else $check=""; print '<option value="' . $artist['id'] . "\" $check >" . $artist['name'] . "</option>\n"; } ?> Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark. </select></ td> </tr> 8 <tr><td>Description</td><td><textarea rows="10" cols="60" name="description" value="<?=$description?>" ><?=$description?></textarea></td></tr> <tr><td>Price</td><td><input type="text" name="price" value="<?=$price?>" /></td></tr> <tr><td>Image</td><td><input type="text" name="image" value="<?=$image?>" /> <small>(Path to the file relative to the root of the Web site)</small></td></tr> <tr><td colspan="2"><input type="submit" name="submit" 9 value="Submit"/></td></tr> </table> </form> <? include("admin_footer.php") ?> Explanation [ &L(E(C@CJH(LFD(CMH(EDC(I@HGH(QEB(IDFN@>H>(_BHH(R@PSDH(!):`(TK(GJ@GV@AP(E(J@AV(@A(CMH( admin_art_list.php(L@JHU(E(9X,(UPDATE(BCECH?HAC(@B(IDHIEDH>(LFD(EA(EDC@BC(TEBH>(FA(M@B(FD( MHD(id) 1 &L(E(AHQ(C@CJ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figure A.4. The user selected the Title Dragonfly from the admin_art_list.php file for update. (Note the id is passed in the URL.) ( A.3.3. Public Pages The public pages are the pages a user can view without being required to log in. The public pages for the Art Gallery are are index.php, artist_detail.php, and contact.php. All these pages include header.php and footer.php at the beginning and at the end of the page. The Header Page (header.php) The header.php page is an HTML page included to provide the navigation menu, consisting of links to the other pages for the site (see Example A.6 and Figure A.5). Typically, the links to the administration section would not be included for security reasons, but we include them here for this demo Web site to make it easier to navigate between the private and public pages. Session data verifies whether or not the user has successfully logged in, and if not, redirects him or her to a login page called admin_login.php, discussed in detail in Chapter 16, “Cookies and Sessions.” Example A.6. 6F>H(Y@HQZ( <html> <head> <title>The Canvas Gallery, San Francisco Art Gallery, Music Club, Bar, and Restaurant, Open Mic, Poetry, Film</title> <meta http-equiv="Content-Type" content="text/html; charset="iso-8859-1"> Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark. <link href="style.css" rel="stylesheet" type="text/css"> </head> <body bgcolor="#FFFFFF" leftmargin="0" topmargin="0" marginwidth="0" marginheight="0" <table cellSpacing=0 cellPadding=0 align=center border=0 borderColor="#CCCCCC" width="563" <tr> <td><br><IMG src="header.jpg" border="0" <div align="left"> [<a href="index.php">HOME</a>] [<a href="contact.php">Contact Us</a>] [<a href="admin_artist_list.php">Administration</a>] </div> </td> </tr> <tr> <td width="100%"><font face="Verdana, Arial, Helvetica, sans-serif"><br> Figure A.5. The header.php page. ( ( The Index Page (index.php) The initial public page is index.php (see Figure A.6). This page will be opened by default when the user comes to the Canvas Gallery Web site. The SQL SELECT statement (see line 1 in Example A.7) retrieves all the information for each artist by name, in sorted order, from the database. A function called art_for_artist is called for each record and is passed the id of the artist. Art for each artist is selected (line 3) and limited to one piece of art for each artist. That piece of art is returned by the function and used as the image to display with the artist’s information. Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark. [...]... < ?php 1 $sql = "SELECT * FROM Artist ORDER BY name"; mysql_ connect("localhost", "root", "password") or die (mysql_ error()); mysql_ select_db("test") or die (mysql_ error()); $recordset = mysql_ query( $sql ) or die (mysql_ error()); 2 function art_for_artist( $artist_id ) { 3 $sql = "SELECT * FROM Art WHERE artist_id='$artist_id' LIMIT 1 ;"; $recordset = mysql_ query( $sql ) or die (mysql_ error()); $row = mysql_ fetch_assoc(... artist_detail .php page (The ID of this artist is 12, as shown in the URL.)     Example A.8 Code  View:   < ?php extract($_REQUEST); mysql_ connect("localhost", "root", "password") or die (mysql_ error()); mysql_ select_db("test") or die (mysql_ error()); 1 $sql = "SELECT * from Artist WHERE id='$id'"; Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark $resultset = mysql_ query( $sql )or die (mysql_ error());... http://localhost/art_gallery/ and the list of the artists will appear A.5 Conclusion The purpose of presenting the Canvas Gallery Web project was to give you a chance to see the features of both PHP and MySQL working together as a team as described in Chapter 15, PHP and MySQL Integration.” Although there is much that could be added to this site, it serves as an example to demonstrate how to organize the pages and to use... sent Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark Appendix C PHP and Date/Time Working with dates and time is reasonably easy, but can be quite complex when dealing with different time zones, Daylight Saving Time, and so on PHP offers a range of functions to handle date and time, and many of these functions use the UNIX timestamp, the number of seconds since July 1, 1970,... message and the attachment text are divided into two parts and sent as multipart/mixed content separated by a boundary and each containing its own MIME headers Example B.3 Code  View:   < ?php // Reading file content 1 $FilePathName = "/tmp/golden_gate.gif"; // e.g /tmp/myapp/file.gz 2 $FileName = "golden_gate.gif"; // just file.gz 3 $FileHandle = fopen($FilePathName, "rb"); 4 $FileContent = fread($FileHandle,... must have successfully installed and configured PHP and MySQL A.4.1 Where to Find the Files for the Canvas Gallery Site To install the PHP files on your computer, unzip and copy the entire art gallery directory into your Web root—the default directory that is opened when you go to http://localhost/ on your computer The files can all be found on the CD A.4.2 Installing the MySQL Database Next, you need... install the database To do this, log into MySQL using either the mysql. exe command prompt (as shown in Figure A.10) or a GUI such as the MySQL Query Browser (see Figure A.11) We will use the “test” database If you choose the gallerydb.sql script to populate the database (see Figure A.12), there will be no initial data, and you will be responsible for adding the artists and their art work If you use the db.sql... messages originating from its own network Some ISPs require that the mail server request a login and password If this is the case, you can use the mail function provided by PHP Extension and Application Library (PEAR) PEAR’s Mail package defines an interface and functions for sending e-mail See http://pear .php. net/package/Mail You always have the option of setting up your own mail server to take delivery...  is  HTML and  that  the   encoding  is  in  the  U.S  ASCII  standard  Lines  4 and  5  state  that  the  content  of  the   message  is  HTML and  should  be  rendered  as  such 6 The  message  body  is  specified  here and  is  just  text and  plain  HTML 7 The  mail()  function  sends  the  e-­‐mail  to  the  mail  server, and  the  message  is  sent The e-mail message will look like that... provided with some demo art and artists Figure A.10 Logging into the MySQL “test” database       Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark   Figure A.11 Using the Query Browser, rather than the mysql. exe command prompt   Figure A.12 Once you log in, you could simply copy and paste the contents of the gallerydb.sql file into the SQL field and click the Execute button . Gallery are are index .php, artist_detail .php, and contact .php. All these pages include header .php and footer .php at the beginning and at the end of the. installed and configured PHP and MySQL. A.4.1. Where to Find the Files for the Canvas Gallery Site To install the PHP files on your computer, unzip and copy

Ngày đăng: 26/01/2014, 09:20

TỪ KHÓA LIÊN QUAN