-2
I am creating a classifieds page, I have the login and registration ready And when the user logs in takes the page to publish ad!
When the person publishes the ad I am displaying that ad next to it with a delete and edit button.
My problem is this, that I am returning all the ads and just want to show the ads of the person who is logged in! In other words, that person makes the ad, and that ad returns to him but only his ad and no one else can see that ad! And she can edit and delete, but this part I’m going to do, I just want you to give me the solution to return the ad from the user who made it.
I think you know how a user panel of a classified system works. Each user should only see the posts he has made!
The way I have I’m only managing to return all the ads in the table and everyone is seeing regardless of who is logged in. And it wouldn’t be the right way.
Page code post ad! I have her in this situation so far!
if(empty($_SESSION['active'])){
header('location: index.php');
}else{
if(!empty($_POST)){
if(empty($_POST['titulo']) || empty($_POST['descricao']) || empty($_POST['valor'])){
echo "<script>alert('Llene todos los campos');</script>";
}else{
include_once "config.php";
$nom = $_REQUEST['txtnom'];
$foto = $_FILES['foto']['name'];
$ruta = $_FILES['foto']['tmp_name'];
$destino = "images/".$foto;
copy($ruta,$destino);
$id_usuario = $_SESSION['id'];
$titulo = $_POST['titulo'];
$descricao = $_POST['descricao'];
$valor = $_POST['valor'];
$telefone = $_SESSION['telefone'];
$result_anuncios = mysqli_query($connect, "INSERT INTO anuncios(id_usuario, nome_foto, foto, titulo, descricao, valor, telefone,hora) VALUES('$id_usuario', '$nom', '$destino', '$titulo', ' $descricao', '$valor','$telefone', now())");
if($result_anuncios){
echo "<script>alert('sucesso');</script>";
}else{
echo "<script>alert('Erro:');</script>";
}
}
}
}
?>
<!DOCTYPE html>
<html lang="es">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css" integrity="sha384-Vkoo8x4CGsO3+Hhxv8T/Q5PaXtkKtu6ug5TOeNV6gBiFeWPGFN9MuhOf23Q9Ifjh" crossorigin="anonymous">
<link rel="stylesheet" href="css/publicarAnuncio.css">
<title>Anunciar</title>
</head>
<body>
<header>
<div class="container" >
<div class="row">
<div class="col-md-8" style="margin-top:12px;">
<h1>ListoPo</h1>
<span class="user">Bem vindo <?php echo $_SESSION['nome']; ?></span>
</div>
<div class="col-md-4" style="margin-top:2%; text-align:left;">
<div class="btnInicio" >
<button type="button" class="btn btn-danger"><a style="color:white;" href="sair.php">Sair</a></button>
</div>
</div>
</div>
</div>
</header>
<div class="modal" tabindex="-1" role="dialog" id="anuncio">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title">Publicar anuncio</h5>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<div class="container">
<form action="" method="POST" enctype="multipart/form-data">
<div class="form-group">
<input type="hidden" class="form-control" id="exampleInputtitulo1" aria-describedby="emailHelp" name="id_usuario">
</div>
<div class="form-group">
<label for="titulo" style="margin-top:2%;">Titulo</label>
<input type="text" class="form-control" id="exampleInputtitulo1" aria-describedby="emailHelp" name="titulo">
</div>
<div class="form-group">
<label for="descricao" style="margin-top:2%;">Descrição</label>
<input type="text" class="form-control" id="exampleInputdescricao1" aria-describedby="emailHelp" name="descricao">
</div>
<div class="form-group">
<label for="descricao" style="margin-top:2%;">Nome anuncio</label>
<input type="text" class="form-control" id="exampleInputdescricao1" aria-describedby="emailHelp" name="txtnom">
<input type="file" style="padding-bottom:8%;" class="form-control" id="exampleInputdescricao1" aria-describedby="emailHelp" name="foto">
</div>
<div class="form-group">
<label for="valor" style="margin-top:2%;">$ Valor</label>
<input type="text" class="form-control" id="exampleInputdescricao1" aria-describedby="emailHelp" name="valor">
</div>
<button type="submit" class="btn btn-primary" style="margin-bottom:2%;">Publicar</button>
</form></div>
</div>
</div>
</div>
<div class="container" style="margin-top:8%; ">
<div class="row">
<div class="col-4"><h3>Publica seu anuncio</h3>
<button type="button" class="btn btn-success" data-toggle="modal" data-target="#anuncio" style="margin-top:8%;">Publicar anuncio</button>
</div>
<div class="col-8">
<div class="card mb-3" style="max-width: 600px; text-align:center; border:none; margin-top:-10%; margin-left:25%;">
<div class="row no-gutters">
<div class="col-md-4">
<?php
require "config.php";
$sql = mysqli_query($connect, "SELECT * FROM anuncios");
while($res=mysqli_fetch_array($sql)){
echo '<img style="margin-top:5%;" src ="'.$res["foto"].'" width="195px" heigth="100px">';
echo "<div class='titulo' style='text-align:center; margin-top:-1%; '><h3> " . $res['titulo']. "</h3></div>";
echo "<div style='text-align:center; margin-top:-1%;' ><p>" .$res['descricao']. "</p></div>";
echo "<div style='text-align:center; margin-top:-8%;' ><p> $" .$res['valor']. "</p></div>";
echo "<div style='text-align:center; ' ><p>" .$res['hora']. "</p></div>";
echo "<button type='button' class='btn btn-primary'>Editar</button>";
echo "<button type='button' class='btn btn-danger'>Deletar</button>";
}
?>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
Please limit the code to the specific part of the problem.
– MagicHat