Undefined variable: comments

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

mike08
Forum Commoner
Posts: 57
Joined: Mon Apr 19, 2004 3:18 am

Undefined variable: comments

Post by mike08 »

trying to build a guestbook but keep getting this error - hope someone can have a look at it for me and tell me where im going wrong.

Code: Select all

//check a comment has been entered
if (strlen($comments) > 0) { 
	$comments = stripslashes($comments);
} else { // if there are no comments..
	$comments = NULL;
	echo '<p><font color="red" size"+1">You forgot to enter your comments!</b</p>';
}

//if everything was filled out ok
if ($username && $email && $comments) { // if everything's ok

//Add comments
$query = "INSERT INTO guestbook (username, email, comments) VALUES ('$username','$email','$comments')";
$result = @mysql_query ($query); // run the query.

if ($result) { // If it ran ok.
echo '<h3>Thank you for leaving a comments!</h3>';
include ('includes/footer.html'); // include the HTML footer.
exit();
} else { // If it did not run ok.
echo '<p><font color="red" size="+1">You could not leave a comment due to a system error.  We apologise for any inconvenience.</font></p>';
}
mysql_close(); // Close the database connection.
} 
}
?>
Last edited by mike08 on Mon Apr 19, 2004 8:21 am, edited 1 time in total.
User avatar
JayBird
Admin
Posts: 4524
Joined: Wed Aug 13, 2003 7:02 am
Location: York, UK
Contact:

Post by JayBird »

where is $comments set?

Mark
User avatar
JayBird
Admin
Posts: 4524
Joined: Wed Aug 13, 2003 7:02 am
Location: York, UK
Contact:

Post by JayBird »

does it give you a line number for the error?

What method are you using on your form? GET/POST?

Is regeister_global on or off in your php.ini?

If you are not sure about the last one, run the following code and post what it returns

Code: Select all

echo '<pre>'; 
echo 'PHP Version: '.phpversion()."\n"; 
echo 'Display Errors: '.(ini_get('display_errors') == '1' ? 'On' : 'Off')."\n"; 
echo 'Error Level: '.(ini_get('error_reporting') == '2047' ? 'E_ALL' : 'Not E_ALL')."\n"; 
echo 'Register Globals: '.(ini_get('register_globals') == '' ? 'Off' : 'On')."\n"; 
echo '</pre>';
Mark
mike08
Forum Commoner
Posts: 57
Joined: Mon Apr 19, 2004 3:18 am

not sure - don't think it is set.

Post by mike08 »

if i put

$comments = $_POST["comments"]; is this right to set comments if i place this above this

Code: Select all

//check a comment has been entered
if (strlen($comments) > 0) { 
	$comments = stripslashes($comments);
} else { // if there are no comments..
	$comments = NULL;
	echo '<p><font color="red" size"+1">You forgot to enter your comments!</b</p>';
}
User avatar
JayBird
Admin
Posts: 4524
Joined: Wed Aug 13, 2003 7:02 am
Location: York, UK
Contact:

Post by JayBird »

yes, as long as the form method is POST

Mark
mike08
Forum Commoner
Posts: 57
Joined: Mon Apr 19, 2004 3:18 am

just checked register globals

Post by mike08 »

my register globals are off is that correct?
User avatar
JayBird
Admin
Posts: 4524
Joined: Wed Aug 13, 2003 7:02 am
Location: York, UK
Contact:

Re: just checked register globals

Post by JayBird »

mike08 wrote:my register globals are off is that correct?
Yes, that is best!

Mark
mike08
Forum Commoner
Posts: 57
Joined: Mon Apr 19, 2004 3:18 am

now i have undefined index: comment and undefined variable

Post by mike08 »

