Jump to content

My form validation is giving errors lots of them


redrabbit

Recommended Posts

Ok I have been struggling with this form for a good while now and I cant seem to get it working I tried and tried and tried, struggled with displaying the error messages at the start cause I didnt know how to (total NOOB with PHP which really sucks) and so I asked for help on here, I have to say thanks to TeddyKiller for helping me.

 

Now after I inserted all that stuff and fixed all the errors I still am getting some new errors, the form is not "Sticky" (does not retain the entered data as it is supposed to) and I really doubt it is validating anything at all, can you please help me out before I go bald!!! Many thanks in advance,

 

The form displays but the error I get now is:

 

 

Notice: Undefined variable: msg_error in C:\wamp\www\AdminSys\registration.php on line 217

....

line 217:

<td class="error">Required: <?php $msg_error['title'] ?></td>

 

<?php

require_once('connectvars.php');


/**Checks if the form is submitted and processes only when submit = true**/
if (isset($_POST['submit'])) {

// Connects to the database
	$dbc = mysqli_connect('DB_HOST', 'DB_USER', 'DB_PASSWORD', 'DB_NAME')
	or die ('Error connecting to MYQSQL server.');


	/**Retrieves the data entered into the registration form
	and stores them in variables**/

	$title = $_POST['title'];
	$fname = mysqli_real_escape_string ($dbc, trim($_POST['firstname']));
	$lname = mysqli_real_escape_string ($dbc, trim($_POST['surname']));
	$country = $_POST['country'];
	$email1 = mysqli_real_escape_string ($dbc, trim($_POST['email1']));
	$email2 = mysqli_real_escape_string ($dbc, trim($_POST['email2']));
	$address = mysqli_real_escape_string ($dbc, trim($_POST['address'])) ;
	$postcode = mysqli_real_escape_string ($dbc, trim($_POST['postcode']));
	$phone = mysqli_real_escape_string ($dbc, trim($_POST['phone']));
	$cellphone = mysqli_real_escape_string ($dbc, trim($_POST['cellphone']));
	$pswd1 = $_POST['password1'];
	$pswd2 = $_POST['password2'];
	$error = false;
	$output_form = false;
	$msg_error = array();	



/**Validates the form to determine for any errors b4 storing to the db**/



	if (empty($title)) {
		$msg_error['title'] = "Please select your title";
		$error = true;
		$output_form = true;
	}

	if (empty($fname)) {
		$msg_error['fname'] = "Please enter your firstname";
		$error = true;
		$output_form = true;
	}

	if (empty($lname)) {
		$msg_error['lname'] = "Please enter your surname";
		$error = true;
		$output_form = true;
	}

	if (empty($email1)) {
		$msg_error['email1'] = "Please enter your email";
		$error = true;
		$output_form = true;
	}

	if (empty($email2)) {
		$msg_error['email2'] = "Please re-enter the same email";
		$error = true;
		$output_form = true;
	}

require_once('validate_email.php');
	if (!empty($email1) && !empty($email2)) {
		if ($email1 == $email2) { 
		$result = validate_email($email1);
			if ($result = false) {
				$msg_error['invalidemail'] = "you have entered an invalid email";
				$error = true;
				$output_form = true;
				}

		else {$msg_error['mismatchemail'] = "Your emails do not match";
				$error = true;
				$output_form = true;
			}
		}
	}


	if (empty($address1)) {
		$msg_error['address'] = "Please enter your postal address";
		$error = true;
		$output_form = true;
	}

	if (empty($postcode)) {
		$msg_error['postcode'] = "Please enter your post code (numbers only)";
		$error = true;
		$output_form = true;
	} 

	if (!is_numeric($postcode)) {
		$msg_error['intpost'] = "Phone number can only be numbers";
		$error = true;
		$output_form = true;
	}

	if (empty($phone)) {
		$msg_error['phone'] = "Please enter your phone number";
		$error = true;
		$output_form = true;
	}

	if (!is_numeric($phone)) {
		$msg_error['intphone'] = "Phone number can only be numbers";
		$error = true;
		$output_form = true;
	}


	if (!empty($cellphone)) {

		if (!is_numeric($cellphone)) {
			$msg_error ['intcellphone'] = "Cell phone can only be numbers";
			$error = true;
			$output_form = true;
		}
	}

	if (!empty($pswd1) && !empty($pswd2)) {
		if (pswd1 == pswd2) {
			$pswd = $pswd1;
		}
		else {$msg_error['mismatchpswd'] = "Your passwords do not match";
		$error = true;
		$output_form = true;
		}
	}
	if (!empty($pswd1) || !empty($pswd2)) {
		$msg_error['pswd'] = "Please enter a value for a password";
		$error = true;
		$output_form = true;
			}
	}

	/** if there are no errors then it creates a new user in the database 
		using the submitted details**/

	if ($error = false) {

							$create_user = "Insert into user (UserNr, Title, FirstName, LastName, Country, Email, PostalAddress, " .
											"PostalCode, Phone, Cellphone)" .
								"Values (0, $title, $fname, $lname, $country, $email1, $address, $postcode, " .
										" $phone, $cellphone)";

							$query_result = mysqli_query($dbc, $create_user)
											or die ('Error querying database');


							echo 'You account has been created please check your email';
						}



