Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Page 1 of 2 12 LastLast
Results 1 to 15 of 22
  1. #1
    New Coder
    Join Date
    Mar 2012
    Posts
    52
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Prevent same username registered.

    PHP Code:
    <?php

    if(isset($_POST["submitbtn"]))
    {
        
    $customername $_POST["usernametxt"];
        
        
    /*---------------------------------------*/
        
            
    $password1=0;
            
    $password2=0;
            
    $password3=0;
            
    $password4=0;
            
    $password5=0;
            
            
    $email $_POST["email"];
            
    $password1=$_POST["pass1"];
            
    $password2=$_POST["pass2"];
            
    $password3=$_POST["pass3"];
            
    $password4=$_POST["pass4"];
            
    $password5=$_POST["pass5"];
            
            if(
    $password1=="" ||$password2=="" ||$password3=="" ||$password4=="" ||$password5=="")
            {
                
    ?>        
            <script type="text/javascript">
            
            alert("Please select 5 catergories of photo!");
            
            </script>
                
    <?php            
            
    }
            
            else if    (
    $email==""||$customername=="" )
            {
                
    ?>        
            <script type="text/javascript">
            
            alert("Please fill in all needed the requirements!");
            
            </script>
    <?php    
            
            
    }
            
            else
        
            {    
            
    mysql_query("insert into customerdetail(customer_id,customer_pass1,customer_pass2,customer_pass3,customer_pass4,customer_pass5,customer_email) values ('$customername','$password1','$password2','$password3','$password4','$password5','$email')");

    ?>

        <script type="text/javascript">
        
        alert("Your have successfully registered!");
        location = "welcome.php";
        </script>

    <?php
            
    }
        

    }

    ?>
    Can anyone give some idea how do i do that ? Thank you so much for giving suggestion and guide. Appreciate it a lots

  • #2
    Master Coder
    Join Date
    Jun 2003
    Location
    Cottage Grove, Minnesota
    Posts
    9,472
    Thanks
    8
    Thanked 1,085 Times in 1,076 Posts
    Why are there 5 passwords, and is there any encryption?
    Something seems amiss.

    There is probably an explanation needed about what your script does,
    and what the goal of the script actual is supposed to do.

  • #3
    Senior Coder Dormilich's Avatar
    Join Date
    Jan 2010
    Location
    Behind the Wall
    Posts
    3,253
    Thanks
    12
    Thanked 341 Times in 337 Posts
    Prevent same username registered.
    easiest and most failsafe method is making the username column either the Primary Key or UNIQUE.
    The computer is always right. The computer is always right. The computer is always right. Take it from someone who has programmed for over ten years: not once has the computational mechanism of the machine malfunctioned.
    André Behrens, NY Times Software Developer

  • #4
    Regular Coder LearningCoder's Avatar
    Join Date
    Jan 2011
    Location
    The Pleiades
    Posts
    922
    Thanks
    76
    Thanked 29 Times in 29 Posts
    Maybe run a select query, checking the database for the username and email they entered. Create an if else statement checking the return value of the query. If it returns FALSE, the username is ok to use, so carry on with the insert. If it returns a result set, you know that either the username or email address has already been registered, because it was matched in your table, so send them back to the form where you print an error?

    Not sure if this is a good way but this is how I checked before when I was trying the user login system. It worked ok for me. There are bound to be plenty of better ways though I'm sure.

    Regards,

    LC.

  • #5
    Senior Coder
    Join Date
    Feb 2011
    Location
    Your Monitor
    Posts
    4,316
    Thanks
    58
    Thanked 525 Times in 512 Posts
    Blog Entries
    5
    Quote Originally Posted by Dormilich View Post
    easiest and most failsafe method is making the username column either the Primary Key or UNIQUE.
    I'm also going with this too

    @LearningCoder, while selecting will work it's an extra query to run on the sql server. IF you get too many of those it will grind to a halt.

    If you set the column to unique and try to insert an existing user, the sql server won't insert it. You can then test whether or not the user was inserted by using mysql_num_rows() which will tell you how many rows were inserted - of course if there were none due to the username already existing then it will be 0. This doesn't (afaik) run a second query on the server because mysql automatically counts the affected rows and returns it with each query and holds it in memory meaning that you don't need to run another query on the database files to get the number.
    See my new CodingForums Blog: http://www.codingforums.com/blogs/tangoforce/

    Many useful explanations and tips including: Cannot modify headers - already sent, The IE if (isset($_POST['submit'])) bug explained, unexpected T_CONSTANT_ENCAPSED_STRING, debugging tips and much more!

  • Users who have thanked tangoforce for this post:

    LearningCoder (01-03-2013)

  • #6
    Senior Coder Dormilich's Avatar
    Join Date
    Jan 2010
    Location
    Behind the Wall
    Posts
    3,253
    Thanks
    12
    Thanked 341 Times in 337 Posts
    Quote Originally Posted by tangoforce View Post
    If you set the column to unique and try to insert an existing user, the sql server won't insert it. You can then test whether or not the user was inserted by using mysql_num_rows() which will tell you how many rows were inserted - of course if there were none due to the username already existing then it will be 0.
    or you just test whether the query suceeded. mysql_query() returns false on any DB/query error (like such a constraint violation).
    The computer is always right. The computer is always right. The computer is always right. Take it from someone who has programmed for over ten years: not once has the computational mechanism of the machine malfunctioned.
    André Behrens, NY Times Software Developer

  • #7
    Senior Coder
    Join Date
    Feb 2011
    Location
    Your Monitor
    Posts
    4,316
    Thanks
    58
    Thanked 525 Times in 512 Posts
    Blog Entries
    5
    Quote Originally Posted by Dormilich View Post
    or you just test whether the query suceeded. mysql_query() returns false on any DB/query error (like such a constraint violation).
    You have to be a bit careful using that technique because it doesn't always return false. You can select rows based on certain criteria and if none are returned it doesn't return false. Likewise if you run an update that affects 0 rows it doesn't return false. I've found that false is generally returned with badly formed SQL being executed but not often for much else (although it does happen but rarely imo).

    It's always better to go for the rows affected imo as that is the most accurate way of knowing that something was actually done
    See my new CodingForums Blog: http://www.codingforums.com/blogs/tangoforce/

    Many useful explanations and tips including: Cannot modify headers - already sent, The IE if (isset($_POST['submit'])) bug explained, unexpected T_CONSTANT_ENCAPSED_STRING, debugging tips and much more!

  • #8
    Senior Coder Dormilich's Avatar
    Join Date
    Jan 2010
    Location
    Behind the Wall
    Posts
    3,253
    Thanks
    12
    Thanked 341 Times in 337 Posts
    Quote Originally Posted by tangoforce View Post
    Likewise if you run an update that affects 0 rows it doesn't return false.
    then you have a mysql different from that described in the Manual. there it is stated:
    For other type of SQL statements, INSERT, UPDATE, DELETE, DROP, etc, mysql_query() returns TRUE on success or FALSE on error.
    The computer is always right. The computer is always right. The computer is always right. Take it from someone who has programmed for over ten years: not once has the computational mechanism of the machine malfunctioned.
    André Behrens, NY Times Software Developer

  • #9
    New Coder
    Join Date
    Mar 2012
    Posts
    52
    Thanks
    2
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Dormilich View Post
    easiest and most failsafe method is making the username column either the Primary Key or UNIQUE.
    Yeah, it's a good solution though. I set it to unique and it doesn't another a same username.

    But I need to alert the user that the username is existed. So, the user can register again. How should i do this ? I am searching for the suitable solution for two days. But the codes or guidelines are not working with my codes.

  • #10
    Senior Coder
    Join Date
    Feb 2011
    Location
    Your Monitor
    Posts
    4,316
    Thanks
    58
    Thanked 525 Times in 512 Posts
    Blog Entries
    5
    Quote Originally Posted by Jian0203 View Post
    But I need to alert the user that the username is existed. So, the user can register again. How should i do this ?
    Check mysql_num_rows() (or similar for mysqli / prepared statements) to see if there was a row inserted. If 1 then the user was registered. If 0 then there was already a user with that name.
    See my new CodingForums Blog: http://www.codingforums.com/blogs/tangoforce/

    Many useful explanations and tips including: Cannot modify headers - already sent, The IE if (isset($_POST['submit'])) bug explained, unexpected T_CONSTANT_ENCAPSED_STRING, debugging tips and much more!

  • #11
    Senior Coder
    Join Date
    Feb 2011
    Location
    Your Monitor
    Posts
    4,316
    Thanks
    58
    Thanked 525 Times in 512 Posts
    Blog Entries
    5
    Quote Originally Posted by Dormilich View Post
    then you have a mysql different from that described in the Manual. there it is stated:
    Having looked into this a bit more, you are indeed correct - I've become too snug with my own mysql functions lol.

    Anyway IMO I still think that checking the number of rows that have been affected is the better solution. It's what I've always done and what i will continue to do.
    See my new CodingForums Blog: http://www.codingforums.com/blogs/tangoforce/

    Many useful explanations and tips including: Cannot modify headers - already sent, The IE if (isset($_POST['submit'])) bug explained, unexpected T_CONSTANT_ENCAPSED_STRING, debugging tips and much more!

  • #12
    New Coder
    Join Date
    Mar 2012
    Posts
    52
    Thanks
    2
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by tangoforce View Post
    Check mysql_num_rows() (or similar for mysqli / prepared statements) to see if there was a row inserted. If 1 then the user was registered. If 0 then there was already a user with that name.
    Thank you for suggestion. I tried
    PHP Code:
            {    
            mysql_query("insert into customerdetail(customer_id,customer_pass1,customer_pass2,customer_pass3,customer_pass4,customer_pass5,customer_email) values ('$customername','$password1','$password2','$password3','$password4','$password5','$email')");
            
            if (mysql_query(sQuery))
    {
    ?>
        <script type="text/javascript">
        
        alert("Your have successfully registered!");
        location = "welcome.php";
        </script>

    <?php

    }
    else
    {
    ?>
        <script type="text/javascript">
        
        alert("Username or email already existed.");
        location = "register.php";
        </script>

    <?php

            }
    But it will only run "alert("Username or email already existed.")" nor matter fail or success. What's wrong with my coding ? O.o?
    Last edited by Jian0203; 01-03-2013 at 06:47 PM.

  • #13
    Senior Coder
    Join Date
    Feb 2011
    Location
    Your Monitor
    Posts
    4,316
    Thanks
    58
    Thanked 525 Times in 512 Posts
    Blog Entries
    5
    Yes it is.

    Edit:
    Because the op has since changed their previous reply (asking if mysqli_affected_rows() was the function to try) this post is no longer relevant but left in for continuity.
    Last edited by tangoforce; 01-03-2013 at 07:07 PM.
    See my new CodingForums Blog: http://www.codingforums.com/blogs/tangoforce/

    Many useful explanations and tips including: Cannot modify headers - already sent, The IE if (isset($_POST['submit'])) bug explained, unexpected T_CONSTANT_ENCAPSED_STRING, debugging tips and much more!

  • #14
    Senior Coder Dormilich's Avatar
    Join Date
    Jan 2010
    Location
    Behind the Wall
    Posts
    3,253
    Thanks
    12
    Thanked 341 Times in 337 Posts
    I don’t think if (mysql_query(sQuery)) will ever be true.
    The computer is always right. The computer is always right. The computer is always right. Take it from someone who has programmed for over ten years: not once has the computational mechanism of the machine malfunctioned.
    André Behrens, NY Times Software Developer

  • #15
    New Coder
    Join Date
    Mar 2012
    Posts
    52
    Thanks
    2
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Dormilich View Post
    I don’t think if (mysql_query(sQuery)) will ever be true.
    What is sQuery ? Because I am kinda new to php programing, this is the first time i deal with sQuery. If this is wrong, what should i do ? Thank you so replying


  •  
    Page 1 of 2 12 LastLast

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •