Page 1 of 3

Undefined variable: comments

Posted: Mon Apr 19, 2004 8:17 am
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.
} 
}
?>

Posted: Mon Apr 19, 2004 8:20 am
by JayBird
where is $comments set?

Mark

Posted: Mon Apr 19, 2004 8:27 am
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

not sure - don't think it is set.

Posted: Mon Apr 19, 2004 8:36 am
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>';
}

Posted: Mon Apr 19, 2004 8:37 am
by JayBird
yes, as long as the form method is POST

Mark

just checked register globals

Posted: Mon Apr 19, 2004 8:41 am
by mike08
my register globals are off is that correct?

Re: just checked register globals

Posted: Mon Apr 19, 2004 8:44 am
by JayBird
mike08 wrote:my register globals are off is that correct?
Yes, that is best!

Mark

now i have undefined index: comment and undefined variable

Posted: Mon Apr 19, 2004 8:58 am
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');
?>

Posted: Mon Apr 19, 2004 9:05 am
by JayBird
whats wrong with it?

undefined variable comments and undefined index comments

Posted: Mon Apr 19, 2004 9:22 am
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>

Posted: Mon Apr 19, 2004 9:24 am
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>

error messages have gone now

Posted: Mon Apr 19, 2004 9:25 am
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>';

bit confused how come there is two parts to it?

Posted: Mon Apr 19, 2004 9:29 am
by mike08
think im having a bad day. not quite with it.

should i just use the second part then?

Posted: Mon Apr 19, 2004 9:32 am
by magicrobotmonkey
print($_POST['comments']); and see what shows up!

still not quite right

Posted: Mon Apr 19, 2004 9:35 am
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