Hawkman Posted February 15, 2009 Share Posted February 15, 2009 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. Link to comment https://forums.phpfreaks.com/topic/145266-insert-statement-executed-before-the-select-statement-causing-wrong-result/ Share on other sites More sharing options...
Mchl Posted February 15, 2009 Share Posted February 15, 2009 Perhaps you should rather use this http://dev.mysql.com/doc/refman/5.0/en/insert-on-duplicate.html Link to comment https://forums.phpfreaks.com/topic/145266-insert-statement-executed-before-the-select-statement-causing-wrong-result/#findComment-762652 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.