Jump to content
Larry Ullman's Book Forums

Chapter 19 Review And Pursue - Create Register Page


Recommended Posts

I am having a bit of trouble with a register.php script I created for chapter 19's review and pursue. I created it so customers can register before shopping for prints. The problem is that after completing registration and all the fields are entered correctly an error message appears . The message is You could not be registered due to a system error, we apologize for the inconvenience. So it would seem that there mysqli_affected_rows doesn't equal 1. Why is what I can't figure out. A problem with my mysqli insert statement maybe??

 

Here is the code. 

 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>
 
<body>
<?php
# Script 19.21 register.php -
if ($_SERVER['REQUEST_METHOD'] == 'POST'){
include ('mysqli_connect44.php');
$errors = array();
if (empty($_POST['first_name'])){
$errors[] = 'You forgot to enter your first name';
} else {
$fn = mysqli_real_escape_string($dbc, trim($_POST['first_name']));
}
if (empty($_POST['last_name'])){
$errors[] = 'You forgot to enter your last name';
} else {
$ln = mysqli_real_escape_string($dbc, trim($_POST['last_name']));
}
if (!empty($_POST['email']) && filter_var($_POST['email'], FILTER_VALIDATE_EMAIL)){
$em = mysqli_real_escape_string($dbc, trim($_POST['email']));
} else {
$errors[] = 'Either you forgot to enter your email address, or the email you entered is not a valid email address.';
}
if (empty($_POST['address'])){
$errors[] = 'You forgot to enter your address';
} else {
$add = mysqli_real_escape_string($dbc, trim($_POST['address']));
}
if (empty($_POST['zipcode'])){
$errors[] = 'You forgot to enter your zip (postal) code';
} else {
$zip = mysqli_real_escape_string($dbc, trim($_POST['zipcode']));
}
if (empty($_POST['city'])){
$errors[] = 'You forgot to enter your city';
} else {
$city = mysqli_real_escape_string($dbc, trim($_POST['city']));
}
if (empty($_POST['state'])){
$errors[] = 'You forgot to enter your state';
} else {
$state = mysqli_real_escape_string($dbc, trim($_POST['state']));
}
if (empty($_POST['country'])){
$errors[] = 'You forgot to enter your country';
} else {
$coun = mysqli_real_escape_string($dbc, trim($_POST['country']));
}
if (empty($_POST['username'])){
$errors[] = 'You forgot to enter your username';
} else {
$user = mysqli_real_escape_string($dbc, trim($_POST['username']));
}
if (!empty($_POST['pass']) && ($_POST['pass'] != ($_POST['pass2']))){
$errors[] = 'Your passwords don\'t match, please reconfirm your password';
} else {
$pass = mysqli_real_escape_string($dbc, trim($_POST['pass']));
}
 
if (empty($errors)){
 
$q = "INSERT INTO customers (first_name, last_name, email, address, zipcode, city, state, country, username, pass) VALUES('$fn', '$ln', '$em', '$add', '$zip', '$city', '$state', '$coun', '$user', '$pass')";
$query = @mysqli_query($dbc, $q);
if (mysqli_affected_rows($dbc) == 1){
echo 'You are now registered, to shop for prints just <a href="login.php">Login</a> and begin shopping!';
} else {
echo 'You could not be registered due to a system error, we apologize for the inconvenience.';
}
} else {
echo 'The following errors occurred';
foreach($errors as $msg){
echo "- $msg<br />";
}
}
}
 
?>
<p><h1>Register to Shop our Prints Catalog</h1></p>
<form action="register.php" method="post">
<p><b>First Name:</b> <input type="text" name="first_name" size="20" maxlength="30" value="<?php if (isset($_POST['first_name'])) echo $_POST['first_name']; ?>" /></p>
<p><b>Last Name:</b> <input type="text" name="last_name" size="30" maxlength="40" value="<?php if (isset($_POST['last_name'])) echo $_POST['last_name']; ?>" /></p>
<p><b>Email Address:</b> <input type="text" name="email" size="40" maxlength="60" value="<?php if (isset($_POST['email'])) echo $_POST['email']; ?>" /></p>
<p><b>Address:</b> <input type="text" name="address" size="40" maxlength="70" value="<?php if (isset($_POST['address'])) echo $_POST['address']; ?>" /></p>
<p><b>Zip (Postal)Code:</b> <input type="text" name="zipcode" size="6" maxlength="7" value="<?php if (isset($_POST['zipcode'])) echo $_POST['zipcode']; ?>" /></p>
<p><b>City:</b> <input type="text" name="city" size="6" maxlength="6" value="<?php if (isset($_POST['city'])) echo $_POST['city']; ?>" /></p>
<p><b>State:</b> <input type="text" name="state" size="6" maxlength="6" value="<?php if (isset($_POST['state'])) echo $_POST['state']; ?>" /></p>
<p><b>Country:</b> <input type="text" name="country" size="15" maxlength="25" value="<?php if (isset($_POST['country'])) echo $_POST['country']; ?>" /></p>
<p><b>Username:</b> <input type="text" name="username" size="15" maxlength="25" value="<?php if (isset($_POST['username'])) echo $_POST['username']; ?>" /></p>
<p><b>Password:</b> <input type="password" name="pass" size="10" maxlength="20" value="<?php if (isset($_POST['pass'])) echo $_POST['pass']; ?>" /></p>
<p><b>Confirm Password:</b> <input type="password" name="pass2" size="10" maxlength="20" value="<?php if (isset($_POST['pass2'])) echo $_POST['pass2']; ?>" /></p>
<p><input type="submit" name="submit" value="Submit" /></p>
</form>
</body>

 

</html>

 

 

Link to comment
Share on other sites

 Share

×
×
  • Create New...