diskhub Posted November 23, 2005 Share Posted November 23, 2005 Hi, i have a code like this: <?php ... $query = "SELECT User_id from gp_users where User_name = '$NAME' and User_email = '$EMAIL'"; $result = mysql_query($query); $row2 = @mysql_num_rows($result); if ($row2 == 0 || !$row2) { DO A INSERT } else { Do nothing. } ... ?> What i want to do is to query the database to see whether there is any entry. And if there is already one, then ignore an insertion. However, i do not know what is wrong. I don't seem to get anything from [!--coloro:#CC0000--][span style=\"color:#CC0000\"][!--/coloro--]mysql_num_rows($result)[!--colorc--][/span][!--/colorc--] and it always insert data into my database. please help. Is my syntax for the $query right? Quote Link to comment Share on other sites More sharing options...
manmadareddy Posted November 23, 2005 Share Posted November 23, 2005 <?php // Connecting, selecting database $link = mysql_connect('localhost', 'dbusername', 'dbpassword') or die('Could not connect: ' . mysql_error()); mysql_select_db('dbname') or die('Could not select database'); //$message=stripslashes($_POST['txt_message']); $sql="select username from mytable where username='".$_POST['username']."' and password='".$_POST['pass']."'"; $rs=mysql_query($sql); $count=mysql_num_rows($rs); if(!$count) { $insertsql="insert into mytable (id,username,password) values(0,'".$_POST['username']."','".$_POST['pass']."')"; mysql_query($insertsql); header("Location:userhome.php"); exit; } else { echo "username alerady exists"; } ?> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> <HTML> <HEAD> <TITLE> New Document </TITLE> <META NAME="Generator" CONTENT="EditPlus"> <META NAME="Author" CONTENT=""> <META NAME="Keywords" CONTENT=""> <META NAME="Description" CONTENT=""> </HEAD> <BODY> <FORM METHOD=POST ACTION=""> <input type='text' name='username' > <input type='text' name='pass' > <INPUT TYPE="submit"> </FORM> </BODY> </HTML> Try this Quote Link to comment Share on other sites More sharing options...
diskhub Posted November 23, 2005 Author Share Posted November 23, 2005 i did the same. $query = "SELECT User_id from gp_users where User_name = '$NAME' and User_email = '$EMAIL'"; $result = mysql_query($query); $row2 = @mysql_num_rows($result); if ($row2 == 0 || !$row2) { DO A INSERT } else { Do nothing. I also have mysql_connect too. just that i didn't mention the script on top. Is $query = "SELECT User_id from gp_users where User_name = '$NAME' and User_email = '$EMAIL'"; different from $query = "SELECT User_id from gp_users where User_name = '.$NAME.' and User_email = '.$EMAIL.'"; ? Quote Link to comment Share on other sites More sharing options...
ryanlwh Posted November 23, 2005 Share Posted November 23, 2005 echo/print the query. i think $NAME and $EMAIL are null. if those 2 came from a form, make sure you use $_POST['NAME'] or turn register_globals on. Quote Link to comment Share on other sites More sharing options...
diskhub Posted November 24, 2005 Author Share Posted November 24, 2005 [!--quoteo(post=321486:date=Nov 24 2005, 02:22 AM:name=ryanlwh)--][div class=\'quotetop\']QUOTE(ryanlwh @ Nov 24 2005, 02:22 AM) 321486[/snapback][/div][div class=\'quotemain\'][!--quotec--] echo/print the query. i think $NAME and $EMAIL are null. if those 2 came from a form, make sure you use $_POST['NAME'] or turn register_globals on. Hi they are not null. anyway what's the different between these 3: [!--coloro:#CC0000--][span style=\"color:#CC0000\"][!--/coloro--]$query = "SELECT User_id from gp_users where User_name = '$NAME' and User_email = '$EMAIL'"; $query = "SELECT User_id from gp_users where User_name = '.$NAME.' and User_email = '.$EMAIL.'"; $query = "SELECT User_id from gp_users where (User_name = '$NAME' and User_email = '$EMAIL')";[!--colorc--][/span][!--/colorc--] which one you guys use usually? Quote Link to comment 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.