Send message to connected user

I want to send a message from user ( user A ) to another user ( user B ) that these users are connected to each other in the database. To be more specific.

We maintain a join of users in a table in a database that we have called friends . This table has two columns, username and friend.

I have some code to send data between users, but it doesn't do any checks to see if user A wants to send a message to user B related to each other. If users are connected, I want to allow them to send a message, and if they are not I want to echo a notification that they are not allowed to send a message because they are not connected.

I can figure out that I need a condition if

where I am performing a check to check if the users are connected and have the corresponding code below, and if it is not connected, output the notification described above.

How can I create this check?

I am using php and mysql

HERE IS MY CODE ...

<?php
include_once 'header.php';

if (!$loggedin) die();

if (isset($_GET['view'])) {
  $view = sanitizeString($_GET['view']);
} else {
  $view = $username;
}

if (isset($_POST['text'])){
    $text = sanitizeString($_POST['text']);

    if ($text != ""){
        $pm   = substr(sanitizeString($_POST['pm']),0,1);
        $time = time();
        queryMysql("INSERT INTO messages VALUES(NULL, '$username', '$view', '$pm', $time, '$text')");
    }
}

if ($view != "") {
    if ($view == $username) {
       $name1 = $name2 = "Your";
    } else {
        $name1 = "<a href='members.php?view=$view'>$view</a>'s";
        $name2 = "$view's";
    }

    echo "<div class='main'><h3>$name1 Messages</h3>";
    showProfile($view);

    echo <<<_END
<form method='post' action='messages.php?view=$view'>
Type here to leave a message:<br />
<textarea name='text' cols='40' rows='3'></textarea><br />
Public<input type='radio' name='pm' value='0' checked='checked' />
Private<input type='radio' name='pm' value='1' />
<input type='submit' value='Post Message' /></form><br />
_END;

    if (isset($_GET['erase'])) {
        $erase = sanitizeString($_GET['erase']);
        queryMysql("DELETE FROM messages WHERE id=$erase AND recip='$username'");
    }

    $query  = "SELECT * FROM messages WHERE recip='$view' ORDER BY time DESC";
    $result = queryMysql($query);
    $num    = mysql_num_rows($result);

    for ($j = 0 ; $j < $num ; ++$j) {
        $row = mysql_fetch_row($result);

        if ($row[3] == 0 || $row[1] == $username || $row[2] == $username) {
            echo date('M jS \'y g:ia:', $row[4]);
            echo " <a href='messages.php?view=$row[1]'>$row[1]</a> ";

            if ($row[3] == 0) {
                 echo "wrote: &quot;$row[5]&quot; ";
            } else { 
                 echo "whispered: <span class='whisper'>" . "&quot;$row[5]&quot;</span> ";
            }
            if ($row[2] == $username) {
                echo "[<a href='messages.php?view=$view" . "&erase=$row[0]'>erase</a>]";
            }
            echo "<br>";
        }
    }
}

if (!$num) {
  echo "<br /><span class='info'>No messages yet</span><br /><br />";
}
echo "<br /><a class='button' href='messages.php?view=$view'>Refresh messages</a>";
?>

</div><br /></body></html>

      

+3


source to share


1 answer


The checkout system for my question is below and it works.

<?php
include_once 'header.php';

if (!$loggedin) die();

if (isset($_GET['view'])) $view = sanitizeString($_GET['view']);
else                      $view = $username;

$result1 = mysql_num_rows(queryMysql("SELECT username,friend FROM friends
            WHERE username='$username' AND friend='$view'"));



$result2 = mysql_num_rows(queryMysql("SELECT username,friend FROM friends
            WHERE username='$view' AND friend='$username'"));

if (($result1 + $result2) > 1)
{
 //REST OF THE CODE 
}

      

? >



what we do is that for result1 we check if the logged in username ($ username) is associated with the viewed profile ($ view), and for result2 we do the other way around to be more specific, we check in result 2 that if the username of the profile being viewed ($ view) is associated with ($ username), then in the if statement we check that if these two results have more than one row in the table, then they are both related.

PS: sorry for my bad english

+2


source







All Articles