else {
		$output_form = true;

	}

	if($output_form) {


?>





<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
	<div id="form">

		<table>
			<tr>
				<th>Title:</th>
					<td> 
						<select name="title" value="<?php if (!empty($title))echo $title; ?>" >
							<option value="null"></option>
							<option value="Mr.">Mr.</option>
							<option value="Mrs.">Mrs.</option>
							<option value="Miss">Miss</option>
							<option value="Ms.">Ms.</option>
							<option value="Dr.">Dr.</option>
							<option value="Prof.">Prof.</option>
							<option value="Rev.">Rev.</option>
							<option value="Other">Other</option>
						</select>
					</td>
					<td class="error">Required: <?php $msg_error['title'] ?></td>
			</tr>

			<tr>
					   
				<th>First Name:</th>
					<td>
						<input type="text" name="firstname" class="medium" value= "<?php  if (!empty($fname)) echo $fname; ?>" />
					</td>	
					<td class="error">Required: <?php $msg_error['fname']; ?> </td>
			</tr>

				<tr>
					<th>Surname:</th>
						<td>
							<input type="text" name="surname" class="large" value="<?php  if (!empty($lname)) echo $lname; ?>" />
						</td>
						<td class="error"> Required: <?php $msg_error['lname']; ?> </td>
				</tr>

				<tr>
				<th> Country:</th>
					<td> 
						<select name="country" selected="South Africa">
							<option value="Afghanistan" >Afghanistan</option>
<option value="Albania" >Albania</option>
<option value="Algeria" >Algeria</option>
<option value="Andorra" >Andorra</option>
<option value="Antigua and Barbuda" >Antigua and Barbuda</option>
<option value="Argentina" >Argentina</option>
<option value="Iraq" >Iraq</option>
<option value="Japan" >Japan</option>
<option value="Jordan" >Jordan</option>
<option value="Lebanon" >Lebanon</option>
<option value="Somalia" >Somalia</option>
<option value="South Africa" selected="select" >South Africa</option>
<option value="Spain" >Spain</option>
<option value="Syria" >Syria</option>
<option value="Taiwan" >Taiwan</option>
						</select>
					</td>
			</tr>




			<tr>
				<th>Postal Address:</th>
					<td> 
						<input type="text" name="address" class="large" value="<?php  if (!empty($address)) echo $address; ?>"/>
					</td>
					<td class="error">Required: <?php $msg_error['address']; ?> </td>
			</tr>	



			<tr>
					<th> Postal Code: </th>
						<td> 
							<input type="text" name="postcode" value="<?php  if (!empty($postcode)) echo $postcode; ?>" />
						</td>
						<td class="error">Required: <?php $msg_error['postcode']; ?> </td>
			</tr>

			<tr>
					<th> Phone Number: </th>
						<td> 
							<input type="text" name="phone" value="<?php if (!empty($phone)) echo $phone; ?>"/>
						</td>
						<td class="error">Required: <?php $msg_error['phone']; ?> </td>

					<th> Cellphone:</th>
						<td> 
							<input type="text" name="cellphone" />
						</td>
						<td class="error"> <?php $msg_error['intcellphone']; ?> </td>
			</tr>	

			<tr>
					<th> Email: </th>
						<td> 
							<input type="text" name="email1" class="medium" value="<?php if (!empty($email1)) echo $email1; ?>" />
						</td>
						<td class="error">Required: <?php $msg_error['email1']; ?> </td>
			</tr>

			<tr>
					<th> Re-Enter Email:</th>
						<td> 
							<input type="text" name="email2" class="medium" value="<?php if (!empty($email2)) echo $email2; ?>" />
						</td>
						<td class="error">Required: <?php $msg_error['email2']; ?> </td>
			</tr>

			<tr>
					<th> Password:</th>
						<td> 
							<input type="password" name="password1" class="medium" />
						</td>
						<td class="error">Required: <?php $msg_error['pswd']; ?> </td>
			</tr>

			<tr>
					<th> Re-Enter Password:</th>
						<td> 
							<input type="password" name="password2" class="medium" />
						</td>
						<td class="error">Required: <?php $msg_error['pswd']; ?> <?php $msg_error['mismatchpswd'];  ?> </td>
			</tr>

<tr>
					<th> </th>
						<td> 
							<input type="submit" value="submit"/>
						</td>
			</tr>

		</table>
	</form>
	</div>


<?PHP
}

