1
Hello, guys! I’m developing a social network and I’m in the publishing section. The problem is that when I go to make a new post and click to publish, it publishes the previous post and when I update the page is that it publishes the post that I did. Since I’m new to PHP, I think it has to do with the browser cookie but I don’t know how to solve it. Anyway, 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>©CTRL-ALT-DEL - Direitos reservados.</p></div>
</body>
</html>
What you said worked here, there’s only one problem now: when I update the page, it answers the last message I posted only that, before gave update, comes that browser dialog box talking that will resend the form. Would it be possible to avoid this or is it something from the same browser?
– André Hugo
There is another problem, is the fact that you try to update a POST. The ideal would be to separate the Phps, one receiving the post and recording, and redirecting the user to the listing page. Already have a post talking about it on the site, if I find link put here.
– Bacco
It is not a question with answers, but there are already some tips in the comments: http://answall.com/questions/160560/70
– Bacco
in short, you make a form action="post.php", and in post.php you only write to DB, nothing else, and at the end of a
header( 'Location: /listagem.php?'.uniqid() );
so, if the person gives refresh, will only give in the listing. Theuniqid()
was an example, just so the page doesn’t cache and doesn’t update.– Bacco