Answer the question
In order to leave comments, you need to log in
Why doesn't the query show news on a separate page?
I am doing one project, I wanted to make it so that when I click on a news item on the index.php main page, I will be redirected to the same news item, where I clicked only on another page, and with a different description, as it were.
But when I click on the title of the news on the main page, index.php redirects me to the news.php page, instead of displaying the news I clicked on, it displays all the news in the MySQL database.
I suppose the problem is in the sql query, because I already confuse these id, news id and users id, my head is already spinning.
in the news.php file, here is the code:
$news = $_GET['id'];
$mysqli = mysqli_connect('localhost', 'weltkind', '123321', 'wl_db');
$sql = "SELECT
n.id AS news_id,
n.title AS title,
n.content AS content,
u.id AS user_id
FROM news n
LEFT JOIN users u
ON n.user_id = u.id
";
$query = mysqli_query($mysqli, $sql);
while ($row = mysqli_fetch_assoc($query)) {
echo '<div>';
echo '<h1><a href="news.php?id='.$row['id'].'">'.$row['title'].'</a></h1>';
echo '<p>'.$row['content'].'</p>';
echo '</div>';
}
Answer the question
In order to leave comments, you need to log in
maybe so?)
$news = $_GET['id'];
$mysqli = mysqli_connect('localhost', 'weltkind', '123321', 'wl_db');
$sql = "SELECT
n.id AS news_id,
n.title AS title,
n.content AS content,
u.id AS user_id
FROM news n
LEFT JOIN users u
ON n.user_id = u.id
WHERE n.id = $news LIMIT 1
";
$query = mysqli_query($mysqli, $sql);
$row = array();
$row = mysqli_fetch_assoc($query);
echo '<div>';
echo '<h1><a href="news.php?id='.$row['news_id'].'">'.$row['title'].'</a></h1>';
echo '<p>'.$row['content'].'</p>';
echo '</div>';
The question is not entirely clear. Specify what problem you would like to solve.
Wrong redirect?
compose a SQL query?
Hello.
Here you read the value
but do not use it when filtering data with an sql query.
Therefore at you the list also falls out.
If the query is SQl, then add a selection condition by id (WHWRE id=$news)
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question