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