Hi, I'm working on a site that allows users to connect with each other. User can search, find friend, and then request a connection. It works but if user request a second time it duplicates the row in table. I only want INSERT if does not exists.
Here is code.

if ((isset($_POST["MM_insert"])) && ($_POST["MM_insert"] == "form2")) { 
	$insertSQL = sprintf("INSERT INTO mystuff.contact(user_id, user_url, rec_id, contact_id) VALUES(%s, %s, %s, %s)",
                       GetSQLValueString($_POST['hiddenField2'], "int"),
					   GetSQLValueString($_POST['hiddenField'], "text"),
					   GetSQLValueString($_POST['hiddenField3'], "text"),
					   GetSQLValueString($_POST['hiddenField4'], "text"),
					   GetSQLValueString($_POST['hiddenField5'], "text"),
                       GetSQLValueString($_POST['contact_id'], "int"));
mysql_select_db($database_connAdmin, $connAdmin);
  $Result1 = mysql_query($insertSQL, $connAdmin) or die(mysql_error());

Only thing that really matters here is user_id and rec_id, which is other users user_id.

7 Years
Discussion Span
Last Post by phpDave

You can use REPLACE in SQL syntax which will update if does exist or insert if not.


Although I am thinking that using REPLACE would reset my request col. from 1 back to 0.
0 if request, and 1 if request is accepted.
Anyway, I trying to just validate by using:

do {
	$already = $row_Recordset4['rec_id'];
	$test = $row_Recordset1['user_id'];
} while ($row_Recordset4 = mysql_fetch_assoc($Recordset4));
if ($already == $test) {
	$error['No'] = 'Already connected with.'; }

Problem is $test is Recordset1 and $already is Recordset4
Can not seem to get it to work right.


their r 2 ways of doing this:-
1. Use this code...

$result = mysql_query("SELECT * FROM table1", $link);
$num_rows = mysql_num_rows($result);
echo "Already Connected";

Another solution can be setting a variable and checking that variable before insertion...if that variable is set then display already connected...
Hope i m clear...


Thanks, I like the second choice and I have tried it before but could not get it to work, any advice on the code.
I tried something like this before and also a few other things.

if ((isset($_POST["MM_insert"])) && ($_POST["MM_insert"] == "form2")[B] &&(!$already)[/B]) {
$insertSQL = sprintf("INSERT INTO mystuff.contact(user_id, user_url, rec_id, contact_id) VALUES(%s, %s, %s, %s)",

whats the problem with ur code...it's looks fine ...
check this way....

if ((isset($_POST["MM_insert"])) && ($_POST["MM_insert"] == "form2") &&($already!=1))

for first way i told...
use the select query that u want...i don't know ur exact query so i wrote the simplest one...


Thanks, I tried but still not working, I have the $already var. in a do while loop.
Could that be why not working?
Each user can have more than one contact. I got the validation to work but only match one connection, not multiple.


Almost there!
Here is what I have:

$var1_check = "-1";
if (isset($_POST['hiddenField9'])) {
  $var1_check = $_POST['hiddenField9'];

do {
	 $already = $row_Recordset4['rec_id'];
} while ($row_Recordset4 = mysql_fetch_assoc($Recordset4));
if ($var1_check ==($already) {
	$error['No'] = 'Already connected with.';}

Only problem is $already in the if statement is only bringing up one rec_id and not all as it does inside the loop. Not sure how to handle this.


Thanks for replies, Problem solved. Some of it was in SELECT QUERY.

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.