DrTrans Posted July 27, 2009 Share Posted July 27, 2009 Basically what im doing is.. Checking Form submission to see if the groupname & groupuser already exist in db, If it doesnt exist, add it. if ($submit) { if($groupname) { $connect = mysql_connect("localhost","damgears_blaster","blaster1"); mysql_select_db("damgears_blaster"); $chkdb = mysql_query("SELECT * FROM groups"); while($row = mysql_fetch_assoc($chkdb)) { $dbgroupid = $row['groupid']; $dbgroupname = $row['groupname']; $dbgroupuser = $row['user']; if ($groupname==$dbgroupname&&$dbusername==$dbgroupuser) { echo "Group Already Exit"; } else $queryreg = mysql_query("INSERT INTO groups VALUES ('','$groupname','$dbusername')"); die ("Added Group $groupname! $dbusername"); } } else echo "Please fill in all field"; } } else die ("You are not logged in!!! Please login to continue"); Im sure its something very simple Thanks Quote Link to comment Share on other sites More sharing options...
sKunKbad Posted July 27, 2009 Share Posted July 27, 2009 why not just use mysql_num_rows() ?? If mysql_num_rows() gives you anything but 0, then you know to insert the new group/user into the database. Quote Link to comment Share on other sites More sharing options...
Psycho Posted July 27, 2009 Share Posted July 27, 2009 Write your code in a more meaningful, structured way and it will be easier to spot the errors. But, to be honest the script makes no sense. It appears that you are querying the database and after the first record is checked it will either determine the value already exists OR it will add it. But, it only checks the first record, not the others. You should be using criteria in your query. I don't see where $dbusername is defined, so I will it is assigned a value before this section of code <?php if ($submit) { if($groupname) { $connect = mysql_connect("localhost","damgears_blaster","blaster1"); mysql_select_db("damgears_blaster");$groupname $query = "SELECT * FROM groups WHERE groupname = '$groupname' AND user = '$dbusername' "; $result = mysql_query($query); if (mysql_num_rows($result) === 0) { $query = "INSERT INTO groups (groupname, user) VALUES ('$groupname','$dbusername')" mysql_query($query); $response = "Added Group $groupname! $dbusername"; } else { $response = "Group $groupname Already Exists"; } } else { $response = "Please fill in all field"; } } else { $response = "You are not logged in!!! Please login to continue"; } ?> <html> <head></head> <body> <?php echo $response; ?> </body> </html> Quote Link to comment Share on other sites More sharing options...
DrTrans Posted July 28, 2009 Author Share Posted July 28, 2009 Thanks, I appreciate it. I see what i was doing wrong. 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.