will only write to table once

I’m trying to do a php/mysql guestbook. Very simple and strait forward. But it will only work once, then breaks. I don’t know if it’s the code or the way I have the table in mysql set up. here’s the code.

[php]

Guestbook Comments
First Name :
Last Name :
Email :
Comment :
[/php]

[php]

Guestbook <?php $host=""; // Host name $username=""; // Mysql username $password=""; // Mysql password $db_name=""; // Database name $tbl_name=""; // Table name

// Connect to server and select database.
mysql_connect("$host", “$username”, “$password”)or die(“cannot connect”);
mysql_select_db("$db_name")or die(“cannot select DB”);

// Get values from form
$name=$_POST[‘name’];
$lastname=$_POST[‘lastname’];
$email=$_POST[‘email’];
$comment=$_POST[‘comment’];

// Insert data into mysql
$sql=“INSERT INTO guestbook(name, lastname, email, comment)VALUES(’$name’, ‘$lastname’, ‘$email’, ‘$comment’)”;
$result=mysql_query($sql);

// if successfully insert data into database, displays message “Successful”.
if($result){
echo “Successful”;
echo “
”;
echo “Back to main page”;
}

else {
echo “ERROR”;

}

// close connection
mysql_close();

?>

[/php]

It seems to write things one time. then I try it a second time and it stops working. I delete the table and start over and it’s good again. This came from a tutorial, but I did do some revisions.

i see some very wrong html in there and a couple of issues with the php, but nothing that would break it. There’s no reason to have this on two different pages, it just makes things really difficult to troubleshoot. This is what i’d do.

[php]

<?php // put your connection stuff into a seperate file require_once 'dbconn.php'; // set your defaults $mes = "" $err = "" // Get values from form if(isset($_POST['Submit'])) { // name of submit button $name= mysql_real_escape_string($_POST['name']); $lastname=mysql_real_escape_string($_POST['lastname']); $email=mysql_real_escape_string($_POST['email']); $comment= mysql_real_escape_string($_POST['comment']); $ins = mysql_query("INSERT INTO guestbook (name, lastname, email, comment) VALUES('$name', '$lastname', '$email', '$comment')"; if($ins){ $mes = "Successful"; } else { $err = 1; } } if(!isset($mes) || isset($err)) { ?>
Guestbook Comments
First Name : />
Last Name : />
Email : />
Comment : value=<?= nl2br($_POST['comment']); ?>
<?php } else { echo $msg; } ?>
[/php]

If this is going to be a public page, i would really suggest you use some kind of form validation. improper data can really screw up a database (i found this out the hard way), specially when the public is going to see it.

mysql_real_escape() simply escapes quotes to prevent sql injection, just be sure to see if you’re host has magic quotes turned on first, else it’ll end up being a nightmare to fix.

i don’t know which tutorial told you put the form inside the table, but that would screw things up and i didn’t see a nested table, so i took the excess /table tag out.

I changed it around so that if there is an error, it’ll display the table with the user’s previous data. if its good, it’ll just display the success message.

Thanks much for the help. I will work on it tonight. have a great day!

Sponsor our Newsletter | Privacy Policy | Terms of Service