Hey, just wondering if anyone would mind having a look at my code to tell me what i'm doing wrong.

What i'm trying to do is build a guestbook. I have:
'Guestbook.html' - The form for signing the guestbook
'Gsign.php' - the script that adds the entry to the database
'Gview.php' - the script that gets the data from the database and prints it on screen

The trouble i am having is the only thing that 'Gview.php' is displaying is '1' 🙁

Here is the code i am using:

----Gsign.php----

<?php
$dbhost = "localhost";
$dbname = "steven";
$dbpass = "scarred9";
$connection = @mysql_connect($dbhost, $dbname, $dbpass) or die("Error connecting");

$result1 = @mysql_select_db('mydatabase',$connection) or die("Error3");

            $name = ($_POST['name']);
$message = ($_POST['message']);
$date_added = date("l dS of F Y h:i:s A");

$sql = "INSERT INTO gbook_data
(gbookname, gbookmessage, gbookdate)
VALUES
(\"$name\", \"$message\", \"$date_added\")

";
$result = mysql_query($sql, $connection) or die("Error4");

echo ("<p align='center'>Your message has been added successfully.<br>Click <a href='Gview.php'>here</a> to view the guestbook");

unset($connection);
unset($result);
unset($result1);

---Gview.php----

<?php
$db_name = "scarred_mydatabase";
$dbhost = "localhost";
$dbname = "steven";
$dbpass = "scarred9";
$connection = @mysql_connect($dbhost, $dbname, $dbpass) or die("Error connecting");
mysql_select_db($db_name, $connection) or die("Error selecting database");

$sql = "SELECT * FROM gbook_data ORDER BY gbookdate DESC LIMIT 0,50";
$result = @($sql, $connection) or die("Couldnt execute query");

while ($row=mysql_fetch_array($result)) {
$name = $row['gbookname'];
$message = $row['gbookmessage'];
$date_added = $row['gbookdate'];
$display_block = "<b>Name: </b>$name<br><b>Date Added: </b>$date_added<br><b>Message: </b>$message<hr>";
}
echo ("display_block") or die("Couldn't echo");
?>

As i say, all i get from 'Gview.php' is the number '1'. And i cant figure out why. Thanks in advance...

BIOSTALL

    This is happening because of your:

    echo ("display_block") or die("Couldn't echo");
    

    It is returing 1 as in true, this is how the boolean statement works. echo is a language construct. Think of it as a void function (if you have programming experience elsewhere), it has no return, no true, no false, nothing. It just outputs what you pass it. There is no need to check if it suceeds or fails because it doesn't allow you to do it. If echo failed, most likely php crash.

    Now, if for some strange reason you expect echo to fail, you could always do output buffering and check that everything you echo reaches the buffer - however, I highly doubt it is that much of a concern to you.

    Simply echo $display_block (not "display_block" as that would display the string "display_block").

    Notes:
    Please use the

     bbcode tag in future, makes code easier to read for other members.
    
    Also, research SQL Injection.

      Wow!! thanks for the very quick reply. U'r a star!!

      Just one more problem:
      The date that get echoed is always 0000-00-00. What i want is it in the format (Monday 15 Oct 22:30).

      I can work out the format myself, i just need to know why it's always 0000-00-00

      Thanks

      BIOSTALL 😃

        I am pretty sure that is because of MySQL. If you are inserting your date format into MySQL datetime or date field, it doesn't reconize it as a date and therefore rejects it. It however needs to have a date, so it uses the one you are presented. One way around this is just to use MySQL functions to insert the date (such as NOW) and then format the date with the MySQL function format_date (or with php functions). Another would be to change the field to a CHAR (not reccommended). Or, insert the date with PHP function formatting it the way MySQL wants it, then formatting it again the way you want it before displaying it.

        Look at PHP date functions and MySQL date functions for guidance. I hope you understand the problem at hand.

        Good Luck

          Brilliant, thanks for that. Have got it sorted it out now 🙂

          Just ONE more problem though:

          'Gview.php' will display the first entry i add, but if i add one another entry, this first entry will still remain with the second entry not being added to it.

          Any ideas Please? I think it's probably got something to do with the way i select the records from the database:

          while ($row=mysql_fetch_array($result)) {
          	$name = $row['gbookname'];
          	$message = $row['gbookmessage'];
          	$date_added = $row['gbookdate'];
          	$display_block = "<b>Name: </b>$name<br><b>Date Added: </b>$date_added<br><b>Message: </b>$message<hr>";
          	}
          
          echo $display_block;

          Thanks again

          BIOSTALL

            while ($row=mysql_fetch_array($result)) {
                $name = $row['gbookname'];
                $message = $row['gbookmessage'];
                $date_added = $row['gbookdate'];
                // The . will mean it is appended to the end of string, before you were overwriting it with the new row
                $display_block. = "<b>Name: </b>$name<br><b>Date Added: </b>$date_added<br><b>Message: </b>$message<hr>";
                }
            
            echo $display_block;
            

            Simple enough.

              My god, you deserve a medal 😉 Thanks a lot for that, dunno what i'd do without you.

              BIOSTALL

                Write a Reply...