H
H
HeartOfProgrammer2015-08-12 13:24:02
PHP
HeartOfProgrammer, 2015-08-12 13:24:02

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>';
  }

My database and tables:
246e20cb845345b6b62b811c51cb7bb6.png

Answer the question

In order to leave comments, you need to log in

4 answer(s)
S
Sergey Petrochenko, 2015-08-12
@HeartOfProgrammer

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>';

S
Sergey Ivanov, 2015-08-12
@Writerim

The question is not entirely clear. Specify what problem you would like to solve.
Wrong redirect?
compose a SQL query?

H
heartdevil, 2015-08-12
@heartdevil

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.

S
Sergey Okhonko, 2015-08-12
@sersergei

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 question

Ask a Question

731 491 924 answers to any question