Go Back   CodingForums.com > :: Server side development > PHP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 01-02-2013, 09:44 PM   PM User | #1
Jian0203
New Coder

 
Join Date: Mar 2012
Posts: 47
Thanks: 2
Thanked 0 Times in 0 Posts
Jian0203 is an unknown quantity at this point
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
Jian0203 is offline   Reply With Quote
Old 01-02-2013, 11:18 PM   PM User | #2
mlseim
Master Coder

 
mlseim's Avatar
 
Join Date: Jun 2003
Location: Cottage Grove, Minnesota
Posts: 9,045
Thanks: 8
Thanked 1,029 Times in 1,020 Posts
mlseim has a spectacular aura aboutmlseim has a spectacular aura aboutmlseim has a spectacular aura about
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.
mlseim is offline   Reply With Quote
Old 01-03-2013, 01:21 PM   PM User | #3
Dormilich
Senior Coder

 
Dormilich's Avatar
 
Join Date: Jan 2010
Location: Behind the Wall
Posts: 2,870
Thanks: 9
Thanked 291 Times in 287 Posts
Dormilich is on a distinguished road
Quote:
Prevent same username registered.
easiest and most failsafe method is making the username column either the Primary Key or UNIQUE.
__________________
please post your code wrapped in [CODE] [/CODE] tags
Dormilich is offline   Reply With Quote
Old 01-03-2013, 01:45 PM   PM User | #4
LearningCoder
Regular Coder

 
LearningCoder's Avatar
 
Join Date: Jan 2011
Location: The Pleiades
Posts: 849
Thanks: 67
Thanked 28 Times in 28 Posts
LearningCoder is an unknown quantity at this point
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.
LearningCoder is offline   Reply With Quote
Old 01-03-2013, 02:34 PM   PM User | #5
tangoforce
Senior Coder

 
tangoforce's Avatar
 
Join Date: Feb 2011
Location: Your Monitor
Posts: 3,504
Thanks: 45
Thanked 439 Times in 428 Posts
tangoforce will become famous soon enoughtangoforce will become famous soon enough
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.
__________________
Please wrap your code in [php] tags. It is a sticky topic and it HELPS us to HELP YOU!
TIP: Coding styles and $end errors :::::::::: TIP: Warning: Cannot modify header information - headers already sent :::::::::: TIP: Quotes / Parse error: syntax error, unexpected T_..
PHP Code:
//Please don't use this for your form processing:
if (isset($_POST['submit']))
//Internet explorer has a bug and does not always send the submit value. 
Explanation: The IE if(isset($_POST['submit'])) bug explained.
tangoforce is offline   Reply With Quote
Users who have thanked tangoforce for this post:
LearningCoder (01-03-2013)
Old 01-03-2013, 02:57 PM   PM User | #6
Dormilich
Senior Coder

 
Dormilich's Avatar
 
Join Date: Jan 2010
Location: Behind the Wall
Posts: 2,870
Thanks: 9
Thanked 291 Times in 287 Posts
Dormilich is on a distinguished road
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).
__________________
please post your code wrapped in [CODE] [/CODE] tags
Dormilich is offline   Reply With Quote
Old 01-03-2013, 03:34 PM   PM User | #7
tangoforce
Senior Coder

 
tangoforce's Avatar
 
Join Date: Feb 2011
Location: Your Monitor
Posts: 3,504
Thanks: 45
Thanked 439 Times in 428 Posts
tangoforce will become famous soon enoughtangoforce will become famous soon enough
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
__________________
Please wrap your code in [php] tags. It is a sticky topic and it HELPS us to HELP YOU!
TIP: Coding styles and $end errors :::::::::: TIP: Warning: Cannot modify header information - headers already sent :::::::::: TIP: Quotes / Parse error: syntax error, unexpected T_..
PHP Code:
//Please don't use this for your form processing:
if (isset($_POST['submit']))
//Internet explorer has a bug and does not always send the submit value. 
Explanation: The IE if(isset($_POST['submit'])) bug explained.
tangoforce is offline   Reply With Quote
Old 01-03-2013, 04:43 PM   PM User | #8
Dormilich
Senior Coder

 
Dormilich's Avatar
 
