Jump to content

unique fields?


Love2c0de

Recommended Posts

Hello,

 

I am attempting a user login system, got all my DB setup and wrote the code. I am having some issues with the username and email fields not being unique (even though when I created my table I made them both unique).

 

Weird thing is, if I reload page and re-enter a username which I know is already entered, it re-enters it the firs time, but when I try to enter it again with a simple F5, it shows the error that it's already registered here.....

 

Here's my form processing code:

<?php

//array to deal with any processing errors, such as empty strings, invalid email etc.
$errors = array();

unset($_POST['reg_submit']);

//registration form submitted,
$_POST['username'] = trim($_POST['username']);
$_POST['password'] = trim($_POST['password']);
$_POST['email'] = trim($_POST['email']);

if($_POST['username'] == "")
{
   $errors[] = "You must enter a username.";
}

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

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

   if(!empty($errors))
   {
       $res = implode("<br />",$errors);
   }
   else
   {
       //we know all fields now have some sort of data for us to work with.

       //check the username field
       $user_len = strlen($_POST['username']);

       if($user_len < 8 || $user_len > 32)
       {
           $errors[] = "Your username must be more than 7 and less than 32 characters.";
       }

       if(!ctype_alnum($_POST['username']))
       {
           $errors[] = "Your username can only contain letters and digits.";
       }

       //check the password field
       $pass_len = strlen($_POST['password']);

       if($pass_len < 6 || $pass_len > 18)
       {
               $errors[] = "Your password must be more than 5 and less than 33 characters.";
       }

       //check the email field
       if(!filter_var($_POST['email'], FILTER_VALIDATE_EMAIL))
       {
           $errors[] = "You did not enter a valid email address.";
       }

       if(!empty($errors))
       {
           $res = implode("<br />",$errors);
       }
       else
       {
           require("db_queries/registration_insert.php");
       }
   }


if(isset($rows))
{
   if($rows == 1)
   {
       $res = "Registration Successful.";
   }
   else
   {
       $res = "Username and/or email address already in use.";
   }
}
?>

 

Here is the prepared insert:

<?php

$conn = new mysqli("localhost","root","","gaming") or die("Error: ");
$stmt = $conn->prepare("INSERT INTO users (username,password,email) VALUES(?,?,?)") or die("Error:".mysqli_error($conn));

$stmt->bind_param("sss",$_POST['username'],$_POST['password'],$_POST['email']);
$stmt->execute();
$stmt->store_result();

$rows = $stmt->affected_rows;

?>

 

I have also provided a couple of images. One of my db structure and one of the data which is entered.

 

Can anyone help?

 

Kind regards,

 

L2c.

post-128658-0-80096600-1357288494_thumb.png

post-128658-0-63439500-1357288503_thumb.png

Link to comment
Share on other sites

I didn't know how to deal with the error.f what i

 

It would seem that both the username and email are unique but I think I've done something wrong somewhere.

 

Here is an image of what it is showing me:

 

 

Kind regards,

 

L2c.

post-128658-0-48715700-1357305523_thumb.png

Edited by Love2c0de
Link to comment
Share on other sites

What you've done is created a UNIQUE constraint on two columns, not individually i.e. both the `username` and the `email` combined must be unique.

 

E.g. Valid entries

 

Username Email
cpd cpd@phpfreaks.com
cpd iam@phpfreaks.com

 

Invalid entries

 

Username Email
cpd cpd@phpfreaks.com
cpd cpd@phpfreaks.com

 

You need to edit the constraint and remove the email, then add a new unique constraint on the email column.

 

Edit: I don't know why there's soo much whitespace.... :shrug:

 

Edit: the nl2br (or similar) function was screwing up the post!!!!

Edited by CPD
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.