Jump to content

User registration issue


mfaulkner

Recommended Posts

Hey guys, thanks so much for helping me with the dynamic drop down menu problem I was having a couple of weeks ago. Now I totally understand how to do it and have re-used the code more than once :)

 

I now have a new problem though, but I think its a straight forward one, if you know how:

 

The following code is a user registration page. Once the Submit button is clicked the page reloads itself and checks to make sure none of the form fields were empty and to ensure that the two passwords entered match each other.

 

The problem I'm having is how to do these checks (if statements) and then not execute the mysql query to insert the data if any of the checks fail. If none of them fail, then ok, execute query in order to register user.

 


<?php
require_once('./includes/dbconnect.php');

$error_firstname = "";
$error_surname = "";
$error_username = "";
$error_password = "";
$error_confirm_password = "";
$error_email = "";

$retrieve_current_users = mysql_query("SELECT user_name, user_email FROM users")
							or die(mysql_error());
$rows = mysql_fetch_array($retrieve_current_users);					
extract($rows);


if (isset($_POST['submit'])) {
if ($_POST['user_first_name'] == "") {
	$error_firstname = "* You must enter a first name.";
}

if ($_POST['user_surname'] == "") {
	$error_surname = "* You must enter a surname.";
}

if ($_POST['user_name'] == "") {
	$error_username = "* You must enter a user name.";
} elseif ($_POST['user_name'] == $user_name) {
	$error_username = "* That user name is already in use.";
}

if ($_POST['user_password'] == "") {
	$error_password = "* You must enter a password.";
}

if ($_POST['confirm_password'] == "") {
	$error_confirm_password = "* You must confirm your password.";
} elseif (($_POST['user_password']) != ($_POST['confirm_password'])) {
	$error_confirm_password = "* Your passwords do not match.";
	$error_password = "* Your passwords do not match.";
}

if ($_POST['user_email'] == "") {
	$error_email = "* You must enter an email address.";
}

//all checks pass --- insert info into database
$user_first_name = $_POST['user_first_name'];
$user_surname = $_POST['user_surname'];
$user_name = $_POST['user_name'];
$user_password = $_POST['user_password'];
$user_email = $_POST['user_email'];
$today = date("Y-m-d H:i:s");

$insert = "INSERT INTO users
	(user_first_name, user_surname, user_name, user_password, user_email, user_reg_date, user_group)
	VALUES
	('$user_first_name', '$user_surname', '$user_name', '$user_password', '$user_email', '$today', 'user')";

mysql_query($insert)
	or die(mysql_error());

header("location: ./registration_complete.php")
}
?>

<html>
<head>
<title>User Registration</title>
</head>
<body>

<form name="user_registration" action="<?php echo $_SERVER['PHP_SELF'] ?>" method="post">
<table border="0">
<tr>
	<td>First Name:</td>
	<td><input type="text" name="user_first_name" size="20" /></td>
	<td><?php echo $error_firstname; ?></td>
</tr>
<tr>
	<td>Surname:</td>
	<td><input type="text" name="user_surname" size="20" /></td>
	<td><?php echo $error_surname; ?></td>
</tr>
<tr>
	<td>User Name:</td>
	<td><input type="text" name="user_name" size="20" /></td>
	<td><?php echo $error_username; ?></td>
</tr>
<tr>
	<td>Password:</td>
	<td><input type="password" name="user_password" size="20" /></td>
	<td><?php echo $error_password; ?></td>
</tr>
<tr>
	<td>Confirm Password:</td>
	<td><input type="password" name="confirm_password" size="20" /></td>
	<td><?php echo $error_confirm_password; ?></td>
</tr>
<tr>
	<td>Email:</td>
	<td><input type="text" name="user_email" size="30" /></td>
	<td><?php echo $error_email; ?></td>
</tr>
<tr>
	<td></td>
	<td><input type="submit" name="submit" value="Register" /></td>
	<td></td>
</tr>
</form>

</body>
</html>

 

 

Thanks in advance guys

 

Mark

Link to comment
https://forums.phpfreaks.com/topic/37760-user-registration-issue/
Share on other sites

Using a variable in this instance can be quite helpful

e.g

if ($_POST['user_first_name'] == "") {
$error_firstname = "* You must enter a first name.";
$errors = "1";
}

Notice the $errors?

 

Then later on (before your sql) add:

if ($errors == "1")
{
$user_first_name = $_POST['user_first_name'];
$user_surname = $_POST['user_surname'];
$user_name = $_POST['user_name'];
$user_password = $_POST['user_password'];
$user_email = $_POST['user_email'];
$today = date("Y-m-d H:i:s");
$insert = "INSERT INTO users
(user_first_name, user_surname, user_name, user_password, user_email, user_reg_date, user_group)
VALUES
('$user_first_name', '$user_surname', '$user_name', '$user_password', '$user_email', '$today', 'user')";
mysql_query($insert)
or die(mysql_error());
header("location: ./registration_complete.php")
}
else
{
// if there is an error code here.
}

 

Thanks papaface, that worked a treat. One thing though... in your suggestion you indicate 

 

if ($errors == "1")

 

If each time an error was being found, $error was being set to "1", then I'd only want to the SQL to execute if $error was NOT "1"??

 

 

 

Mark.

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.