Join Date: Jan 2010
Location: Behind the Wall
Posts: 2,870
Thanks: 9
Thanked 291 Times in 287 Posts
Dormilich is on a distinguished road
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:
Quote:
For other type of SQL statements, INSERT, UPDATE, DELETE, DROP, etc, mysql_query() returns TRUE on success or FALSE on error.
__________________
please post your code wrapped in [CODE] [/CODE] tags
Dormilich is offline   Reply With Quote
Old 01-03-2013, 06:01 PM   PM User | #9
Jian0203
New Coder

 
Join Date: Mar 2012
Posts: 47
Thanks: 2
Thanked 0 Times in 0 Posts
Jian0203 is an unknown quantity at this point
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.
Jian0203 is offline   Reply With Quote
Old 01-03-2013, 06:03 PM   PM User | #10
tangoforce
Senior Coder

 
tangoforce's Avatar
 
Join Date: Feb 2011
Location: Your Monitor
Posts: 3,504
Thanks: 45
Thanked 439 Times in 428 Posts
tangoforce will become famous soon enoughtangoforce will become famous soon enough
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.
__________________
Please wrap your code in [php] tags. It is a sticky topic and it HELPS us to HELP YOU!
TIP: Coding styles and $end errors :::::::::: TIP: Warning: Cannot modify header information - headers already sent :::::::::: TIP: Quotes / Parse error: syntax error, unexpected T_..
PHP Code:
//Please don't use this for your form processing:
if (isset($_POST['submit']))
//Internet explorer has a bug and does not always send the submit value. 
Explanation: The IE if(isset($_POST['submit'])) bug explained.
tangoforce is offline   Reply With Quote
Old 01-03-2013, 06:05 PM   PM User | #11
tangoforce
Senior Coder

 
tangoforce's Avatar
 
Join Date: Feb 2011
Location: Your Monitor
Posts: 3,504
Thanks: 45
Thanked 439 Times in 428 Posts
tangoforce will become famous soon enoughtangoforce will become famous soon enough
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.
__________________
Please wrap your code in [php] tags. It is a sticky topic and it HELPS us to HELP YOU!
TIP: Coding styles and $end errors :::::::::: TIP: Warning: Cannot modify header information - headers already sent :::::::::: TIP: Quotes / Parse error: syntax error, unexpected T_..
PHP Code:
//Please don't use this for your form processing:
if (isset($_POST['submit']))
//Internet explorer has a bug and does not always send the submit value. 
Explanation: The IE if(isset($_POST['submit'])) bug explained.
tangoforce is offline   Reply With Quote
Old 01-03-2013, 06:21 PM   PM User | #12
Jian0203
New Coder

 
Join Date: Mar 2012
Posts: 47
Thanks: 2
Thanked 0 Times in 0 Posts
Jian0203 is an unknown quantity at this point
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..
Jian0203 is offline   Reply With Quote
Old 01-03-2013, 06:48 PM   PM User | #13
tangoforce
Senior Coder

 
tangoforce's Avatar
 
Join Date: Feb 2011
Location: Your Monitor
Posts: 3,504
Thanks: 45
Thanked 439 Times in 428 Posts
tangoforce will become famous soon enoughtangoforce will become famous soon enough
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.
__________________
Please wrap your code in [php] tags. It is a sticky topic and it HELPS us to HELP YOU!
TIP: Coding styles and $end errors :::::::::: TIP: Warning: Cannot modify header information - headers already sent :::::::::: TIP: Quotes / Parse error: syntax error, unexpected T_..
PHP Code:
//Please don't use this for your form processing:
if (isset($_POST['submit']))
//Internet explorer has a bug and does not always send the submit value. 
Explanation: The IE if(isset($_POST['submit'])) bug explained.

Last edited by tangoforce; 01-03-2013 at 07:07 PM..
tangoforce is offline   Reply With Quote
Old 01-03-2013, 06:50 PM   PM User | #14
Dormilich
Senior Coder

 
Dormilich's Avatar
 
Join Date: Jan 2010
Location: Behind the Wall
Posts: 2,870
Thanks: 9
Thanked 291 Times in 287 Posts
Dormilich is on a distinguished road
I don’t think if (mysql_query(sQuery)) will ever be true.
__________________
please post your code wrapped in [CODE] [/CODE] tags
Dormilich is offline   Reply With Quote
Old 01-03-2013, 06:56 PM   PM User | #15
Jian0203
New Coder

 
Join Date: Mar 2012
Posts: 47
Thanks: 2
Thanked 0 Times in 0 Posts
Jian0203 is an unknown quantity at this point
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
Jian0203 is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 04:58 PM.


Advertisement
Log in to turn off these ads.