Update page when entering data in database

Asked

Viewed 69 times

-1

I have a database with a table conversations,it has the following attributes:

  1. transmitter
  2. receiver
  3. hora_msg
  4. message
  5. id

I need that as soon as a user sends a message ,update the receiver page,

$sql         = "SELECT * FROM conversas WHERE transmissor LIKE '$transmissor' AND receptor LIKE '$receptor' OR receptor LIKE '$transmissor' AND transmissor LIKE '$receptor'";
        $result      = mysqli_query($connect,$sql);
        echo "<table><thead></thead>
                <tbody>";
        if($linhas = mysqli_num_rows($result) > 0){ 
            $teste = mysqli_affected_rows($connect);
            while($row = mysqli_fetch_array($result)){
                $mensagem  = $row['mensagem'];
                $rec       = $row['receptor'];
                $hora      = $row['hora_msg'];
                $id        = $row['id'];
                $hora      = preg_replace('/:00.0000/',"",$hora);
                $ultimaMsg = strtotime($hora);
                $classe    = "";
                $result2   = mysqli_query($connect,$sql);
                while($l = mysqli_fetch_array($result2) > 0){
                    $hora2  = $l['hora_msg'];
                    $hora2  = preg_replace('/:00.0000/',"",$hora2);
                    $ultMsg = strtotime($hora2);
                    if($ultMsg > $ultimaMsg){
                        header("Refresh: 0; url = cnv.php");
                    }

                }
                echo "<div class='invisivel'>$id</div>";
                if($rec === $transmissor)$classe = 'receptor';
                else $classe = 'transmissor';
                $mensagem = base64_decode($mensagem);

                if(preg_match("/\b(?:(?:https?|ftp):\/\/|www\.)[-a-z0-9+&@#\/%?=~_|!:,.;]*[-a-z0-9+&@#\/%=~_|]/i",$mensagem))
                     echo "<tr class='sms'><td><div class=$classe><div class='hora'>$hora</div><a href='$mensagem' target='blank'>$mensagem</a></div></td></tr>";
                else     
                     echo "<tr class='sms'><td><div class=$classe><div class='hora'>$hora</div>$mensagem </div></td></tr>";
            }
            echo "</tbody>
                    </table>";

            }

But I did not get the expected result, what I need is that once something is inserted in the database the page is updated.

1 answer

2


You are trying to achieve a goal through an inappropriate path, when a message is sent, the script that is loaded is only to the user who sent the message (the request is only from a user).

When you refresh with PHP you are sending a header only to that user who sent the message. Thus, only the transmitter "wins" the refresh.

What you should be looking for is the reverse path of a "common" HTTP request, instead of the client (browser) triggering the request, the server will send a request to the browsers of one or more receivers. To do this, use websockets!

Socket.io is a good tool for this, but it’s not PHP but Nodejs.

  • I get it,

  • If you are satisfied with the answer, mark that she answered your question, but can wait if someone else answers and so have other views for your problem ;)

  • I will wait someone else, but after that mark as answered ,thank you

Browser other questions tagged

You are not signed in. Login or sign up in order to post.