17-mysql_database/address_book.php

Go to the documentation of this file.
00001 <?php
00010 //This file is used to define common functions which are used in
00011 //implementing address book code
00012 require_once('common_functions.php');
00013 
00014 
00017 function address_book_page()
00018 {
00019     global $message;
00020     global $name;
00021     global $mobile_number;
00022     global $room_number;
00023     global $roll_number;
00024     global $iiit_mail_id;
00025     global $other_mail_id;
00026     global $pc_name;
00027     global $date_of_birth;
00028     global $gaming_nick;
00029     global $peer_web_nick;
00030     global $remote_address;
00031     
00032     //create a different random image each time page is dispayed
00033     create_random_image();
00034     
00035     ?>
00036         <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
00037         <html>
00038         <head>
00039             <title> Address Book for www.sbarjatiya.in </title>
00040             <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"/>
00041             <link rel="stylesheet" type="text/css" href="style1.css" />
00042         </head>
00043         <body style="background-color:black; color:white">
00044             <form method="POST" action="address_book.php?<?php echo session_id() . "\">"; ?>
00045                 <span class="large arial blue">
00046                     <?php echo "<br/>$message"; ?>
00047                 </span>
00048                 <div class="arial">
00049                     Currently stored details:<br>
00050                     <table border=1 cellspacing=2 cellpadding=2>
00051                         <thead>
00052                             <tr>
00053                                 <th>S. No. </th>
00054                                 <th>Name </th>
00055                                 <th>Mobile No</th>
00056                                 <th>Room No</th>
00057                                 <th>Roll number</th>
00058                                 <th>IIIT mail id</th>
00059                                 <th>Other mail id</th>
00060                                 <th>IP address</th>
00061                                 <th>Date Of Birth</th>
00062                                 <th>Gaming Nick </th>
00063                                 <th>Peer Web Nick</th>
00064                             </tr>
00065                         </thead>
00066                         <tbody>
00067                             <?php
00068                                 $connection=connect_to_database();
00069                             
00070                                 $sql = "SELECT * from directory";
00071                                 $result= mysql_query($sql, $connection);
00072                         
00073                                 if(!$result)
00074                                     die("Cannot select from table directory since, " . mysql_error());
00075 
00076                                 $total = mysql_num_rows($result);
00077                                 for ($counter1 = 0; $counter1 < $total; $counter1++) 
00078                                 {
00079                                     echo "<tr>\n";
00080                                     //Use mysql_fetch_array instead of mysql_array_row for more
00081                                     //robust code which does not depends on order of columns.
00082                                     $row_array = mysql_fetch_array($result, MYSQL_ASSOC);
00083                                     echo "<td>" . ($counter1+1) . ". </td>\n";
00084                                     echo "<td>" . htmlspecialchars( stripslashes( $row_array["name"])) . "</td>\n";
00085                                     echo "<td>" . htmlspecialchars( stripslashes( $row_array["mobile_number"])) . "</td>\n";
00086                                     echo "<td>" . htmlspecialchars( stripslashes( $row_array["room_number"])) . "</td>\n";
00087                                     echo "<td>" . htmlspecialchars( stripslashes( $row_array["roll_number"])) . "</td>\n";
00088                                     echo "<td>" . htmlspecialchars( stripslashes( $row_array["iiit_mail_id"])) . "</td>\n";
00089                                     echo "<td>" . htmlspecialchars( stripslashes( $row_array["other_mail_id"])) . "</td>\n";
00090                                     echo "<td>" . htmlspecialchars( stripslashes( $row_array["pc_name"])) . "</td>\n";
00091                                     echo "<td>" . htmlspecialchars( stripslashes( $row_array["date_of_birth"])) . "</td>\n";
00092                                     echo "<td>" . htmlspecialchars( stripslashes( $row_array["gaming_nick"])) . "</td>\n";
00093                                     echo "<td>" . htmlspecialchars( stripslashes( $row_array["peer_web_nick"])) . "</td>\n";
00094                                     echo "</tr>\n";
00095                                 }
00096 
00097                                 mysql_free_result($result);
00098                                 mysql_close($connection);
00099                             ?>
00100                         </tbody>
00101                     </table>
00102                     <br/><br/>
00103                     You can add your own details to the list:<br>
00104                     <table>
00105                         <tr>
00106                             <td>*Name : </td>
00107                             <td> <input type=text name="name" <?php echo "value='$name'"; ?> /><br/></td>
00108                         </tr>
00109                         <tr>
00110                             <td>Mobile No : </td>
00111                             <td> <input type=text name="mobile_number" <?php echo "value='$mobile_number'"; ?> /><br/></td>
00112                         </tr>
00113                         <tr>
00114                             <td>Room No : </td>
00115                             <td> <input type=text name="room_number" <?php echo "value='$room_number'"; ?> /><br/></td>
00116                         </tr>
00117                         <tr>
00118                             <td>*Roll number : </td>
00119                             <td> <input type=text name="roll_number" <?php echo "value='$roll_number'"; ?> /><br/></td>
00120                         </tr>
00121                         <tr>
00122                             <td>IIIT mail id : </td>
00123                             <td> <input type=text name="iiit_mail_id" <?php echo "value='$iiit_mail_id'"; ?> /><br/></td>
00124                         </tr>
00125                         <tr>
00126                             <td>Other mail id : </td>
00127                             <td> <input type=text name="other_mail_id" <?php echo "value='$other_mail_id'"; ?> /><br/></td>
00128                         </tr>
00129                         <tr>
00130                             <td>PC Name : </td>
00131                             <td> <input type=text name="pc_name" <?php echo "value='$pc_name'"; ?> /><br/></td>
00132                         </tr>
00133                         <tr>
00134                             <td>Date Of Birth : </td>
00135                             <td> <input type=text name="date_of_birth" <?php echo "value='$date_of_birth'"; ?> /><br/></td>
00136                         </tr>
00137                         <tr>
00138                             <td>Gaming Nick : </td>
00139                             <td> <input type=text name="gaming_nick" <?php echo "value='$gaming_nick'"; ?> /><br/></td>
00140                         </tr>
00141                         <tr>
00142                             <td>Peer Web Nick : </td>
00143                             <td> <input type=text name="peer_web_nick" <?php echo "value='$peer_web_nick'"; ?> /><br/></td>
00144                         </tr>
00145                         <tr>
00146                             <td>Verification Code : </td>
00147                             <td>
00148                                 <input type=text name=random_code value="" />
00149                                 <?php 
00150                                     echo '<img alt="random image" src="' . IMAGE_FILENAME . '"/>';
00151                                 ?>
00152                             </td>
00153                     </table>
00154                     <br/>
00155                     <input type=submit value="Submit" name="submit1" />
00156                 </div>
00157             </form>
00158             </body>
00159         </html>
00160     <?php
00161     
00162 }
00163 
00164 
00172 function verify_post_data()
00173 {
00174     global $message;
00175     global $name;
00176     global $mobile_number;
00177     global $room_number;
00178     global $roll_number;
00179     global $iiit_mail_id;
00180     global $other_mail_id;
00181     global $pc_name;
00182     global $date_of_birth;
00183     global $gaming_nick;
00184     global $peer_web_nick;  
00185     
00186     $error_found=false;
00187 
00188     if(strlen($name) > 50 || strlen($name) <3)
00189     {
00190         $message .= "Name must be between 3 and 50 characters.<br/>";
00191         $error_found=true;
00192     }
00193     
00194     if(strlen($mobile_number) > 20)
00195     {
00196         $message .= "Mobile no must be less than 20 characters.<br/>";
00197         $error_found=true;
00198     }
00199 
00200     if(strlen($room_number) > 10)
00201     {
00202         $message .= "Room no must be less than 10 characters.<br/>";
00203         $error_found=true;
00204     }
00205 
00206     if(strlen($roll_number) != 9)
00207     {
00208         $message .= "Roll number must be of 9 characters.<br/>";
00209         $error_found=true;
00210     }
00211 
00212     if(strlen($iiit_mail_id) > 50)
00213     {
00214         $message .= "IIIT mail id must be less than 50 characters.<br/>";
00215         $error_found=true;
00216     }
00217     
00218     if(strlen($other_mail_id) > 50)
00219     {
00220         $message .= "Other mail id must be less than 50 characters.<br/>";
00221         $error_found=true;
00222     }
00223 
00224     if(strlen($pc_name) > 20)
00225     {
00226         $message .= "PC name must be less than 20 characters.<br/>";
00227         $error_found=true;
00228     }
00229 
00230     if(strlen($date_of_birth) > 20)
00231     {
00232         $message .= "Date Of Birth must be less than 20 characters.<br/>";
00233         $error_found=true;
00234     }
00235     
00236     if(strlen($gaming_nick) > 20)
00237     {
00238         $message .= "Gaming nick must be less than 20 characters.<br/>";
00239         $error_found=true;
00240     }
00241     
00242     if(strlen($peer_web_nick) > 20)
00243     {
00244         $message .= "Peer web nick must be less than 20 characters.<br/>";
00245         $error_found=true;
00246     }
00247 
00248     return $error_found;
00249 }
00250 
00251 
00257 function main()
00258 {
00259     global $random_code;
00260     global $verification_code;
00261     global $message;
00262     global $name;
00263     global $mobile_number;
00264     global $room_number;
00265     global $roll_number;
00266     global $iiit_mail_id;
00267     global $other_mail_id;
00268     global $pc_name;
00269     global $date_of_birth;
00270     global $gaming_nick;
00271     global $peer_web_nick;  
00272     global $remote_address;
00273     
00274     //get proper i.p. address of client, if he has not bypassed proxy
00275     //calculate it before displaying the page, so that user can be 
00276     //informed about his i.p. address.
00277     $remote_address = get_remote_address();
00278     
00279     //check whether page is opened for first time.
00280     if($random_code == "" && $name=="" && $mobile_number=="" && $room_number == ""
00281               && $roll_number=="" && $iiit_mail_id=="" && $other_mail_id=="" && $pc_name ==""
00282               && $date_of_birth == "" && $gaming_nick =="" && $peer_web_nick == "" )
00283         $first_time=true;
00284     else
00285         $first_time=false;
00286         
00287     //if it is opened for first time display the page and exit.
00288     if($first_time)
00289     {
00290         address_book_page();
00291         exit;
00292     }
00293 
00294     $error_found=verify_post_data();
00295     
00296     if($error_found==true)
00297     {
00298         address_book_page();
00299         exit;
00300     }
00301 
00302     //first verify that random_code submitted is correct. If it is incorrect
00303     //display the error message and do not process further.
00304     if($random_code!=$verification_code)
00305     {
00306         $message .= "Verification code is incorrect<br/>";
00307         address_book_page();
00308         exit;   
00309     }
00310 
00311     
00312     $connection=connect_to_database();
00313     
00314     $sql = "INSERT INTO directory VALUES(";
00315     $sql .= "'" . addslashes($name) . "',";
00316     $sql .= "'" . addslashes($mobile_number) . "',";
00317     $sql .= "'" . addslashes($room_number) . "',";
00318     $sql .= "'" . addslashes($roll_number) . "',";
00319     $sql .= "'" . addslashes($iiit_mail_id) . "',";
00320     $sql .= "'" . addslashes($other_mail_id) . "',";
00321     $sql .= "'" . addslashes($pc_name) . "',";
00322     $sql .= "'" . addslashes($date_of_birth) . "',";
00323     $sql .= "'" . addslashes($gaming_nick) . "',";
00324     $sql .= "'" . addslashes($peer_web_nick) . "',";
00325     $sql .= "'" . addslashes($remote_address) . "')";
00326     
00327     $result = mysql_query($sql, $connection);
00328     
00329     if(!$result)
00330         die("Query failed since, " . mysql_error() . "<br/> Query was " .  $sql);
00331 
00332     mysql_free_result($result);
00333     mysql_close($connection);       
00334     
00335     redirect('thank_you.php');
00336 }
00337 
00338 
00339 
00344 $random_code=trim(get_post('random_code'));
00345 
00346 
00350 $verification_code=get_session('verification_code');
00351 
00352 
00357 $remote_address = ""; 
00358 
00359 
00364 $message="";
00365 
00366 
00367 $name=trim(get_post('name'));
00368 $mobile_number=trim(get_post('mobile_number'));
00369 $room_number=trim(get_post('room_number'));
00370 $roll_number=trim(get_post('roll_number'));
00371 $iiit_mail_id=trim(get_post('iiit_mail_id'));
00372 $other_mail_id=trim(get_post('other_mail_id'));
00373 $pc_name=trim(get_post('pc_name'));
00374 $date_of_birth=trim(get_post('date_of_birth'));
00375 $gaming_nick=trim(get_post('gaming_nick'));
00376 $peer_web_nick=trim(get_post('peer_web_nick'));
00377     
00378 main();
00379 ?>

Generated on Fri Nov 4 14:16:54 2011 for PHP example documentation by  doxygen 1.4.7