Advertisement
gitlez

Untitled

Dec 6th, 2011
151
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.93 KB | None | 0 0
  1. <?php
  2.  
  3. $conn = mysql_connect('localhost', 'root' , '') or die('Cannot Connect To MySQL Server. <b' . 'r>' . mysql_error());
  4. mysql_select_db('test', $conn) or die('Cannot Select MySQL Database.<b' . 'r>' . mysql_error());
  5.  
  6. $guessbook_name = '';
  7. $guessbook_email = '';
  8. $guessbook_message = '';
  9. $errors = Array ();
  10.  
  11. if($_SERVER['REQUEST_METHOD'] === 'POST'){
  12.     $time = time ();
  13.     if (isset($_POST['guestbook_name'], $_POST['guestbook_email'], $_POST['guestbook_message'] )) {
  14.         $guessbook_name = mysql_real_escape_string( htmlentities( $_POST['guestbook_name'] ));
  15.         $guessbook_email = mysql_real_escape_string( htmlentities( $_POST['guessbook_email'] ));
  16.         $guessbook_message = mysql_real_escape_string( htmlentities( $_POST['guessbook_message'] ));
  17.         /** More Descriptive Errors Are Better  **/
  18.         if( strlen($guessbook_name) > 25){ $errors[] = '<span style="font-weight: bold";>Name</span> is too long -or- Contains Invalid Characters (Max 25 Characters).'; }
  19.         if( strlen($guessbook_email) > 255){ $errors[] = '<span style="font-weight: bold";>Email</span> is too long -or- Contains Invalid Characters (Max 255 Characters).'; }
  20.         if( strlen($guessbook_message) > 255){ $errors[] = '<span style="font-weight: bold";>Message</span> is too long -or- Contains Invalid Characters (Max 255 Characters).'; }
  21.     }else{
  22.         $errors[] = 'All Fields Are Required.';
  23.     }
  24.    
  25.     /** If No Errors, Proceed And Insert Data In To Database    **/
  26.     if(count($errors) === 0){
  27.         $insert = "INSERT INTO entries VALUES('', '{$time}', '{$guessbook_name}', '{$guessbook_email}', '{$guessbook_message}')";
  28.         if(!mysql_query( $insert )){
  29.             $errors[] = 'Server Error, please try again later.';
  30.             // Really should log the error.
  31.         }
  32.     }
  33. }
  34.  
  35.  
  36. $form = <<<FORM
  37.     <form method="post" action="{$_SERVER['PHP_SELF']}">
  38.         <span style="font-weight: bold">Post Something...</span><br>
  39.         Name:
  40.         <br>
  41.         <input type= "text" name="guestbook_name" maxlength="25" value="{$guessbook_name}">
  42.         <br>
  43.         Email
  44.         <br>
  45.         <input type= "text" name="guestbook_email" maxlength"255" value="{$guessbook_email}">
  46.         <br>
  47.         Message:
  48.         <br>
  49.         <textarea name= "guestbook_message" rows="6" cols="30" maxlength="255">{$guessbook_message}</textarea>
  50.         <br>
  51.         <input type="submit" value="Post">
  52.     </form>
  53. FORM;
  54.  
  55. $entries = '';
  56. $results = mysql_query("SELECT timestamp,name,email,message FROM entries ORDER BY timestamp DESC") or die(mysql_error());
  57. if(mysql_num_rows($results) === 0){
  58.     $entries = '<p>No Entries Yet</p>';
  59. }else{
  60.     while($row = mysql_fetch_assoc($results)){
  61.         $timestamp = date('D M Y @ h:i:s', $row['timestamp']);
  62.         $entries .= <<<ENTRY
  63.             <p>
  64.                 <span style="font-weight: bold;">
  65.                     Posted By: {$row['name']}({$row['email']}) on {$timestamp}.                
  66.                 </span>
  67.                 <br>
  68.                 {$row['message']}
  69.             </p>
  70. ENTRY;
  71.     }
  72. }
  73.  
  74.  
  75. if(count($errors) > 0){
  76.     echo '<p><span style="font-weight: bold;">Errors</span><br>';
  77.     echo implode('<br>', $errors);
  78.     echo '</p>';
  79. }
  80. echo $form;
  81. echo $entries;
  82.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement