lAZLf Posted December 28, 2009 Share Posted December 28, 2009 I'm creating a registration page for my website. I have two forms I want to check the value of to see if it is in the database already (username and email). How could I do this? Link to comment https://forums.phpfreaks.com/topic/186507-check-if-something-exists-in-the-database-already/ Share on other sites More sharing options...
cags Posted December 28, 2009 Share Posted December 28, 2009 There are several ways, the most common method is probably to use "SELECT id FROM table WHERE username='$username'" and to then use mysql_num_rows to check how many rows are returned. If it's 0 then you know that the username passed as $username is not in the database already. Link to comment https://forums.phpfreaks.com/topic/186507-check-if-something-exists-in-the-database-already/#findComment-984913 Share on other sites More sharing options...
lAZLf Posted December 28, 2009 Author Share Posted December 28, 2009 I did what u suggested, but it won't work for some reason. I keep getting this error: "Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in /home4/annarbo1/public_html/registerin.php on line 12 Your passwords or email adresss don't match up" the code: <?php session_start(); $con = mysql_connect("localhost","xxxxxxx","xxxxxxxxx"); if (!$con) { die('Could not connect: ' . mysql_error()); } mysql_select_db("annarbo1_Archive", $con); $date = date("Y/m/d"); $sql="INSERT INTO people (fname, lname, username, password, joined, email, level) VALUES ('$_POST[fname]','$_POST[lname]','$_POST[user]',sha1('$_POST[pass]'),'$date','$_POST[email]','norm')"; $sqlcheck="SELECT * FROM people WHERE username = '$_POST[user]' OR email = '$_POST[email]'"; $numrows = mysql_num_rows($sqlcheck); if (!empty($_POST['pass']) || !empty($_POST['user']) || !empty($_POST['confemail']) || !empty($_POST['confpass']) || !empty($_POST['email']) || !empty($_POST['fname']) || !empty($_POST['lname'])) { if($_POST['confemail'] == $_POST['email'] && $_POST['pass'] == $_POST['confpass']) { if($numrows == 0) { if (!mysql_query($sql,$con)) { die('Error: ' . mysql_error()); } else{ header ("Location: userinfo.php"); } } else { echo "username or email taken already"; } } else { echo"Your passwords or email adresss don't match up"; } } else { echo "empty fields"; } mysql_close($con); ?> Link to comment https://forums.phpfreaks.com/topic/186507-check-if-something-exists-in-the-database-already/#findComment-984933 Share on other sites More sharing options...
atl_andy Posted December 28, 2009 Share Posted December 28, 2009 You have syntax issues...try: $sql="INSERT INTO people (fname, lname, username, password, joined, email, level) VALUES ('$_POST[fname]','$_POST[lname]','$_POST[user]',sha1('$_POST[pass]'),'$date','$_POST[email]','norm')"; $result = mysql_query($sql); $sqlcheck="SELECT * FROM people WHERE username = '$_POST[user]' OR email = '$_POST[email]'"; $result = mysql_query($sqlcheck); $numrows = mysql_num_rows($result); check these links for the correct usage of INSERT and SELECT statements http://us.php.net/mysql_num_rows for using mysql_num_rows and http://us.php.net/manual/en/function.mysql-query.php for SELECT and INSERT statements Link to comment https://forums.phpfreaks.com/topic/186507-check-if-something-exists-in-the-database-already/#findComment-984942 Share on other sites More sharing options...
lAZLf Posted December 28, 2009 Author Share Posted December 28, 2009 ^thanks! it's working now Link to comment https://forums.phpfreaks.com/topic/186507-check-if-something-exists-in-the-database-already/#findComment-984955 Share on other sites More sharing options...
atl_andy Posted December 28, 2009 Share Posted December 28, 2009 Please mark this topic solved. Link to comment https://forums.phpfreaks.com/topic/186507-check-if-something-exists-in-the-database-already/#findComment-984956 Share on other sites More sharing options...
lAZLf Posted December 28, 2009 Author Share Posted December 28, 2009 how do i do that? Link to comment https://forums.phpfreaks.com/topic/186507-check-if-something-exists-in-the-database-already/#findComment-984958 Share on other sites More sharing options...
atl_andy Posted December 28, 2009 Share Posted December 28, 2009 There should be a green button near the bottom of your screen that says "SOLVED", or something...click it. Link to comment https://forums.phpfreaks.com/topic/186507-check-if-something-exists-in-the-database-already/#findComment-984960 Share on other sites More sharing options...
lAZLf Posted December 28, 2009 Author Share Posted December 28, 2009 ah thanks Link to comment https://forums.phpfreaks.com/topic/186507-check-if-something-exists-in-the-database-already/#findComment-984961 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.