Jump to content

How to stop the double add


DrTrans

Recommended Posts

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

Link to comment
Share on other sites

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>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.