/    Sign up×
Community /Pin to ProfileBookmark

User registration issue

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.

[code=php]

<?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>

[/code]

Thanks in advance guys

Mark

to post a comment
PHP

2 Comments(s)

Copy linkTweet thisAlerts:
@apg88Feb 09.2007 — [code=php]<?php
require_once('./includes/dbconnect.php');
$error = false;
$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.";
$error = true;

}

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

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

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

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

if ($_POST['user_email'] == "") {
$error_email = "* You must enter an email address.";
$error = true;
}
if($error=false){
//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>[/code]

That should do it.

You were also missing a semicolon after the head() tags.
Copy linkTweet thisAlerts:
@mfaulknerauthorFeb 10.2007 — Cheers apg88, that did the job!

Couldn't spot the missing semicolon though? (obviously I mean I couldn't spot where it should go - just for the pedantic ones out there).

Regards,

Mark
×

Success!

Help @mfaulkner spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.19,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...