Forum Moderators: coopster
if (isset($_POST['username']) and ! empty($_POST['username'])) {
$exists=0;
// CLEANSE IT FIRST
// select id from table where username=$username
($exists) = mysql_fetchrow_array;
// So $exists will be > 0
if ($exists > 0) {
output_form("user $username already exists, please choose another user name");
}
// The previous should output and exit, no need for an "else"
// insert into database here
// output success message
}
else { output_form(null); }
function output_form($error=null) {
// there are better ways to do this - but populate
// post with empty values if they have not been set.
// If you have error reporting off, it will work without it
// BUT it will clog up your error log with every call
// to the script. It's just good practice to define
// variables even if they are not always used.
$vals = array ('username','pwd','yourname','email');
foreach ($vals as $val) {
$_POST[$val] = (isset($_POST[$val]))?$_POST[$val]:'';
}
$form = '
<form method="post" action="yourscript.php">
';
if ($error) {
$form .= '<p>There was an error in the data you
submitted: ' . $error . '</p><p>Please make the
appropriate corrections and try again.</p>';
}
else {
$form .= '<p>All fields are required. (or whatever....)</p>';
}
$form .= '
<p><label for="username">Username:</label>
<input type="text" name="username" id="username" value="' . $_POST['username'] . '"></p>
<p><label for="pwd">Password:</label>
<input type="password" name="pwd" id="pwd" value="' . $_POST['pwd'] . '"></p>
<p><label for="email">Email:</label>
<input type="text" name="email" id="email" value="' . $_POST['email'] . '"></p>
<p><label for="yourname">Your Name:</label>
<input type="text" name="yourname" id="yourname" value="' . $_POST['yourname'] . '"></p>
<p><input type="submit" value="Register"></p>
</form>
';
echo $form;
exit;
}
if (isset($_POST['username']) and ! empty($_POST['username'])) {
$exists=0;
// CLEANSE IT FIRST
// select id from table where username=$username
($exists) = mysql_fetchrow_array;
// So $exists will be > 0
if ($exists > 0) {
$title = 'User Name Exists';
$content = output_form("user $username already exists, please choose another user name");
}
else {
// insert into database here
$title = 'Thank You For Registering';
$content = '<p>Your success message or whatever</p>';
}
}
else {
$title = 'Register';
$content =output_form(null);
}
output_page($title,$content);
I am getting a lot of 'undefined' variable notices even though I am checking whether a variable is set or not using the isset() function... is there any way to avoid this?
if (isset($_POST[username])) {/*...*/} $errCount = 0;
//e-mails do not match
if ($mail_match != 0) {
$errCount++;
if ($errCount == 0) {
//Prepare query to add user to database
$query = "INSERT INTO users (username, password, email, age, hobbies) ... Conditionals with a true argument will always evaluate to true... so that 'if(true)' ONLY deals with the 'username' div, for example... the the next if(true) ONLY deals with 'password' div, etc.... you see my point?
if (isset($_POST['username']) and ! empty($_POST['username'])) {
$errors = check_input($required_fields);
if ($errors) { output_form($errors); }
// insert into database here
// output success message
}
else { output_form(null); }
function check_input($fields) {
$exists=0;
$errors=null;
// First check required fields
foreach ($fields as $formname => $english) {
if (! isset($_POST[$formname) or (isset($_POST[$formname]) and empty($_POST[$formname]))) {
$errors .= "<li>The $english field is required to register.</li>";
}
}
// This only checks if there are no errors to this point.
// If you try to check it with $username empty, it will give a
// misleading message.
if (! $errors) {
// CLEANSE IT FIRST
// select id from table where username=$username
($exists) = mysql_fetchrow_array;
// So $exists will be > 0
if ($exists > 0) {
$errors .= "<li>user " . $_POST['username'] . "already exists, please choose another user name</li>";
}
return $errors;
}
the page can display itself but the error messages are not working, can anyone possibly clarify what I have forgotten to do or what I have done incorrectly?
how does the following 'foreach' loop run?
is my logic correct?
how does the following code pick up on a usernames that already exist that is trying to be added to the database?
if (! $errors) {
// CLEANSE IT FIRST
// select id from table where username=$username
$query = "select id from table where username='" . $_POST['username'] . "'";
$result = mysql_query($query,$conn);
($exists) = mysql_fetchrow_array($result);
// So $exists will be > 0 IF it . . .exists
if ($exists > 0) {
$errors .= "<li>user " . $_POST['username'] . "already exists, please choose another user name</li>";
}
...and last question, what does "CLEANSE IT" mean?
function check_input($fields){
foreach($fields as $forname => $english){
if(!isset($_POST['$forname'])){
$errors . "<li>The $english field is required to register.</li>";
}
if(! $errors){
if($exists == 0){
$errors = $errors . "<li> user " . $_POST['userid'] . "already exists please choose a different username.</li>";
}
return $errors;
}
}
}
function check_input($fields){
foreach($fields as $forname => $english){
if(!isset($_POST['$forname'])){
$errors . "<li>The $english field is required to register.</li>";
}
}
if(! $errors){
if($exists == 0){
$errors = $errors . "<li> user " . $_POST['userid'] . "already exists please choose a different username.</li>";
}
}
return $errors;
}