-1

I am trying to make a forum-like website from scratch and this is the first big problem that I encountered so far, I am trying to make an individual link with ?info[id] for each ticket/topic but I simply can't, no matter what id I put in the url, I see all of them instead of the specific one, you have all the code in the video.

Video Link

My Details.php Code

<?php
require('Includes/Header.php');

$query = "SELECT * FROM forum"; 
$result = mysqli_query($conn ,$query);


?>

<?php while($row = mysqli_fetch_array($result)) {?> 
<div class="FormAfter">
            <label>ID</label><br><br>
            <span><?php echo htmlspecialchars($row['id']); ?></span><br><br>
            <label>Titlu</label><br><br>
            <span name="Titlu"><?php echo htmlspecialchars($row['titlu']);?></span><br><br>
            <label>Categorie</label><br><br>
            <span><?php echo htmlspecialchars($row['categorie']); ?></span><br><br>
            <label>Descriere</label><br><br>
            <span name="Descriere" cols="30" rows="10" readonly><?php echo htmlspecialchars($row['descriere']);?></span>
    </div>

<?php
}
mysqli_close($conn);
?>

This is my Topics.php code

<?php 
require('Includes/Header.php');

$query = "SELECT * FROM forum"; 
$result = mysqli_query($conn ,$query);

?> 
    <div class="TopicListBig">
        <span id="IdTitlu">ID <strong>|</strong> Titlu <strong>|</strong> Categorie</span> <br> <br> <br>
    </div>
<?php 
while($row = mysqli_fetch_array($result)){ ?>

    <div class="RandomSpan">
        <span class="TopicList"><?php echo htmlspecialchars($row['id']);  ?></span>
        <span class="TopicList"><?php echo htmlspecialchars($row['titlu']); ?></span>
        <span class="TopicList"><?php echo htmlspecialchars($row['categorie']); ?></span>
        <span class="TopicList"><a href="Details.php?id=<?php echo $row['id'];?>">Info</a></span><br><br>
    </div>

<?php
}

mysqli_close($conn);
?>
  • 1
    It's difficult to work with this information in video format. Text in your question showing how you are trying to handle the id from the url would make it much easier for everyone to help you with this. – James Clark Mar 05 '21 at 12:04

1 Answers1

0

Your SQL query is explicitly fetching all records: "SELECT * FROM forum" It's the exact same query in Details as it is in Topics and nowhere in the code for Details do you make use of the id parameter in the URL's query string.

What you're looking for in that SQL query is the WHERE keyword. For example:

SELECT * FROM forum WHERE id=?

Within your WHERE clause you identify the specific filter to find the exact record(s) you want. Then you bind your value to that parameter before executing the query. While that link shows the (generally preferred) object-oriented style, you can also use the procedural style you currently use. For example:

$query = mysqli_prepare($conn, 'SELECT * FROM forum WHERE id=?');
mysqli_stmt_bind_param($query, 's', $_GET['id']);
mysqli_stmt_execute($query);

$result = mysqli_stmt_get_result($query);
while ($row = mysqli_fetch_array($result)) {
    // your output
}
David
  • 208,112
  • 36
  • 198
  • 279