Simple GUESTBOOK problem

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
buknoii
Forum Newbie
Posts: 4
Joined: Fri Mar 14, 2008 5:49 am

Simple GUESTBOOK problem

Post 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.
Last edited by RobertGonzalez on Sun Mar 16, 2008 12:15 am, edited 3 times in total.
Reason: Tags note
timsewell
Forum Newbie
Posts: 21
Joined: Tue Feb 26, 2008 5:43 am
Location: Hove, England

Re: Simple GUESTBOOK problem

Post by timsewell »

You haven't assigned the $_POST data from your form to the 'frm_' variables you're trying to insert into the database.
User avatar
califdon
Jack of Zircons
Posts: 4484
Joined: Thu Nov 09, 2006 8:30 pm
Location: California, USA

Re: Simple GUESTBOOK problem

Post 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.
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Re: Simple GUESTBOOK problem

Post 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.
Post Reply