Jump to content

[Solved] Guestbook


Sami

Recommended Posts

I have a problem making a guestbook.It's the first time I'm using MySQL, and I'm not getting the post.php-file working! :) I'm also just getting a blank screen!Here's the file content:

<!doctype html><head><title>Post</title></head><body><?php$con = mysql_connect("localhost","gbok","******");if (!$con)  {  die('<div style='border:1px red;'>Beklager, databasefeil. Feilkode: ' . mysql_error() . '</div>');  }else  {  echo "<div style=\"border:1px lime; displpay:block;\">Kommentaren din var vellykket lagret!</div>";  }mysql_select_db("gbok", $con);mysql_query("INSERT INTO comments (name, comment, date)VALUES ($_POST['name'], $_POST['comment'], date('d.m.Y H:i'))");mysql_close($con);?><br><a href="./">Tilbake</a></body></html>

btw, right up my writing mistakes.

Link to comment
Share on other sites

@thescientist: die echoes its argument before terminating the script.@Sami: After if (!$con) you have a die statement and an echo statement. The quotation marks in the echo statement are escaped correctly. They are escaped incorrectly in the die statement. This will cause a parsing error (blank screen), and it is probably your problem right now.You really should check the return values of all mysql statements.Always use isset() or empty() to test the value of $_POST elements before you try to use them. If a user leaves a field blank, a POST value might not exist. Trying to access it will throw a warning and might lead to other runtime errors.

right up my writing mistakes.
You mean: write up my writing mistakes. That's the kind of mistake I expect from native speakers, however. :)
Link to comment
Share on other sites

@thescientist: die echoes its argument before terminating the script.@Sami: After if (!$con) you have a die statement and an echo statement. The quotation marks in the echo statement are escaped correctly. They are escaped incorrectly in the die statement. This will cause a parsing error (blank screen), and it is probably your problem right now.You really should check the return values of all mysql statements.Always use isset() or empty() to test the value of $_POST elements before you try to use them. If a user leaves a field blank, a POST value might not exist. Trying to access it will throw a warning and might lead to other runtime errors.You mean: write up my writing mistakes. That's the kind of mistake I expect from native speakers, however. :)
I'm not sure if I misunderstood you.
<!doctype html><html><head><title>Post</title></head><body><?php$con = mysql_connect("localhost","gbok","******");if (!$con){echo '<div style='border:1px red;'>Beklager, databasefeil. Feilkode: ' . mysql_error() . '</div>';die();}else{echo "<div style=\"border:1px lime; displpay:block;\">Kommentaren din var vellykket lagret!</div>";}mysql_select_db("gbok", $con);mysql_query("INSERT INTO comments (name, comment, date)VALUES ($_POST['name'], $_POST['comment'], date('d.m.Y H:i'))");mysql_close($con);?><br><a href="./">Tilbake</a></body></html>

Link to comment
Share on other sites

There was nothing incorrect about your die statement. The problem is in the string. Look at your new echo statement:

echo '<div style='border...

PHP sees the first single quotation mark and wants to echo from there to the next single quotation mark. So it plans to echo this: '<div style=' . After that, it sees the word border, which is outside the quotation marks. PHP does not want to echo this word. It wants to treat it as a PHP statement. Because there is no border statement, PHP considers this word an error and terminates.If you want to use quotation marks inside your string, they must be escaped. Your second echo statement does this correctly. That is what the \ character does when it is placed in front of a quotation mark. It tells PHP not to treat it like a quotation mark, but to echo it like a normal character. Your first echo statement should use the escape character the way your second echo statement does.

Link to comment
Share on other sites

There was nothing incorrect about your die statement. The problem is in the string. Look at your new echo statement:
mysql_connect("hostname","username","password");

Because it didn't help adding the backslash.

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...