Page 1 of 1

Simple GUESTBOOK problem

Posted: Fri Mar 14, 2008 6:06 am
by buknoii
Everah | Please use [php], [code] and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read: Posting Code in the Forums to learn how to do it too.


greetings! I installed XAMPP on my computer and when i tried creating a php script to view the database it shows the result perfectly.

But when I tried creating a simple guestbook program it keeps showing the same error message "WRONG"

below are the codes of my program

SIGNUP.PHP

Code: Select all

<form method="post" action="signup_db.php">Name : <input type="text" name="frm_name"><br>E-mail : <input type="text" name="frm_email"><br>Tel No : <input type="text" name="frm_telno"><br>Message : <input type="text" name="frm_message"><br><input type="submit" name="frm_submit" value="POST GUESTBOOK">

SIGNUP_DB.PHP

Code: Select all

<?php
 
include("dbconnect.php");
 
 
if ($frm_submit=="POST GUESTBOOK")
 
{
 
    $query = " insert into gb_entry
        (gb_name,gb_email,gb_telno,gb_message) values
        ('".mysql_real_escape_string($frm_name)."','".
            mysql_real_escape_string($frm_email)."','".
            mysql_real_escape_string($frm_telno)."','".
            mysql_real_escape_string($frm_message)."')"
        ;
 
        mysql_query($query) or
        die(mysql_error());
 
?>
 
<h2>entry added</h2>
 
<?php
 
}
else
{
    echo "WRONG";
}
?>


dbconnect.php

Code: Select all

<?php
 
mysql_connect("127.0.0.1","root","november") or
    die("query=$query\nerror=".mysql_error());
 
mysql_select_db("guestbook") or
    die ("could not select database");
 
 
?>

any help would be appreciated

thanks!



Everah | Please use [php], [code] and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read: Posting Code in the Forums to learn how to do it too.

Re: Simple GUESTBOOK problem

Posted: Fri Mar 14, 2008 10:54 am
by timsewell
You haven't assigned the $_POST data from your form to the 'frm_' variables you're trying to insert into the database.

Re: Simple GUESTBOOK problem

Posted: Sat Mar 15, 2008 1:27 pm
by califdon
Here's how to analyze an error like this. Ask yourself "Why does the word WRONG appear?" Look at your code, in effect it says,

Code: Select all

if ($frm_submit=="POST GUESTBOOK") 
{
    // do some stuff
}
else
{
echo "WRONG";
}
Thus, your variable $frm_submit DID NOT EQUAL "POST GUESTBOOK". That's why it echoed "WRONG". So does that give you a clue? Where would $frm_submit obtain a value? You never assigned it a value.

You need code like this at the beginning of your SIGNUP_DB.PHP script:

Code: Select all

$name = $_POST['frm_name'];
$email = $_POST['frm_email'];
$telno = $_POST['frm_telno'];
$frm_submit = $_POST['frm_submit'];
But really, you don't need all the submit stuff, just check for any one of the form variables. You rarely need to be interested in the submit status. Only if it makes a difference whether the user had hit the Submit button or merely pressed Enter, I think.

Re: Simple GUESTBOOK problem

Posted: Sun Mar 16, 2008 12:18 am
by RobertGonzalez
Instead of checking the conditionals you are checking (the one califdon pointed out) try comparing with the request method...

Code: Select all

<?php
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
  // ...snip ...
}
?>
Then you don't need to worry about the text that is on the submit button or even if the button has been clicked.