Jump to content

Insert statement executed before the select statement causing wrong result


Hawkman

Recommended Posts

Hello,

 

I am using PHP and MySQL. I am trying to check whether a member already exists in a table, and if so, it will not perform the INSERTinto another table.  If the member does not exist, the INSERT should be executed.

 

Here is a quick breakdown of the code:

 

$query_rsGrpInvite = "SELECT * FROM groupinvite WHERE ToMemberID=$aMemID AND GroupID=$GrpID";

$rsGrpInvite = mysql_query($query_rsGrpInvite, $connection) or die(mysql_error());

$row_rsGrpInvite = mysql_fetch_assoc($rsGrpInvite);

$totalRows_rsGrpInvite = mysql_num_rows($rsGrpInvite);

 

# Add to Group Invite table

if ($totalRows_rsGrpInvite > 0) {

$insertGgrpInvSQL = sprintf("INSERT INTO groupinvite (GroupID, ByMemberID, ToMemberID, InvDate) VALUES (%s, %s, %s, %s)",

  GetSQLValueString($GrpID, "int"),

  GetSQLValueString($MemberID, "int"),

  GetSQLValueString($aMemID, "int"),

  GetSQLValueString($today, "date"));

$Result2 = mysql_query($insertGgrpInvSQL, $connection) or die(mysql_error());

}

 

Regardless of whether it is an existing or a new member, $totalRows_rsGrpInvite will always be = 1.

 

It seems like the INSERT is happening regardless.

 

I may be doing something stupid, so I would appreciate any help on this.

 

Many thanks.

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.