Hi all,
I have assigned a column called "friendshipstatus" as a default value =0 in addfriends table that user can add other users to it to be friends as the code bellow which will give any users added as friend in addfriendss table the "friendshipstatus" value of 1

<?php
include "Config.php";
//To check if user is existing or not in addfriends table by their id.
$id = $_GET['id'];
  $sql = "SELECT * FROM addfriends WHERE id = $id"; 
$result = $conn->query($sql);
if($result->num_rows >= 1) {
    echo '<script>alert("You Are Already Friends.")</script>';
    header("Refresh:0; url=index.php");
 }else{
//Then if user is not exist in the addfriends table add it by its id.
 $sql = "INSERT INTO addfriends (id,fname, lname, email,reg_date,friendshipStatus)
SELECT id, fname, lname, email, reg_date,1
FROM users WHERE id = $id"; 
      if ($conn->query($sql) === TRUE){ 
    echo '<script>alert("You Are Friends Now.")</script>';
    header("Refresh:0; url=index.php");       } else { 
           echo "Error: " . $sql . "<br>" . $conn->error; 
           } 
}
?>

Now I want to know how to detect when "friendshipstatus" =0 and when it =1
using mysql php
Thanks in advance

Recommended Answers

All 11 Replies

Hello
Any help please !!

Hi there. Sorry for taking so long to see this thread. I'm looking at your code and I'm a little confused what you want to do.

It looks like on line 12, you're inserting a row into the addfriends table with the information of user $id and setting friendshipStatus to 1.

However, I think what you're trying to do is on line 5, you want to: SELECT * FROM addfriends WHERE id = $id AND friendshipStatus = 1 because right now you are just checking if the user $id has a row in the table, and not if the friendshipStatus column is set to 1.

Please let me know if I can help further.

Hello Dani,
Thanks for your concern.
What I am trying to do is to detect if user in addfriends table or not.
So if it is in addfriends table to put beside its name a "Add Friend" button .But if it is not in the addfriends table to put beside its name a "You are Friends" button instead.
In the following query I accomplished the first part of what I want tyo do tha t to add "Add Friend" button beside user name if it is in addfriends table. But now how to add "You are Friends" button instead if it is not in the addfriends table?

<?php
include 'config.php';
//Show The Three New Users that have registered today.
$sql = "SELECT * FROM `users`
 WHERE DATE(`reg_date`) = CURDATE()
 ORDER BY id desc
 LIMIT 3";

$result = $conn->query($sql);
      echo "<br />";
      echo "<span class=\"label label-danger col-sm-12\">New Users</span></th>";
      echo "<br />";

while($row = $result->fetch_assoc()) {
    //To display user's profile when click on it.
  echo "<tr>";
  echo "<td><a href='DisplayBasedOnID.php?id=" . $row['id'] . "'>
       ".$row["fname"]." ".$row["lname"]."
       </a></td>"; 
   //To display "Add Friend" button beside each username.
  echo "<td>
  <span class=\"label label-primary pull-right\">
  <a href='AddFriend.php?id=" . $row['id'] . "'style=\"color:White\">
   Add Friend
    </a>
    </span>
    </td>"; 
    echo "</tr>";
  echo "<br />";
    }

Upppp

I haven’t slept well the past couple of days and I’ve been working nonstop. I’m already in bed now so I’ll check this out tomorrow.

I am really sorry then.
Take your time .

Soooo sorry. I know I told you I would have checked this out by now but I've just been so busy the past couple days and I have a very bad migraine today.

I am really sorry again. And I hope you get well soon.

Helllo

uppppp

Gahhhh! Thanks for the bump (reminder). I’ll look at this tomorrow. I promise. Sorrrryyyyyyy.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.