?>
<div class="boxed" id="footer">
			<p>
				This is the footer area
			</p>
		</div>

</div>
</body>

</html>

 

Link to comment
Share on other sites

ok ok this makes sense

 

and....it worked thank you soooooooooooooooo much

 

give me your paypal address Im gonna send you $5 for this I know $5 is nothing but if you consider the fact Im going to be giving out a lot of these then it will quickly add up.

 

Thank you sooooooooo much ken2k7

 

Now the second part of the problem why does it not validate?

 

If I leave something blank its supposed to show the error but its not? (thats another $5 :)

Link to comment
Share on other sites

ok even with echo still nothing shows and even when I fill everything correctly no user is created in the database

 

so something is wrong somewhere with my if statements (I always mess up with if statements especially such long ones as the one im working on, really appreciate your help man)

 

Thanks

Link to comment
Share on other sites

$create_user = "Insert into user (UserNr, Title, FirstName, LastName, Country, Email, PostalAddress, " .
                                    "PostalCode, Phone, Cellphone)" .
                           "Values (0, $title, $fname, $lname, $country, $email1, $address, $postcode, " .
                                 " $phone, $cellphone)";

 

You need to enclose each variable with single quotes.

 

$create_user = "Insert into user (UserNr, Title, FirstName, LastName, Country, Email, PostalAddress, " .
                                    "PostalCode, Phone, Cellphone)" .
                           "Values (0, '$title', '$fname', '$lname', '$country', '$email1', '$address', '$postcode', " .
                                 " '$phone', '$cellphone')";

 

You may want to escape $title and $country too. I see you did it for the others.

Link to comment
Share on other sites

I tried everything you suggested but to no avail, I really think its something to do with the braces of the

if statements anyways ken send me your paypal address via PM its the least I can do for you a little something for your time and effort.

Link to comment
Share on other sites

This should hopefully work:

 

<?php
require_once('connectvars.php');
require_once('validate_email.php');

$msg_error = array();	
$error = false;
$output_form = true;

