Categories
Database Development

How to add “You are Friends” button instead, if user is not in the addfriends table?

How to add “You are Friends” button instead, if user is not in the addfriends table?
Hi all,
I have assigned a column called “friendshipstatus” as a DEFAULT Constraint =0 in “addfriends”

table. Here the table :-

CREATE TABLE addfriends(
                          id INT AUTO_INCREMENT,            
    fname varchar(255), 
    lname varchar(255), 
    email varchar(255),     
    reg_date date,              
    friendshipstatus int(11) DEFAULT 0,
                         PRIMARY KEY (id )
                         );     

In addfriends user can add other users to it to be friends as the code bellow which will give any

users added as friend in “addfriends” table the “friendshipstatus” value of 1 otherwise the value

will still = 0

<?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; 
           } 
}
?>

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 />";
    }                   

Leave a Reply

Your email address will not be published. Required fields are marked *