Hi,

I'm trying to create a simple guestbook following a tutorial. I stucked at some point.

So this is where I am; i've created the form and some php code as well as the database table

<?php
	require($_SERVER["DOCUMENT_ROOT"]."/guest/config/db_config.php");
	$connection = @mysql_connect($db_host, $db_user, $db_password) or die ("error connection");
	mysql_select_db($db_name, $connection);

$name = $_POST["txt_name"];
$len = strlen($name);

//Only write to database if there's a name
if ($len > 0)
{
  $email = $_POST["txt_email"]; //see if there is an email element
  $comment = $_POST["txt_comment"];
  $date = time(); //returns the date in seconds	

  //Now, have to set up mysql query
  $query = "INSERT INTO guestbook (ID, name, email, comment, date_auto) VALUES (NULL '$name', '$email', '$comment', '$date')";

  //Now, we have to run the actual query
  mysql_query($query, $connection) or die (mysql_error()); //save it to the database or send an error

}
?>

<html>
  <head>
    <title>Guestbook</title>
  </head>
<body>
<center>
<form action="<?php echo $SERVER[PHP_SELF]; ?>" method="POST">

 <font face="arial" size="1">
    Name: <input type="text" name="txt_name">&nbsp;
	Email: <input type="text" name="txt_email"><br /><br />
	Comment: <br />
	<textarea style="width: 75%" rows="10" name="txt_comment"></textarea>
	<center><input type="submit" value="Submit"></center>
	</font>
</form></center>

</body>
</html>

This is the table i1ve created:

CREATE TABLE IF NOT EXISTS `guestbook` (
  `ID` int(10) unsigned NOT NULL auto_increment,
  `name` varchar(64) default NULL,
  `email` varchar(64) default NULL,
  `comment` text,
  `date_auto` int(10) unsigned default NULL,
  PRIMARY KEY  (`ID`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;

The form appears in the browser but when I try to send a message i get this sql error:

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ''tom', 'dtommy79@yahoo.com', 'test message', '1219671750')' at line 1

I don't really know what it means
Any help is appreciated

    Can't you just include the config file like this?

    include '/guest/config/db_config.php'; 

    instead of

    require($_SERVER["DOCUMENT_ROOT"]."/guest/config/db_config.php"); 

      yeah... I guess, but that has nothing to do with the problem.

        Sorry, I was just trying to help..

          obel;10883696 wrote:

          Sorry, I was just trying to help..

          No problem. Thanks for the comment

            You're inserting a NULL into the ID field with your query, but you have no comma between NULL and '$name'. Also, if the query is always going to put NULL into this field, you are better off leaving this field out of your SQL statement and setting the field to use this value as the default in phpMyAdmin

              Ashley Sheridan;10883713 wrote:

              You're inserting a NULL into the ID field with your query, but you have no comma between NULL and '$name'. Also, if the query is always going to put NULL into this field, you are better off leaving this field out of your SQL statement and setting the field to use this value as the default in phpMyAdmin

              Many thanks, that fixed the problem.

                Ashley Sheridan;10883719 wrote:

                No problem. Don't forget to mark the thread off as resolved 😉

                Ok just one thing before that.

                I managed to go through with this, but I have one question.

                When you add a message it is displayed right below the form. if I refresh the browser it resubmits the message.
                Is there a way to prevent that?

                  Before running the INSERT query, run a SELECT query to check to see if the exact same message has just been posted.

                    ahm.... i'm not sure how it should look like....

                      Write a Reply...