/**Checks if the form is submitted and processes only when submit = true**/
if (isset($_POST['submit'])) {

	// Connects to the database
	$dbc = mysqli_connect('DB_HOST', 'DB_USER', 'DB_PASSWORD', 'DB_NAME')
	or die ('Error connecting to MYQSQL server.');


	/**Retrieves the data entered into the registration form
	and stores them in variables**/

	$title = $_POST['title'];
	$fname = trim($_POST['firstname']);
	$lname = trim($_POST['surname']);
	$country = $_POST['country'];
	$email1 = trim($_POST['email1']);
	$email2 = trim($_POST['email2']);
	$address = trim($_POST['address']) ;
	$postcode = trim($_POST['postcode']);
	$phone = trim($_POST['phone']);
	$cellphone = trim($_POST['cellphone']);
	$pswd1 = $_POST['password1'];
	$pswd2 = $_POST['password2'];
	$output_form = false;

/**Validates the form to determine for any errors b4 storing to the db**/



	if (empty($title)) {
		$msg_error['title'] = "Please select your title";
	}

	if (empty($fname)) {
		$msg_error['fname'] = "Please enter your firstname";
	}

	if (empty($lname)) {
		$msg_error['lname'] = "Please enter your surname";
	}

	if (empty($email1)) {
		$msg_error['email1'] = "Please enter your email";
	}

	if (empty($email2)) {
		$msg_error['email2'] = "Please re-enter the same email";
	}

	if (empty($address1)) {
		$msg_error['address'] = "Please enter your postal address";
	}

	if (empty($postcode) || !is_numeric($postcode)) {
		$msg_error['postcode'] = "Post code cannot be empty and it must be numbers only.";
	} 

	if (empty($phone) || !is_numeric($phone)) {
		$msg_error['phone'] = "Phone number cannot be empty and it must be numbers only.";
	}

	if (!empty($cellphone)) {
		if (!is_numeric($cellphone)) {
			$msg_error ['intcellphone'] = "Cell phone can only be numbers";
		}
	}

	if (!empty($email1) && !empty($email2)) {
		if ($email1 == $email2) { 
			if ($result = validate_email($email1)) {
				$msg_error['invalidemail'] = "You have entered an invalid email";
			}
			else {
				$msg_error['mismatchemail'] = "Your emails do not match";
			}
		}
	}

	if (empty($pswd1) || empty($pswd2)) {
		$msg_error['pswd'] = "Please enter a value for a password";
	}
	else {
		if ($pswd1 == $pswd2) {
			$pswd = $pswd1;
		}
		else {
			$msg_error['mismatchpswd'] = "Your passwords do not match";
		}
	}

	if (!empty($msg_error)) {
		$error = true;
		$output_form = true;
	}

	/** if there are no errors then it creates a new user in the database 
		using the submitted details**/
	if (empty($msg_error)) {
		$create_user = sprintf("INSERT INTO user (UserNr, Title, FirstName, LastName, Country, Email, PostalAddress, PostalCode, Phone, Cellphone) VALUES (0, '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s');",
			mysqli_real_escape_string($dbc, $title),
			mysqli_real_escape_string($dbc, $fname),
			mysqli_real_escape_string($dbc, $lname),
			mysqli_real_escape_string($dbc, $country),
			mysqli_real_escape_string($dbc, $email1),
			mysqli_real_escape_string($dbc, $address),
			mysqli_real_escape_string($dbc, $postcode,
			mysqli_real_escape_string($dbc, $phone),
			mysqli_real_escape_string($dbc, $cellphone),
		);

		$query_result = mysqli_query($dbc, $create_user) or trigger_error('Error querying database');

		if ($query_result)
			echo 'You account has been created please check your email';
		else echo 'Your information cannot be saved. Please try again later.';

		$output_form = false;
	}
}

if($output_form) {
?>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
	<div id="form">

		<table>
			<tr>
				<th>Title:</th>
					<td> 
						<select name="title" value="<?php if (!empty($title)) echo $title; ?>" >
							<option value="null"></option>
							<option value="Mr.">Mr.</option>
							<option value="Mrs.">Mrs.</option>
							<option value="Miss">Miss</option>
							<option value="Ms.">Ms.</option>
							<option value="Dr.">Dr.</option>
							<option value="Prof.">Prof.</option>
							<option value="Rev.">Rev.</option>
							<option value="Other">Other</option>
						</select>
					</td>
					<td class="error">Required: <?php if (isset($msg_error['title'])) echo $msg_error['title']; ?></td>
			</tr>

			<tr>
				<th>First Name:</th>
					<td>
						<input type="text" name="firstname" class="medium" value= "<?php  if (!empty($fname)) echo $fname; ?>" />
					</td>	
					<td class="error">Required: <?php if (isset($msg_error['fname'])) echo $msg_error['fname']; ?> </td>
			</tr>

				<tr>
					<th>Surname:</th>
						<td>
							<input type="text" name="surname" class="large" value="<?php  if (!empty($lname)) echo $lname; ?>" />
						</td>
						<td class="error"> Required: <?php if (isset($msg_error['lname'])) echo $msg_error['lname']; ?> </td>
				</tr>

				<tr>
				<th> Country:</th>
					<td> 
						<select name="country" selected="South Africa">
							<option value="Afghanistan" >Afghanistan</option>
<option value="Albania" >Albania</option>
<option value="Algeria" >Algeria</option>
<option value="Andorra" >Andorra</option>
<option value="Antigua and Barbuda" >Antigua and Barbuda</option>
<option value="Argentina" >Argentina</option>
<option value="Iraq" >Iraq</option>
<option value="Japan" >Japan</option>
<option value="Jordan" >Jordan</option>
<option value="Lebanon" >Lebanon</option>
<option value="Somalia" >Somalia</option>
<option value="South Africa" selected="select" >South Africa</option>
<option value="Spain" >Spain</option>
<option value="Syria" >Syria</option>
<option value="Taiwan" >Taiwan</option>
						</select>
					</td>
			</tr>




			<tr>
				<th>Postal Address:</th>
					<td> 
						<input type="text" name="address" class="large" value="<?php  if (!empty($address)) echo $address; ?>"/>
					</td>
					<td class="error">Required: <?php if (isset($msg_error['address'])) echo $msg_error['address']; ?> </td>
			</tr>	



			<tr>
					<th> Postal Code: </th>
						<td> 
							<input type="text" name="postcode" value="<?php  if (!empty($postcode)) echo $postcode; ?>" />
						</td>
						<td class="error">Required: <?php if (isset($msg_error['postcode'])) echo $msg_error['postcode']; ?> </td>
			</tr>

			<tr>
					<th> Phone Number: </th>
						<td> 
							<input type="text" name="phone" value="<?php if (!empty($phone)) echo $phone; ?>"/>
						</td>
						<td class="error">Required: <?php if (isset($msg_error['phone'])) echo $msg_error['phone']; ?> </td>

					<th> Cellphone:</th>
						<td> 
							<input type="text" name="cellphone" />
						</td>
						<td class="error"> <?php if (isset($msg_error['cellphone'])) echo $msg_error['cellphone']; ?> </td>
			</tr>	

			<tr>
					<th> Email: </th>
						<td> 
							<input type="text" name="email1" class="medium" value="<?php if (!empty($email1)) echo $email1; ?>" />
						</td>
						<td class="error">Required: <?php if (isset($msg_error['email1'])) echo $msg_error['email1']; ?> </td>
			</tr>

			<tr>
					<th> Re-Enter Email:</th>
						<td> 
							<input type="text" name="email2" class="medium" value="<?php if (!empty($email2)) echo $email2; ?>" />
						</td>
						<td class="error">Required: <?php if (isset($msg_error['email2'])) echo $msg_error['email2']; ?> </td>
			</tr>

			<tr>
					<th> Password:</th>
						<td> 
							<input type="password" name="password1" class="medium" />
						</td>
						<td class="error">Required: <?php if (isset($msg_error['pswd'])) echo $msg_error['pswd']; ?> </td>
			</tr>

			<tr>
					<th> Re-Enter Password:</th>
						<td> 
							<input type="password" name="password2" class="medium" />
						</td>
						<td class="error">Required: <?php if (isset($msg_error['pswd'])) echo $msg_error['pswd']; ?>  <?php if (isset($msg_error['mismatchpswd'])) echo $msg_error['mismatchpswd']; ?> </td>
			</tr>
			<tr>
					<th> </th>
						<td> 
							<input type="submit" value="submit"/>
						</td>
			</tr>
		</table>
	</form>
	</div>


<?php
}
?>
<div class="boxed" id="footer">
			<p>
				This is the footer area
			</p>
		</div>
</div>
</body>
</html>

Link to comment
Share on other sites

I cant believe you went through the whole thing and formatted it, I tried it still gives a parse error I would fix it but I dont know how especially now: this is the line with the error

 

if (empty($msg_error)) {

        $create_user = sprintf("INSERT INTO user (UserNr, Title, FirstName, LastName, Country, Email, PostalAddress, PostalCode, Phone, Cellphone) VALUES (0, '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s');",

            mysqli_real_escape_string($dbc, $title),

            mysqli_real_escape_string($dbc, $fname),

            mysqli_real_escape_string($dbc, $lname),

            mysqli_real_escape_string($dbc, $country),

            mysqli_real_escape_string($dbc, $email1),

            mysqli_real_escape_string($dbc, $address),

            mysqli_real_escape_string($dbc, $postcode),

            mysqli_real_escape_string($dbc, $phone),

            mysqli_real_escape_string($dbc, $cellphone),

117        ); <-----------------------------------------------------------------------says parse error in C:\wamp\www\AdminSys\regnew.php on line 117

 

 

Also since you changed it all can you also include this query into the database

 

$create_acct = "Insert into account (AcctNr, AcctTypeID, Username, Password, UserNr," .

"DateCreated" .

"Values (0, 1, '$email1', SHA('$pswd'), 0, TIMESTAMP" ;

 

 

And last but not least please send me your paypal address, gonna see what I can do for you

Link to comment
Share on other sites

Oh there is a comma at the last mysqli_real_escape_string. Just remove it. Here:

<?php
require_once('connectvars.php');
require_once('validate_email.php');

$msg_error = array();	
$error = false;
$output_form = true;

/**Checks if the form is submitted and processes only when submit = true**/
if (isset($_POST['submit'])) {

	// Connects to the database
	$dbc = mysqli_connect('DB_HOST', 'DB_USER', 'DB_PASSWORD', 'DB_NAME')
	or die ('Error connecting to MYQSQL server.');


	/**Retrieves the data entered into the registration form
	and stores them in variables**/

	$title = $_POST['title'];
	$fname = trim($_POST['firstname']);
	$lname = trim($_POST['surname']);
	$country = $_POST['country'];
	$email1 = trim($_POST['email1']);
	$email2 = $dbc, trim($_POST['email2']);
	$address = trim($_POST['address']) ;
	$postcode = trim($_POST['postcode']);
	$phone = trim($_POST['phone']);
	$cellphone = trim($_POST['cellphone']);
	$pswd1 = $_POST['password1'];
	$pswd2 = $_POST['password2'];
	$output_form = false;

/**Validates the form to determine for any errors b4 storing to the db**/



	if (empty($title)) {
		$msg_error['title'] = "Please select your title";
	}

	if (empty($fname)) {
		$msg_error['fname'] = "Please enter your firstname";
	}

	if (empty($lname)) {
		$msg_error['lname'] = "Please enter your surname";
	}

	if (empty($email1)) {
		$msg_error['email1'] = "Please enter your email";
	}

	if (empty($email2)) {
		$msg_error['email2'] = "Please re-enter the same email";
	}

	if (empty($address1)) {
		$msg_error['address'] = "Please enter your postal address";
	}

	if (empty($postcode) || !is_numeric($postcode)) {
		$msg_error['postcode'] = "Post code cannot be empty and it must be numbers only.";
	} 

	if (empty($phone) || !is_numeric($phone)) {
		$msg_error['phone'] = "Phone number cannot be empty and it must be numbers only.";
	}

	if (!empty($cellphone)) {
		if (!is_numeric($cellphone)) {
			$msg_error ['intcellphone'] = "Cell phone can only be numbers";
		}
	}

	if (!empty($email1) && !empty($email2)) {
		if ($email1 == $email2) { 
			if ($result = validate_email($email1)) {
				$msg_error['invalidemail'] = "You have entered an invalid email";
			}
			else {
				$msg_error['mismatchemail'] = "Your emails do not match";
			}
		}
	}

	if (empty($pswd1) || empty($pswd2)) {
		$msg_error['pswd'] = "Please enter a value for a password";
	}
	else {
		if ($pswd1 == $pswd2) {
			$pswd = $pswd1;
		}
		else {
			$msg_error['mismatchpswd'] = "Your passwords do not match";
		}
	}

	if (!empty($msg_error)) {
		$error = true;
		$output_form = true;
	}

	/** if there are no errors then it creates a new user in the database 
		using the submitted details**/
	if (empty($msg_error)) {
		$create_user = sprintf("INSERT INTO user (UserNr, Title, FirstName, LastName, Country, Email, PostalAddress, PostalCode, Phone, Cellphone) VALUES (0, '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s');",
			mysqli_real_escape_string($dbc, $title),
			mysqli_real_escape_string($dbc, $fname),
			mysqli_real_escape_string($dbc, $lname),
			mysqli_real_escape_string($dbc, $country),
			mysqli_real_escape_string($dbc, $email1),
			mysqli_real_escape_string($dbc, $address),
			mysqli_real_escape_string($dbc, $postcode,
			mysqli_real_escape_string($dbc, $phone),
			mysqli_real_escape_string($dbc, $cellphone)
		);

		$cu_result = mysqli_query($dbc, $create_user) or trigger_error('Error saving user profile');

		$create_acct = sprintf("INSERT INTO account (AcctNr, AcctTypeID, Username, Password, UserNr, DateCreated) VALUES (0, 1, '%s', SHA1('%s'), 0, CURRENT_TIMESTAMP());",
			mysqli_real_escape_string($dbc, $email1),
			mysqli_real_escape_string($dbc, $pswd)
		);

		$ca_result = mysqli_query($dbc, $create_user) or trigger_error('Error  saving account profile');

		if ($cu_result && $ca_result)
			echo 'You account has been created please check your email';
		else echo 'An error occured and your information cannot be saved. Please try again later.';

		$output_form = false;
	}
}

if($output_form) {
?>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
	<div id="form">

		<table>
			<tr>
				<th>Title:</th>
					<td> 
						<select name="title" value="<?php if (!empty($title)) echo $title; ?>" >
							<option value="null"></option>
							<option value="Mr.">Mr.</option>
							<option value="Mrs.">Mrs.</option>
							<option value="Miss">Miss</option>
							<option value="Ms.">Ms.</option>
							<option value="Dr.">Dr.</option>
							<option value="Prof.">Prof.</option>
							<option value="Rev.">Rev.</option>
							<option value="Other">Other</option>
						</select>
					</td>
					<td class="error">Required: <?php if (isset($msg_error['title'])) echo $msg_error['title']; ?></td>
			</tr>

			<tr>
				<th>First Name:</th>
					<td>
						<input type="text" name="firstname" class="medium" value= "<?php  if (!empty($fname)) echo $fname; ?>" />
					</td>	
					<td class="error">Required: <?php if (isset($msg_error['fname'])) echo $msg_error['fname']; ?> </td>
			</tr>

				<tr>
					<th>Surname:</th>
						<td>
							<input type="text" name="surname" class="large" value="<?php  if (!empty($lname)) echo $lname; ?>" />
						</td>
						<td class="error"> Required: <?php if (isset($msg_error['lname'])) echo $msg_error['lname']; ?> </td>
				</tr>

				<tr>
				<th> Country:</th>
					<td> 
						<select name="country" selected="South Africa">
							<option value="Afghanistan" >Afghanistan</option>
<option value="Albania" >Albania</option>
<option value="Algeria" >Algeria</option>
<option value="Andorra" >Andorra</option>
<option value="Antigua and Barbuda" >Antigua and Barbuda</option>
<option value="Argentina" >Argentina</option>
<option value="Iraq" >Iraq</option>
<option value="Japan" >Japan</option>
<option value="Jordan" >Jordan</option>
<option value="Lebanon" >Lebanon</option>
<option value="Somalia" >Somalia</option>
<option value="South Africa" selected="select" >South Africa</option>
<option value="Spain" >Spain</option>
<option value="Syria" >Syria</option>
<option value="Taiwan" >Taiwan</option>
						</select>
					</td>
			</tr>




			<tr>
				<th>Postal Address:</th>
					<td> 
						<input type="text" name="address" class="large" value="<?php  if (!empty($address)) echo $address; ?>"/>
					</td>
					<td class="error">Required: <?php if (isset($msg_error['address'])) echo $msg_error['address']; ?> </td>
			</tr>	



			<tr>
					<th> Postal Code: </th>
						<td> 
							<input type="text" name="postcode" value="<?php  if (!empty($postcode)) echo $postcode; ?>" />
						</td>
						<td class="error">Required: <?php if (isset($msg_error['postcode'])) echo $msg_error['postcode']; ?> </td>
			</tr>

			<tr>
					<th> Phone Number: </th>
						<td> 
							<input type="text" name="phone" value="<?php if (!empty($phone)) echo $phone; ?>"/>
						</td>
						<td class="error">Required: <?php if (isset($msg_error['phone'])) echo $msg_error['phone']; ?> </td>

					<th> Cellphone:</th>
						<td> 
							<input type="text" name="cellphone" />
						</td>
						<td class="error"> <?php if (isset($msg_error['cellphone'])) echo $msg_error['cellphone']; ?> </td>
			</tr>	

			<tr>
					<th> Email: </th>
						<td> 
							<input type="text" name="email1" class="medium" value="<?php if (!empty($email1)) echo $email1; ?>" />
						</td>
						<td class="error">Required: <?php if (isset($msg_error['email1'])) echo $msg_error['email1']; ?> </td>
			</tr>

			<tr>
					<th> Re-Enter Email:</th>
						<td> 
							<input type="text" name="email2" class="medium" value="<?php if (!empty($email2)) echo $email2; ?>" />
						</td>
						<td class="error">Required: <?php if (isset($msg_error['email2'])) echo $msg_error['email2']; ?> </td>
			</tr>

			<tr>
					<th> Password:</th>
						<td> 
							<input type="password" name="password1" class="medium" />
						</td>
						<td class="error">Required: <?php if (isset($msg_error['pswd'])) echo $msg_error['pswd']; ?> </td>
			</tr>

			<tr>
					<th> Re-Enter Password:</th>
						<td> 
							<input type="password" name="password2" class="medium" />
						</td>
						<td class="error">Required: <?php if (isset($msg_error['pswd'])) echo $msg_error['pswd']; ?>  <?php if (isset($msg_error['mismatchpswd'])) echo $msg_error['mismatchpswd']; ?> </td>
			</tr>
			<tr>
					<th> </th>
						<td> 
							<input type="submit" value="submit"/>
						</td>
			</tr>
		</table>
	</form>
	</div>


<?php
}
?>
<div class="boxed" id="footer">
			<p>
				This is the footer area
			</p>
		</div>
</div>
</body>
</html>

 

As for the generous money, please send it as a donation phpfreaks to keep this site running.

 

Go here: http://www.phpfreaks.com/page/donations

Link to comment
Share on other sites

Thank you so much for all your help the donation I made doesnt even come close to what you have done for me and I am definitely going to make PHP freaks as part of my php learning can you just explain to me what does the sprintf function do?

Link to comment
Share on other sites

sprintf.

 

It's used to format a string. I personally like to keep variables separate from strings. I know people do this:

$str = "here's some static text and here's a $variable";

 

I know of no other programming language that allows that other than PHP. And I can see why.

 

I just don't like it. It looks tacky and you run into all sorts of variable interpolation issues, especially with SQL queries. Keeping them separate and clean looking allows you to look at the code easier.

Link to comment
Share on other sites

ok great stuff the form works Im sure it does but its getting set back by the validate_email function (seems like I cant get anything right  :-[)

 

If this registration bit works the rest will be easy, its just this part has me stuck

 

<?php
function validate_email($email) {
(!preg_match('/^[a-zA-Z0-9][a-zA-Z0-9\._\-&!?=#]*@/', $email)) {              <------ this is the problem line apparantly
      // $email is invalid because LocalName is bad
            $output_form = 'yes';
    }
    else {
      // Strip out everything but the domain from the email
      $domain = preg_replace('/^[a-zA-Z0-9][a-zA-Z0-9\._\-&!?=#]*@/', '', $email);
      // Now check if $domain is registered
      if (!checkdnsrr($domain)) {
         echo '<p class="error">Your email address is invalid.</p>';
         $output_form = 'yes';
      }
     }
        return false;
      }
  return true;
?>

Link to comment
Share on other sites

AND....I ran it but it still didnt work, I think I have had enuff for one day Im sure you also have, so lets leave it at this Im not going to close it as solved because Im sure someone who lands here searching for it is not going to be happy that it doesn't work, lets hope someone else looks at it and can figure out the mistake.

 

I just want to ask is this the ideal way to validate with PHP using if statements?

 

Is there no better way, I came up with this code after looking at several samples online and from a book I have, I guess I should just move on, Il ask someone tomorrow to have a look at it for me.

 

Il try it on an actual server instead of a local machine maybe just maybe something to do with that too?

 

Anyways Thanks a lot Ken you have been a great source of help and information. Have a great day!

Link to comment
Share on other sites

AND....I ran it but it still didnt work, I think I have had enuff for one day Im sure you also have, so lets leave it at this Im not going to close it as solved because Im sure someone who lands here searching for it is not going to be happy that it doesn't work, lets hope someone else looks at it and can figure out the mistake.

 

I just want to ask is this the ideal way to validate with PHP using if statements?

 

Is there no better way, I came up with this code after looking at several samples online and from a book I have, I guess I should just move on, Il ask someone tomorrow to have a look at it for me.

 

Il try it on an actual server instead of a local machine maybe just maybe something to do with that too?

 

Anyways Thanks a lot Ken you have been a great source of help and information. Have a great day!

 

Nah, I'm fine. Replace your function with this one. It was posted by mjdamato and it's one of the best for validating an e-mail address.

function validate_email($email) 
{
    $formatTest = '/^[\w!#$%&\'*+\-\/=?^`{|}~]+(\.[\w!#$%&\'*+\-\/=?^`{|}~]+)*@[a-z\d]([a-z\d-]{0,62}[a-z\d])?(\.[a-z\d]([a-z\d-]{0,62}[a-z\d])?)*\.[a-z]{2,6}$/i';
    $lengthTest = '/^(.{1,64})@(.{4,255})$/';
    return (preg_match($formatTest, $email) && preg_match($lengthTest, $email));
}

Link to comment
Share on other sites

Sorry the function bit worked but the form it still does what it was doing before, after filling it in it just refreshes and returns an empty form, does not save to data base and no error messages, I remember when I started coding it, without the validation parts it would save the data to the database no problem ya but this form is getting annoying I have to figure out something and at the moment I think I have had enough gonna take a rest and work on it with a clear head tomoro.

 

And like I said thanks a lot for everything you did.  :)

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.