code is below :(

Code: Select all

$comments = $_POST["comments"];
//check comment has been entered
if (strlen($comments) > 0) { 
	$comments = stripslashes($comments);
} else { // if there are no comments..
	$comments = NULL;
	echo '<p><font color="red" size"+1">You forgot to enter your comments!</b</p>';
}
//if everything was filled out ok
if ($username && $email && $comments) { // if everything's ok

//Add comments
$query = "INSERT INTO guestbook (username, email, comments) VALUES ('$username','$email','$comments')";
$result = @mysql_query ($query); // run the query.

if ($result) { // If it ran ok.

echo '<h3>Thank you for leaving a comments!</h3>';
include ('includes/footer.html'); // include the HTML footer.
exit();
} else { // If it did not run ok.
echo '<p><font color="red" size="+1">You could not leave a comment due to a system error.  We apologise for any inconvenience.</font></p>';
}

mysql_close(); // Close the database connection.
}
}
?>

<h1>Guestbook</h1>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
<fieldset>

<p><b>User Name:</b> <input type="text" name="username" size"10" maxlength="20" 
value="<?php if (isset($_POST['username'])) echo $_POST['username']; ?>" /></p>

<p><b>Email Address:</b> <input type="text" name="email" size"40" maxlength="40" 
value="<?php if (isset($_POST['email'])) echo $_POST['email']; ?>" /></p>

<p><b>Comments:<b> <textarea name="comments" rows="5" cols="30"></textarea
"<?php if (isset($_POST['comments'])) echo $_POST['comments']; ?>" > </p>
</fieldset>

<div align="center"><input type="submit" name="submit" value="Submit" /></div> 

</form><!-- End of form -->  
<?php // inlcude the HTML footer.
include ('includes/footer.html');
?>
User avatar
JayBird
Admin
Posts: 4524
Joined: Wed Aug 13, 2003 7:02 am
Location: York, UK
Contact:

Post by JayBird »

whats wrong with it?
mike08
Forum Commoner
Posts: 57
Joined: Mon Apr 19, 2004 3:18 am

undefined variable comments and undefined index comments

Post by mike08 »

i now get the two error messages in the subject line

is bit correct. in the form

Code: Select all

<p><b>Comments:<b> <textarea name="comments" rows="5" cols="30"></textarea 
"<?php if (isset($_POST['comments'])) echo $_POST['comments']; ?>" > </p> 
</fieldset>
magicrobotmonkey
Forum Regular
Posts: 888
Joined: Sun Mar 21, 2004 1:09 pm
Location: Cambridge, MA

Post by magicrobotmonkey »

Code: Select all

<textarea "<?php if (isset($_POST['comments'])) echo $_POST['comments']; ?>" >
should be

Code: Select all

"<?php if (isset($_POST['comments'])) echo $_POST['comments']; ?>" <textarea>
Last edited by magicrobotmonkey on Mon Apr 19, 2004 9:28 am, edited 1 time in total.
mike08
Forum Commoner
Posts: 57
Joined: Mon Apr 19, 2004 3:18 am

error messages have gone now

Post by mike08 »

it won't send to database.


keeps coming up with my error message -

Code: Select all

echo '<p><font color="red" size="+1">You could not leave a comment due to a system error.  We apologise for any inconvenience.</font></p>';
mike08
Forum Commoner
Posts: 57
Joined: Mon Apr 19, 2004 3:18 am

bit confused how come there is two parts to it?

Post by mike08 »

think im having a bad day. not quite with it.

should i just use the second part then?
magicrobotmonkey
Forum Regular
Posts: 888
Joined: Sun Mar 21, 2004 1:09 pm
Location: Cambridge, MA

Post by magicrobotmonkey »

print($_POST['comments']); and see what shows up!
mike08
Forum Commoner
Posts: 57
Joined: Mon Apr 19, 2004 3:18 am

still not quite right

Post by mike08 »

This is still not quite right as there is writing in the comments box before i even start?

this is what i have now.

Code: Select all

<p></p><b>Comments:<b> <textarea name="comments" rows="5" cols="30">
"<?php if (isset($_POST['comments'])) echo $_POST['comments']; ?>"<textarea></p> 
</fieldset>
sorry to keep bothering you
Post Reply