steeko1990 Posted May 5, 2013 Share Posted May 5, 2013 hi guys I am i am sorry to be a bother but i am new to php and I am currently in the process of creating, login and register pages that store a users name and password in to a database but I am finding it hard to source any code to check if the name exists and if it does tell the user that it does and that they have to choose a alternative user name. The code I have for updating the database is: <?php extract ($_POST); $con = mysql_connect("localhost","root",""); if (!$con) { die('Could not connect: ' . mysql_error()); } mysql_select_db("firstone", $con); $username = $_POST['username']; $password = $_POST['password']; // just to show the collection and storage of information from the form mysql_query("INSERT INTO details (username , password) VALUES ('$username', '$password')"); echo "your account has been created click next to continue"; mysql_close($con); ?> Quote Link to comment https://forums.phpfreaks.com/topic/277678-stop-duplicate-enteries-into-a-database-and-check-to-see-if-user-exists/ Share on other sites More sharing options...
Phear46 Posted May 5, 2013 Share Posted May 5, 2013 do a query to 'select' all the rows matching your input before you add it to the table. then: if rows returned > 0 { user exists } else { process form as you already are } a query like: SELECT * FROM table WHERE user=$usrname, pass=$pass is what your looking for, although i highly doubt ive got the correct syntax there as im also a noob, one of the pros should be able to help with that! Quote Link to comment https://forums.phpfreaks.com/topic/277678-stop-duplicate-enteries-into-a-database-and-check-to-see-if-user-exists/#findComment-1428466 Share on other sites More sharing options...
davidannis Posted May 5, 2013 Share Posted May 5, 2013 (edited) Phear46 has a reasonable suggestion. Don't select on the password, just the username. A few more suggestions: Use mysqli instead of mysql when creating your table make the field username a PRIMARY or UNIQUE index. Salt and hash passwords. Edited May 5, 2013 by davidannis Quote Link to comment https://forums.phpfreaks.com/topic/277678-stop-duplicate-enteries-into-a-database-and-check-to-see-if-user-exists/#findComment-1428468 Share on other sites More sharing options...
steeko1990 Posted May 5, 2013 Author Share Posted May 5, 2013 sorry to be a bother i know i am really out of my depth but could anyone direct me to anywhere that i could find the corrent syntax for if rows >0. Quote Link to comment https://forums.phpfreaks.com/topic/277678-stop-duplicate-enteries-into-a-database-and-check-to-see-if-user-exists/#findComment-1428472 Share on other sites More sharing options...
blacknight Posted May 5, 2013 Share Posted May 5, 2013 http://php.net/manual/en/function.mysql-num-rows.php php.net your friend when lurning allways good code examples there Quote Link to comment https://forums.phpfreaks.com/topic/277678-stop-duplicate-enteries-into-a-database-and-check-to-see-if-user-exists/#findComment-1428480 Share on other sites More sharing options...
RonnieCosta50 Posted May 6, 2013 Share Posted May 6, 2013 (edited) <?php include 'dbConnect.php'; // Database Connection php file. include 'dbTableName.php'; // Contains 1 line of code. The name of your table in the database. I call it $dbTableName. include 'dbTableColumns.php'; // The names of all your columns in your database. Like your primary key is $dbColumn1. $qryDuplicateString = mysql_query("SELECT COUNT(*) AS count FROM $dbTableName WHERE $dbColumn2 = '$wsTextbox1' AND $dbColumn3 = '$wsDropDownList1'"); $duplicateRows = mysql_fetch_assoc($qryDuplicateString); $numOfRows = $duplicateRows['count']; if ($numOfRows<1) // If a row doesn't exist, {echo"Create Account";} else {echo"Choose alternative username";} ?> Hope this helps. Edited May 6, 2013 by RonnieCosta50 Quote Link to comment https://forums.phpfreaks.com/topic/277678-stop-duplicate-enteries-into-a-database-and-check-to-see-if-user-exists/#findComment-1428539 Share on other sites More sharing options...
steeko1990 Posted May 6, 2013 Author Share Posted May 6, 2013 sorry to be a pain but a cant get either of the suggestions to work. could some one be able to put a step by step for me i know i am asking a lot. Quote Link to comment https://forums.phpfreaks.com/topic/277678-stop-duplicate-enteries-into-a-database-and-check-to-see-if-user-exists/#findComment-1428546 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.