MySQL Problems

tearsfall

New Member
Messages
7
Reaction score
0
Points
0
I made a premature guestbook for my site using PHP and MySQL. For some reason when I use the SELECT statement ... my first guestbook entry doesn't show, but all subsequent entries show.

My site is:

http://tearsfall.pcriot.com

My guestbook is:

http://tearsfall.pcriot.com/guestbook.php

I'm using the mysql_escape_string() for the guestbook, for some reason it works without the escape string.

Here is my guestbook.php code:

Code:
<html>
  <head>
    <title>Guestbook v1.061</title>
  </head>

  <body text="#000000">

  <p align="center">
    <table border="1" cellpadding="0" cellspacing="0" width="33%">

      <?php
      
        require "connect.php";
        
        $query = "SELECT * FROM guestbook";
        $result = mysql_query($query) or die(mysql_error());
                 
        if(mysql_fetch_array($result)=="") {
          echo "<tr rowspan='2'><td width='100%'>";
          echo "No entries.";
          echo "</td></tr>";
        }
        else {
       
          while($row = mysql_fetch_array($result)) {
                  
            echo "<tr><td width='100%' bgcolor='#666666'>";
            echo "<font color='#FFFFFF'><strong>&nbsp;&nbsp;&nbsp;";
            echo $row['messageID'] . " " . $row['guest_name'];
            echo "</strong></font>";
            echo "</td></tr>";
            
            echo "<tr><td width='100%' bgcolor='#999999'>";
            echo $row['message_post'];
            echo "</td></tr>";
          }
        }
       
       mysql_close($connection); 
       
      ?>

    </table>
  </p>
  <p align="center">
   Add to the guestbook. The current supported form is for guests. Registered users will eventually be supported.
  </p>
   <form id="guestbook" method="post" action="./guestbook_process.php">
     <p align="center">
      <table border="1" cellspacing="0" cellpadding="0" width="33%">
       <tr>
        <td align="center">Guest name: <input type="text" id="guest_name" name="guest_name" /></td>
       </tr>
       <tr>
        <td><textarea cols="38" rows="8" id="message_post" name="message_post"></textarea><br />255 Characters<br /><br /><input type="submit" id="submit" name="submit" value="Add" /> <input type="reset" id="reset" name="reset" value="clear" /></td>
       </tr>
      </table>
     </p>
    </form>

  </body>
</html>

here is my guestbook_process.php code:

Code:
<html>
  <head>
    <title>Adding to Guestbook</title>

    <script type="text/javascript">
    <!--
      function delayer()
        {
          window.location = "./guestbook.php"
        }
    //-->
    </script>
  </head>

  <body onLoad="setTimeout('delayer()', 1000)">

    <?php

      require "connect.php";

      $guest_name = mysql_escape_string($_POST['guest_name']);
      $message_post = mysql_escape_string($_POST['message_post']);
  
  
      if(empty($guest_name)||empty($message_post)) {
        die('One or more fields are missing to enter your record to our guestbook.');
      }
  
      $query = mysql_query("INSERT INTO 
                        guestbook(messageID,guest_name,message_post)
                        VALUES('','$guest_name','$message_post');");
                        
      echo "Thank you for adding to our guestbook.  You will be redirected to the guestbook page in 1 seconds.";
      
     mysql_close($connection);
     
    ?>

  </body>
</html>

My guestbook database is defined as follows

guestbook(messageID,guest_name,message_post)
 

xPlozion

New Member
Messages
868
Reaction score
1
Points
0
check phpMyAdmin to see if there's any data in the fields. That'll tell us if it's when you try to select data, or when you try to insert it.
 

tearsfall

New Member
Messages
7
Reaction score
0
Points
0
There is data in the fields. It's just simply not returning the first row when using the SELECT statement. Maybe there are invalid characters in my program from where I used notepad2 ... maybe I need to retype it or something?
 

scopey

New Member
Messages
62
Reaction score
0
Points
0
You are requesting the first row in your if statement:

if(mysql_fetch_array($result)==""){

Use instead:

if(mysql_num_rows($result)<1){
 
Top