Alidad Posted June 7, 2007 Share Posted June 7, 2007 Hi, I'm creating small form for sign up, one of the text field called "username", I would like to have check available user name from database, if that user name are taken then send message right away after submit it. If user name avalaible then go on to next process. I wrote small php code for some reason is not working right. I would appreication if you can help me to see what did i missed or correct it please. My database table name member. The field in database called username. The text filed in the form called username. // <? php Open conection to the database mysql_connect('localhost', 'root', '3250'); mysql_select_db('member'); // Function to check if a username exists inside the database function check_user_exist($username) { $username = mysql_escape_string($username); echo "user name is not available"; } // Username is available return array('yes'); } ?> please help me thanks. AM Quote Link to comment https://forums.phpfreaks.com/topic/54631-need-help-with-my-small-code-of-user-name/ Share on other sites More sharing options...
soycharliente Posted June 7, 2007 Share Posted June 7, 2007 Please supply the code for check_user_exist(). Quote Link to comment https://forums.phpfreaks.com/topic/54631-need-help-with-my-small-code-of-user-name/#findComment-270141 Share on other sites More sharing options...
chigley Posted June 7, 2007 Share Posted June 7, 2007 <?php mysql_connect("localhost", "****", "****"); mysql_select_db("member"); function check_user_exist($username) { $username = mysql_real_escape_string($username); $query = mysql_query("SELECT * FROM userstable WHERE username = '{$username}'") or die(mysql_error()); $rows = mysql_num_rows($query); if($rows > 0) { return false; } else { return true; } } if(check_user_exist($_POST["username"])) { die("A user already exists with that username"); } else { // Go ahead with the form processing } ?> Remove your password quick! Quote Link to comment https://forums.phpfreaks.com/topic/54631-need-help-with-my-small-code-of-user-name/#findComment-270144 Share on other sites More sharing options...
soycharliente Posted June 7, 2007 Share Posted June 7, 2007 Still post the code for your function. Let's see what the problem was. Quote Link to comment https://forums.phpfreaks.com/topic/54631-need-help-with-my-small-code-of-user-name/#findComment-270145 Share on other sites More sharing options...
chigley Posted June 7, 2007 Share Posted June 7, 2007 He already posted it: function check_user_exist($username) { $username = mysql_escape_string($username); echo "user name is not available"; } // Username is available return array('yes'); } Quote Link to comment https://forums.phpfreaks.com/topic/54631-need-help-with-my-small-code-of-user-name/#findComment-270147 Share on other sites More sharing options...
soycharliente Posted June 7, 2007 Share Posted June 7, 2007 OH. LOL I read that wrong. I thought he was calling that function. I need a break from life. Quote Link to comment https://forums.phpfreaks.com/topic/54631-need-help-with-my-small-code-of-user-name/#findComment-270152 Share on other sites More sharing options...
Alidad Posted June 7, 2007 Author Share Posted June 7, 2007 Hi, I'm creating small form for sign up, one of the text field called "username", I would like to have check available user name from database, if that user name are taken then send message right away after submit it. If user name avalaible then go on to next process. I wrote small php code for some reason is not working right. I would appreication if you can help me to see what did i missed or correct it please. My database table name member. The field in database called username. The text filed in the form called username. // <? php Open conection to the database mysql_connect('localhost', 'username', 'passwod'); mysql_select_db('member'); // Function to check if a username exists inside the database function check_user_exist($username) { $username = mysql_escape_string($username); echo "user name is not available"; } // Username is available return array('yes'); } ?> please help me thanks. AM Quote Link to comment https://forums.phpfreaks.com/topic/54631-need-help-with-my-small-code-of-user-name/#findComment-270169 Share on other sites More sharing options...
chigley Posted June 7, 2007 Share Posted June 7, 2007 Have you missed my first post, Alidad? Quote Link to comment https://forums.phpfreaks.com/topic/54631-need-help-with-my-small-code-of-user-name/#findComment-270170 Share on other sites More sharing options...
Alidad Posted June 7, 2007 Author Share Posted June 7, 2007 Thank you for your advaice to remove that user name and password, gees how stupid I am. And also thank you for your sample code, but I'm getting error message in browser said "no database select" please correct if this is right statment in (RED) text. <?php mysql_connect("localhost", "username", "password" "DatabaseName"); mysql_select_db("member"); function check_user_exist($username) { $username = mysql_real_escape_string($username); $query = mysql_query("SELECT * FROM userstable WHERE username = '{$username}'") or die(mysql_error()); $rows = mysql_num_rows($query); if($rows > 0) { return false; } else { return true; } } if(check_user_exist($_POST["username"])) { die("A user already exists with that username"); } else { // Go ahead with the form processing } ?> Quote Link to comment https://forums.phpfreaks.com/topic/54631-need-help-with-my-small-code-of-user-name/#findComment-270177 Share on other sites More sharing options...
chigley Posted June 7, 2007 Share Posted June 7, 2007 mysql_connect("localhost", "username", "password"); mysql_select_db("databasename"); Quote Link to comment https://forums.phpfreaks.com/topic/54631-need-help-with-my-small-code-of-user-name/#findComment-270183 Share on other sites More sharing options...
per1os Posted June 7, 2007 Share Posted June 7, 2007 mysql_connect("localhost", "username", "password"); mysql_select_db("member"); The database selection is done with www.php.net/mysql_select_db it should not be in the connection string. Quote Link to comment https://forums.phpfreaks.com/topic/54631-need-help-with-my-small-code-of-user-name/#findComment-270184 Share on other sites More sharing options...
Alidad Posted June 7, 2007 Author Share Posted June 7, 2007 thank you for your help these mysql database site helped me lots, but I apologize this time, this must be confusing me, I have tried everything I could to solve problme but I couldn't find to solve problme. what happend is that I recently add form with text field called usernamebut then I'm getting error message said A user already exists with that username with out any input in text filed. what went wrong! <?php mysql_connect("localhost", "username", "password"); mysql_select_db("MemberDatabase"); function check_user_exist($username) { $username = mysql_real_escape_string($username); $query = mysql_query("SELECT * FROM member WHERE username = '{$username}'") or die(mysql_error()); $rows = mysql_num_rows($query); if($rows > 0) { return false; } else { return true; } } if(check_user_exist($_POST["username"])) { die("A user already exists with that username"); } else { echo "thank you " } ?> <head> <title>check user name</title> </head> <body> <form id="form1" name="form1" method="post" action=""> User Name: <label> <input type="text" name="userName" id="userName" /> </label> <label> <input type="submit" name="button" id="button" value="Submit" /> </label> </form> </body> </html> Quote Link to comment https://forums.phpfreaks.com/topic/54631-need-help-with-my-small-code-of-user-name/#findComment-270194 Share on other sites More sharing options...
chigley Posted June 7, 2007 Share Posted June 7, 2007 if(check_user_exist($_POST["userName"])) { die("A user already exists with that username"); } else { echo "thank you " } Quote Link to comment https://forums.phpfreaks.com/topic/54631-need-help-with-my-small-code-of-user-name/#findComment-270196 Share on other sites More sharing options...
Alidad Posted June 7, 2007 Author Share Posted June 7, 2007 To chigley; hi again , I'm sorry i'm lost again, i made some change by put some text see the code: } if(check_user_exist($_POST["userName"])) { die("A user already exists with that username"); } else { echo ("<h1> <bold> Thank you </bold></hl>"); } ?> But I'm getting same error message in browser said "A user already exists with that username".... I would appreication your help can you explain what did i missed, i'm still rookie guy and learning php on my own but this part is confusing me. AM Quote Link to comment https://forums.phpfreaks.com/topic/54631-need-help-with-my-small-code-of-user-name/#findComment-270226 Share on other sites More sharing options...
per1os Posted June 7, 2007 Share Posted June 7, 2007 Just a helpful tip, the [ code ] tags are your friends. <?php function check_user_exist($username) { $username = mysql_real_escape_string($username); $query = mysql_query("SELECT * FROM member WHERE username = '{$username}'") or die(mysql_error()); $rows = mysql_num_rows($query); if($rows > 0) { return true; } else { return false; } } if(check_user_exist($_POST["userName"])) { die("A user already exists with that username"); } else { echo ("<h1> <bold> Thank you </bold></hl>"); } ?> Reverse the true/false as how you are calling it that makes the logical sense. So basically this will return true if the user exists, if the user does not exist it returns false, which is what you want as shown by the if statement. Quote Link to comment https://forums.phpfreaks.com/topic/54631-need-help-with-my-small-code-of-user-name/#findComment-270232 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.