Jump to content

Undefined variable


j05hr

Recommended Posts

Hi,

 

Getting this error message but I know that it is defined just for some reason it won't pick it up.  Taken the url out so the site can't be accessed.

 

An error occurred in script 'example/register.php' on line 101: Undefined variable: message

Date/Time: 7-Feb-2011 18:32:10

 

<?php # Script 16.6 - register.php
// This is the registration page for the site.
require_once("includes/functions.php"); 
include("includes/header.php"); 
require_once("includes/connection.php"); 

require_once ('includes/config.inc.php');
$page_title = 'Register';
//include ('includes/header.html');

if (isset($_POST['submitted'])) { // Handle the form.

require_once ('mysqli_connect.php');

// Trim all the incoming data:
$trimmed = array_map('trim', $_POST);

// Assume invalid values:
$fn = $ln = $e = $p = FALSE;

// Check for a first name:
if (preg_match ('/^[A-Z \'.-]{2,20}$/i', $trimmed['first_name'])) {
	$fn = mysqli_real_escape_string ($dbc, $trimmed['first_name']);
} else {
	echo '<p class="error">Please enter your first name!</p>';
}

// Check for a last name:
if (preg_match ('/^[A-Z \'.-]{2,40}$/i', $trimmed['last_name'])) {
	$ln = mysqli_real_escape_string ($dbc, $trimmed['last_name']);
} else {
	echo '<p class="error">Please enter your last name!</p>';
}

// Check for an email address:
if (preg_match ('/^[\w.-]+@[\w.-]+\.[A-Za-z]{2,6}$/', $trimmed['email'])) {
	$e = mysqli_real_escape_string ($dbc, $trimmed['email']);
} else {
	echo '<p class="error">Please enter a valid email address!</p>';
}

// Check for a password and match against the confirmed password:
if (preg_match ('/^\w{4,20}$/', $trimmed['password1']) ) {
	if ($trimmed['password1'] == $trimmed['password2']) {
		$p = mysqli_real_escape_string ($dbc, $trimmed['password1']);
	} else {
		echo '<p class="error">Your password did not match the confirmed password!</p>';
	}
} else {
	echo '<p class="error">Please enter a valid password!</p>';
}

if ($fn && $ln && $e && $p) { // If everything's OK...

	// Make sure the email address is available:
	$q = "SELECT user_id FROM users WHERE email='$e'";
	$r = mysqli_query ($dbc, $q) or trigger_error("Query: $q\n<br />MySQL Error: " . mysqli_error($dbc));

	if (mysqli_num_rows($r) == 0) { // Available.

		// Create the activation code:
		$a = md5(uniqid(rand(), true));

		// Add the user to the database:
		$q = "INSERT INTO users (email, pass, first_name, last_name, active, registration_date) VALUES ('$e', SHA1('$p'), '$fn', '$ln', '$a', NOW() )";
		$r = mysqli_query ($dbc, $q) or trigger_error("Query: $q\n<br />MySQL Error: " . mysqli_error($dbc));

		if (mysqli_affected_rows($dbc) == 1) { // If it ran OK.

			// Send the email:
			$body = "Thank you for registering at. To activate your account, please click on this link:\n\n";
			$body .= BASE_URL . 'activate.php?x=' . urlencode($e) . "&y=$a";
			mail($trimmed['email'], 'Registration Confirmation', $body, 'From: ');

			// Finish the page:
			$message = '<h3>Thank you for registering! A confirmation email has been sent to your address. Please click on the link in that email in order to activate your account.</h3>';

		} else { // If it did not run OK.
			echo '<p class="error">You could not be registered due to a system error. We apologize for any inconvenience.</p>';
		}

	} else { // The email address is not available.
		echo '<p class="error">That email address has already been registered. If you have forgotten your password, use the link at right to have your password sent to you.</p>';
	}

} else { // If one of the data tests failed.
	echo '<p class="error">Please re-enter your passwords and try again.</p>';
}

mysqli_close($dbc);

} // End of the main Submit conditional.
?>
<div id="content">
        <div class="paddingContent">
            <h3> 
                Register
            </h3>
<br />
<form action="register.php" method="post" class="contact">
<?php echo $message; ?>

	<div class="registerForm">
            <label for="first_name" class="fixedwidth">First Name:</label>
            <input type="text" name="first_name" id="first_name" value="<?php if (isset($trimmed['first_name'])) echo $trimmed['first_name']; ?>"/>
        </div>
	  <br />
	<div class="registerForm">
            <label for="last_name" class="fixedwidth">Last Name:</label>
            <input type="text" name="last_name" id="last_name" value="<?php if (isset($trimmed['last_name'])) echo $trimmed['last_name']; ?>"/>
        </div>
	  <br />
	<div class="registerForm">
            <label for="email" class="fixedwidth">Email Address:</label>
            <input type="text" name="email" id="email"value="<?php if (isset($trimmed['email'])) echo $trimmed['email']; ?>" />
        </div>
	  <br />
	 <div class="registerForm">
            <label for="password1" class="fixedwidth">Password</label>
            <input type="password" name="password1" id="password1" value="" /> <small>Use only letters, numbers, and the underscore. Must be between 4 and 20 characters long.</small>
        </div>
	  <br />
	<div class="registerForm">
            <label for="password2" class="fixedwidth">Confirm Password:</label>
            <input type="password" name="password2" id="password"/>
        </div>	
	  <br />
<div class="submtForm"><input type="submit" name="submit" value="Register" /></div>
<input type="hidden" name="submitted" value="TRUE" />

</form>
        </div>
        </div>
<?php include("includes/footer.php"); ?>

<?php // Include the HTML footer.
//include ('includes/footer.html');
?>

Link to comment
Share on other sites

Getting this error message but I know that it is defined just for some reason it won't pick it up.

 

If it was set you wouldn't be getting that error message. The only place you set $message is within several IF conditions - four to be exact. If any one of those conditions fails $message will not be set.

 

Here are the four conditions:

if (isset($_POST['submitted'])) { // Handle the form.

if ($fn && $ln && $e && $p) { // If everything's OK...

if (mysqli_num_rows($r) == 0) { // Available.

if (mysqli_affected_rows($dbc) == 1) { // If it ran OK.

 

So, one of the above conditions is returning FALSE. If you just don't need a message if one of the above is false, then set a default value for message at the beginning of the script

$message = '';

Link to comment
Share on other sites

Yeah it is throwing a warning.  It was an echo and my understanding was that you can put a variable in front of it and then you can echo it.  Where would I put the

$message = '';

 

Can someone dumb it down a bit for me? 

 

Thanks,

Josh

Link to comment
Share on other sites

Just check to see if the variable is set before allowing it to be echoed.

 

That will work too. But in situations where I have a variable that is used for an error/response message and the conditions are such that there will not always be a "value" for that variable, I just set the variable to an empty string somewhere at the top of the page. In this case you could place

$message = '';

before the very first IF condition

if (isset($_POST['submitted'])) { // Handle the form

 

That way it is always set. Either solution works and both have their advantages and disadvantages based upon the logic of the page.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.