Doesn’t just open the post by id

Asked

Viewed 38 times

2

Can someone give us a little help, please? When I press on a blog post, I should just show that post on another page. But I’m not being able to show this post. For everyone I open, it’s always showing the same information and the same photo.

index php.

<?php require_once('conn.php');

$consulta_categoria = mysql_query("SELECT Posts.titulo_principal, 
       Posts.data, 
       Posts.descricao,
       Categorias.categoria,
       Posts.texto_inicial,
       Posts.texto_secundario,
       Posts.titulo_principal,
       Posts.imagem1,
       Posts.imagem1_dir,
       Posts.imagem2,
       Posts.imagem2_dir
FROM Posts
INNER JOIN Categorias ON Categorias.id = Posts.categoria_id ORDER BY Posts.id DESC");
$linha_categoria = mysql_fetch_assoc($consulta_categoria);

$consulta_menu = "SELECT id, categoria FROM categorias order by categoria asc";
$resultado_menu = mysql_query($consulta_menu, $conn) or die(mysql_error());
$linhas_menu = mysql_fetch_assoc($resultado_menu);

$consulta = "SELECT * FROM posts ORDER BY id DESC";
$resultado = mysql_query($consulta, $conn) or die(mysqli_error());
$linhas = mysql_fetch_assoc($resultado);

$consulta_recentes = "SELECT * FROM posts ORDER BY id DESC LIMIT 3";
$resultado_recentes = mysql_query($consulta_recentes, $conn) or die(mysqli_error());
$linhas_recentes = mysql_fetch_assoc($resultado_recentes);

?>


<div class="blog-post">
                    <?php do {  ?>

                    <div class="post-thumb">
                        <a href="#"><img src="Admin/app/webroot/files/post/imagem1/<?php echo $linha_categoria['imagem1_dir']; ?>/<?php echo $linha_categoria['imagem1']; ?>"></a>
                        <div class="hover-wrap">
                        </div>
                    </div>


                    <div class="post-content">

                        <h6 class="post-title"><a href="post-detalhe.php?idpost=<?php echo $linhas['id'] ?>"><?php echo utf8_encode($linha_categoria['titulo_principal']); ?></a></h6>
                        <div class="meta">
                            <span class="meta-part"><a href="#"><i class="fas fa-align-justify"></i></i><?php echo utf8_encode($linha_categoria['categoria']); ?></a></span>
                            <span class="meta-part"><a href="#"><i class="fas fa-clock"></i> <?php echo utf8_encode($linha_categoria['data']); ?></a></span>
                        </div>
                        <p><?php echo utf8_encode($linha_categoria['descricao']); ?></p></p>
                        <a href="post-detalhe.php?idpost=<?php echo $linhas['id'] ?>" class="btn btn-common btn-rm">Leia Mais</a>
                    </div>

                <?php } while ($linha_categoria = mysql_fetch_assoc($consulta_categoria));  ?>

                </div>

post-detail.php

<?php require_once('conn.php');

$id = (int)$_GET['idpost'];
$resultado_post = mysql_query('SELECT * FROM posts WHERE id = ' . $id, $conn);
$linhas_post = mysql_fetch_assoc($resultado_post);

$consulta_menu = "SELECT id, categoria FROM categorias order by categoria asc";
$resultado_menu = mysql_query($consulta_menu, $conn) or die(mysql_error());
$linhas_menu = mysql_fetch_assoc($resultado_menu);

?>

Below is the code where it shows the post information, on the post-detail page.php:

<div class="col-lg-8 col-md-12 col-xs-12">

                <div class="blog-post single-post">

                    <div class="post-thumb">
                        <a href="#"><img src="Admin/app/webroot/files/post/imagem1/<?php echo $linhas_post['imagem1_dir']; ?>/<?php echo $linhas_post['imagem1']; ?>"></a>
                        <div class="hover-wrap">
                        </div>
                    </div>


                    <div class="post-content">
                        <h4 class="post-title"><a href="#"><?php echo utf8_encode($linhas_post['titulo_principal']); ?></a></h4>
                        <div class="meta">
                            <span class="meta-part"><a href="#"><i class="fas fa-align-justify"></i> <?php echo $linhas_post['categoria']; ?></a></span>
                            <span class="meta-part"><a href="#"><i class="fas fa-clock"></i> <?php echo utf8_encode($linhas_post['data']); ?></a></span>
                        </div>
                        <p><?php echo utf8_encode($linhas_post['texto_inicial']); ?></p>
                        <br>

                        <div class="row">
                            <div class="col-sm-12">
                                <img src="Admin/app/webroot/files/post/imagem2/<?php echo $linhas_post['imagem2_dir']; ?>/<?php echo $linhas_post['imagem2']; ?>">
                            </div>
                        </div>
                        <br>
                        <p><?php echo utf8_encode($linhas_post['texto_secundario']); ?></p>
                    </div>
                </div>
            </div>

My code must be wrong, but I don’t know where I’m going wrong. Someone to help? Thank you!

  • 1

    For these codes, there seems to be no problem. Is the id number really changing in each record? See if every time you click on a post in "read more" the id is changed in the url.

  • Hi Andrei, I just saw here and the id is not changing. It is always repeating the same id, IE, is repeating the last id registered in the database. How do I show the right id and the post of that id?

  • 1

    Put the code you generate that list there in the question. I see if I find anything...

  • There in the index file there is only one part of the code... the problem is there...

  • Okay, I think that’s it. I changed the index.php

  • Is there the answer!

Show 1 more comment

1 answer

1


The solution found by OP itself was that in select there was no id field.

Use the variable that was created in the loop $linha_categoria, starting with him:

<div class="blog-post">
        <?php while ($linha_categoria = mysql_fetch_assoc($consulta_categoria)) {  ?>

        <div class="post-thumb">
            <a href="#"><img src="Admin/app/webroot/files/post/imagem1/<?php echo $linha_categoria['imagem1_dir']; ?>/<?php echo $linha_categoria['imagem1']; ?>"></a>
            <div class="hover-wrap">
            </div>
        </div>


        <div class="post-content">

            <h6 class="post-title"><a href="post-detalhe.php?idpost=<?php echo $linha_categoria['id'] ?>"><?php echo utf8_encode($linha_categoria['titulo_principal']); ?></a></h6>
            <div class="meta">
                <span class="meta-part"><a href="#"><i class="fas fa-align-justify"></i></i><?php echo utf8_encode($linha_categoria['categoria']); ?></a></span>
                <span class="meta-part"><a href="#"><i class="fas fa-clock"></i> <?php echo utf8_encode($linha_categoria['data']); ?></a></span>
            </div>
            <p><?php echo utf8_encode($linha_categoria['descricao']); ?></p></p>
            <a href="post-detalhe.php?idpost=<?php echo $linha_categoria['id'] ?>" class="btn btn-common btn-rm">Leia Mais</a>
        </div>

    <?php }  ?>

</div>

With these changes your code should work as expected.

A tip...

Force the integer to id so:

$id = (int)$_GET['idpost'];

This can help you avoid injections in the bank.

  • Even using $line_category, it still opens only the last id. http://localhost/Classic/post-detail.php? idpost=11 Only pull id 11, which is the last id registered.

  • @Márciaprates Put SELECT to the question.

  • I put all the selects.

  • This, that when press Learn more, show the post of that id.

  • @Márciaprates made another issue... I found all the mistakes I think. See there.

  • 1

    I’ll see if it works. Thank you for helping me.

  • 1

    @Márciaprates I made one more change... it seems that Categorias is also with minuscule letter.

  • I did it. Do you think it was my fault? The line of the select in index.php $consulta_category = mysql_query("SELECT Posts.maintitulo_, missed id. That’s why it showed the same id for all posts.

  • 1

    @Márciaprates happens! =)

  • Thank you so much for your help and your willingness. And I’m sorry to bother you.

  • 1

    @Márciaprates without problems! =)

Show 6 more comments

Browser other questions tagged

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