Posting system duplicating posts - PHP [closed]

1

Hello everyone! I'm developing a social network and I'm in the publications section. The problem is that when I go to make a new post and click publish, it publishes the previous post and when I update the page it is that it publishes the post that I did. As I am new to PHP still, I think it has to do with the browser cookie but I do not know how to solve it. Finally, follow the code below:

<?php
    include("header.php");
    
    $pubs = mysql_query("SELECT * FROM publicacao ORDER BY id desc");
    
    if (isset($_POST['publish'])){
        if($_FILES["file"]["error"] > 0){
            $texto = $_POST["texto"];
            $hoje = date("Y-m-d");
            
            if ($texto == ""){
                echo "<h3> Publicação vazia! </h3>";
            }else{
                $query = "INSERT INTO publicacao (usuario, texto, data) VALUES ('$login_cookie', '$texto', '$hoje')";
                $data = mysql_query($query) or die ();
                if ($data) {
                    header("Location ./");
                }else{
                    echo "Algo está errado! Tente outra vez mais tarde.";
                }
            }
        }else{
            $n = rand(0, 1000000);
            $img = $n.$_FILES["file"]["name"];
            
            move_uploaded_file($_FILES["file"]["tmp_name"], "upload/".$img);
            
            $texto = $_POST['texto'];
            $hoje = date("Y-m-d");
            
            if ($texto == ""){
                echo "<h3> Publicação vazia! </h3>";
            }else{
                $query = "INSERT INTO publicacao (usuario, texto, imagem, data) VALUES ('$login_cookie', '$texto', '$img', '$hoje')";
                $data = mysql_query($query) or die ();
                if ($data) {
                    header("Location ./");
                }else{
                    echo "Algo está errado! Tente outra vez mais tarde.";
                }
            }
        }
    }
?>
    <head>
        <header>
            <style type="text/css">
                #publish{
                    width: 400px;
                    height: 210px;
                    display: block;
                    margin: auto;
                    border-radius: 5px;
                    background: #FFF;
                    box-shadow: 0px 0px 6px #A1A1A1;
                    margin-top: 30px;
                }
                #publish textarea{
                    width: 365px;
                    height: 150px;
                    display: block;
                    margin: auto;
                    border-radius: 5px;
                    padding-left: 5px;
                    padding-top: 5px;
                    border-width: 1px;
                    border-color: #A1A1A1;
                }
                #publish img{
                    margin-top: 0px;
                    margin-left: 10px;
                    width: 40px;
                    cursor: pointer;
                }
                #publish input[type="submit"]{
                    width: 70px;
                    height: 25px;
                    border-radius: 3px;
                    float: right;
                    margin-right: 15px;
                    border: none;
                    margin-top: 5px;
                    background: #4169E1;
                    color: #FFF;
                    cursor: pointer;
                }
                #publish input[type="submit"]:hover{
                    background: #001F3F;
                }
                
                .pub{
                    width: 400px;
                    min-height: 70px;
                    max-height: 1000px;
                    display: block;
                    margin: auto;
                    border: none;
                    border-radius: 3px;
                    background-color: #FFF;
                    box-shadow: 0px 0px 6px #A1A1A1;
                    margin-top: 30px;
                }
                .pub a{
                    color: #666;
                    text-decoration: none;
                }
                .pub a:hover{
                    color: #111;
                    text-decoration: none;
                }
                .pub p{
                    margin-left: 10px;
                    content: #666;
                    padding-top: 10px;
                }
                .pub span{
                    display: block;
                    margin: auto;
                    width: 380px;
                    margin-top: 10px;
                }
                .pub img{
                    display: block;
                    margin: auto;
                    width: 100%;
                    margin-top: 10px;
                    border-bottom-left-radius: 5px;
                    border-bottom-right-radius: 5px;
                }
            </style>
        </header>
    </head>
    <body>
        <div id="publish">
            <form method="POST" enctype="multipart/form-data">
                <br>
                <textarea placeholder="O que você está pensando nesse momento?" name="texto"></textarea>
                <label for="file-input">
                    <img src="img/imagegrey.png" title="Fazer upload de foto" />
                </label>
                <input type="submit" value="Publicar" name="publish" />
                
                <input type="file" id="file-input" name="file" hidden />
            </form>
        </div>
        <?php
            while ($pub = mysql_fetch_assoc($pubs)){
                $email = $pub['usuario'];
                $saberr = mysql_query("SELECT * FROM usuario WHERE email='$email'");
                $saber = mysql_fetch_assoc($saberr);
                $nome = $saber['nome']." ".$saber['sobrenome'];
                $id = $pub['id'];
                
                if($pub['imagem']==""){
                    echo '<div class="pub" id="'.$id.'">
                            <p><a href="#">'.$nome.'</a> - '.$pub["data"].'</p>
                            <span>'.$pub['texto'].'</span><br /> </div>';
                }else{
                    echo '<div class="pub" id="'.$id.'">
                            <p><a href="#">'.$nome.'</a> - '.$pub["data"].'</p>
                            <span>'.$pub['texto'].'</span>'
                            . '<img src="upload/'.$pub["imagem"].'" /> </div>';
                }
            }
        ?>
        <br>
        <div id="footer"><p>&copy;CTRL-ALT-DEL - Direitos reservados.</p></div>
    </body>
</html>

    
asked by anonymous 18.11.2016 / 00:32

1 answer

2

You are doing the select at the beginning of the code, before inserting the new post, so the new post does not even have to appear, because when it is inserted, you have already picked up the "old"

The solution is to get the line marked here:

<?php
    include("header.php");

    // TIRE ESSA LINHA DAQUI
    // $pubs = mysql_query("SELECT * FROM publicacao ORDER BY id desc");

    if (isset($_POST['publish'])){
        if($_FILES["file"]["error"] > 0){

And move on to this part (see the comment indicating the place):

                <input type="file" id="file-input" name="file" hidden />
            </form>
        </div>
        <?php

            // E PASSE PRA CÁ:
            $pubs = mysql_query("SELECT * FROM publicacao ORDER BY id desc");

            while ($pub = mysql_fetch_assoc($pubs)){
                $email = $pub['usuario'];

                .... resto do código ...
    
18.11.2016 / 02:46