PHP/MySQL Guestbook, by Javier Arevalo:



This is the source code for the MySQL/PHP Guestbook. Notice that I use some of my styles here, change them along with the server, user and similar fields. Hopefully, the code is simple enough that my mistakes and lack of real discipline as a PHP/MySQL beginner won't hurt too much.

SQL Table creation:

CREATE TABLE Guests (
  ID        INT  NOT NULL AUTO_INCREMENT PRIMARY KEY,
  GuestName TEXT NOT NULL,
  GuestDate TEXT NOT NULL,
  GuestText TEXT NOT NULL
);

PHP Code:

<?php
// To be enhanced later
function nice_reformat($str)
{
  $new_str = htmlentities($str);
  return $new_str;
}

// Count elements in a table
function row_count($table, $filter="")
{
  $sqlq = "SELECT COUNT(*) AS count FROM $table " . $filter;
  $result = mysql_query( $sqlq );
  if (!$result)
  {
    return 0;
  }

  $row = mysql_fetch_array($result);
  mysql_free_result($result);
  if (!$row or !$row[0])
  {
    return 0;
  }
  return $row[0];
}

  if (!isset($first_guest) or !is_numeric($first_guest) or $first_guest < 0)
  {
    $first_guest = 0;
  }

  // Connect to the database server
  $dbcnx = @mysql_connect("your_mysql_server", "your_user", "your_password");  // Change these values!
  if (!$dbcnx)
  {
    echo( "<p>Unable to connect to the database server at this time.</p>" );
    exit();
  }

  // Select the database
  if (! @mysql_select_db("your_database") )  // Change this value!
  {
    echo( "<p>Unable to locate the guestbook database at this time.</p>" );
    exit();
  }

  // Insert pending signature
  if (isset($_POST) and isset($login) and isset($_POST['login']) and $_POST['login'] == "do")
  {
    $name = trim($_POST['name']);
    $msg  = trim($_POST['message']);
    if ($name != "" and $msg != "")
    {
      // Don't duplicate entries.
      $was = row_count("Guests", "WHERE (GuestName='$name' AND GuestText = '$msg')");
      if ($was == 0)
      {
        $sql  = "INSERT INTO Guests SET GuestName='$name', GuestText ='$msg', GuestDate=NOW()";
        $sqlq = mysql_query($sql);
      }
    }
  }

  echo ('<form action="' . $_SERVER['PHP_SELF'] . '" method="post">');
  echo ('Your Name: <input type="text" name="name" size="20" maxlength="20"><br />'); 
  echo ('Your Mesage: <input type="message" name="message" size="100" maxlength="200"><br />');
  echo ('<input type="hidden" name="login" value="do">');
  echo ('<br/><input type="submit" value="Leave your message!">'); 
  echo ('</form>');

?>

<?php
  // Request the guests
  $total_guests = row_count("Guests");
  
  $default_max_guests = 10;
  $max_guests = $default_max_guests;
  if ($first_guest >= $total_guests)
  {
    $first_guest = 0;
  }
  if ($first_guest+$max_guests > $total_guests)
  {
    $max_guests = $total_guests - $first_guest;
  }

  echo ("<p>Previous visitors (" . strval($first_guest+1) . "-" . strval($first_guest+$max_guests) . " of $total_guests):</p>");
  
  $sqlq = "SELECT GuestName,GuestText,GuestDate FROM Guests ORDER BY ID DESC LIMIT $first_guest,$max_guests";
  $result = mysql_query( $sqlq );
  if (!$result)
  {
    echo("<p>Error performing query: " . mysql_error() . "</p>");
    exit();
  }

  echo '<table class="newtbl"><tr><td class="sep" /></td></tr></table>';
  // Display the text of each entry
  while ( $row = mysql_fetch_array($result) )
  {
    echo('<table class="newtbl"><tr><td><table width="100%"><tr><td><b>'
       . htmlentities($row["GuestName"]) . '</b> says:</td><td class="date">'
       . $row["GuestDate"] . '</td></tr></table></td></tr></table>'
       . '<table class="newtbl"><tr><td class="new">'
       . nice_reformat($row["GuestText"]) . '<br/></td></tr>'
       . '<tr><td class="sep" /></td></tr></table>');
  }
  mysql_free_result($result);

  // Lame navigation controls
  if ($first_guest > 0)
  {
    $link = '<a href="' . $_SERVER['PHP_SELF'] . '?first_guest='
          . (($first_guest < $default_max_guests)? 0 : $first_guest-$default_max_guests)
          . '">&lt;&lt;</a>&nbsp;';
    echo ($link);
  }
  if ($first_guest + $default_max_guests < $total_guests)
  {
    $link = '&nbsp;<a href="' . $_SERVER['PHP_SELF'] . '?first_guest='
          . ($first_guest+$default_max_guests) . '">&gt;&gt;</a>';
    echo ($link);
  }
?>





(C) Copyright 2000-2003
Javier Arévalo Baeza