Jump to content

Help changing my code :)


Glenskie

Recommended Posts

ok i need to change this code to new table and style ... right now its using an array and i want to change it to where it uses a table with 2 colums for the friends ... here is what im talking about

 

Table = Friends

colums are id , mem_1 , mem_2 , Date

 

here is my code to revise :)

if ($_POST["request"] == "acceptFriend") {

    $reqID = preg_replace('#[^0-9]#i', '', $_POST['reqID']);
$sql = "SELECT * FROM friends_requests WHERE id='$reqID' LIMIT 1";
$query = mysql_query($sql) or die ("Sorry we had a mysql error!");
$num_rows = mysql_num_rows($query); 
if ($num_rows < 1) {
	echo 'An error occured';
	exit();
}
    while ($row = mysql_fetch_array($query)) { 
	$mem1 = $row["mem1"];
	$mem2 = $row["mem2"];
    }
$sql_frnd_arry_mem1 = mysql_query("SELECT friend_array FROM myMembers WHERE id='$mem1' LIMIT 1"); 
$sql_frnd_arry_mem2 = mysql_query("SELECT friend_array FROM myMembers WHERE id='$mem2' LIMIT 1"); 
while($row=mysql_fetch_array($sql_frnd_arry_mem1)) { $frnd_arry_mem1 = $row["friend_array"]; }
while($row=mysql_fetch_array($sql_frnd_arry_mem2)) { $frnd_arry_mem2 = $row["friend_array"]; }
$frndArryMem1 = explode(",", $frnd_arry_mem1);
$frndArryMem2 = explode(",", $frnd_arry_mem2);
    if (in_array($mem2, $frndArryMem1)) { echo  'This member is already your Friend'; exit(); }
if (in_array($mem1, $frndArryMem2)) { echo  'This member is already your Friend'; exit(); }
if ($frnd_arry_mem1 != "") { $frnd_arry_mem1 = "$frnd_arry_mem1,$mem2"; } else { $frnd_arry_mem1 = "$mem2"; }
if ($frnd_arry_mem2 != "") { $frnd_arry_mem2 = "$frnd_arry_mem2,$mem1"; } else { $frnd_arry_mem2 = "$mem1"; }
    $UpdateArrayMem1 = mysql_query("UPDATE myMembers SET friend_array='$frnd_arry_mem1' WHERE id='$mem1'") or die (mysql_error());
    $UpdateArrayMem2 = mysql_query("UPDATE myMembers SET friend_array='$frnd_arry_mem2' WHERE id='$mem2'") or die (mysql_error());
$deleteThisPendingRequest = mysql_query("DELETE FROM friends_requests WHERE id='$reqID' LIMIT 1"); 
    echo "You are now friends with this member!";
exit();
}

Link to comment
Share on other sites

$reqID = preg_replace('#[^0-9]#i', '', $_POST['reqID']);

 

Replace this with, you don't need the regex (regex is also wrong, you don't need the i modifier):

 

$reqID = intval($_POST['reqID']);
if ($reqID <= 0) {
    //invalid ID specified
}

 

while ($row = mysql_fetch_array($query)) { 
    $mem1 = $row["mem1"];
    $mem2 = $row["mem2"];
}

 

You don't need the while() here since you only return 1 record, unless your ID is not unique? But then your code is wrong too since you would only process the last returned relation (as each iteration overwrites $mem1 and $mem2).

 

$sql = "SELECT * FROM friends_requests WHERE id='$reqID' LIMIT 1";

 

Replace with:

 

$sql = "
    SELECT FIND_IN_SET(T2.id, T3.friend_array) !== 0 AS mem2_has_friend,
           FIND_IN_SET(T3.id, T2.friend_array) !== 0 AS mem1_has_friend
    FROM friends_requests T1
    JOIN myMembers T2 ON T2.id = T1.mem1
    JOIN myMembers T3 ON T3.id = T1.mem2
    WHERE id = $reqID
";

 

This returns who is befriended with who. That said you should not store friends in a CSV instead you should store this relation in a separate table (for example keep it in friends_requests, don't delete at the end, and add additional columns that indicate wether both accepted the relation).

 

 

 

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.