1

if my question appears to be silly but I ran out of explanations. I'm trying to set up a page with some news on it. I have the database with such attributes like News_id, news_header, news body etc.

I have 2 pages first page gives a list of the brief descriptions of the news with the button which allows user to read some more of a particular news on it. If clicked it passes a corresponding news_id value to the page 2 which fetches News_Id Value, queries corresponding values from database value and outputs the content.

The problem is that no mater which button in the list of page 1 I click to see news, the page 2 always receives the news_id value of the news which is *first in the lin*e. here are the codes

PAGE ONE (the list)

<form name='NewsLineSelection' method='post' action='news.php'>
<?php
 $News_Query = "Select * from news order by Date_Posted Desc;";
 $GetNews = mysql_query( $News_Query, $IVE_Connection ) or die("ERROR: ".mysql_error());

while ( $News_Database = mysql_fetch_array( $GetNews ) ){                   
?>
<tr><td ><?php echo $News_Database[1]; ?></td>//header
<tr><td ><?php echo $News_Database[2]; ?></td>//news body
...etc

<tr><td class="maintext">
<input type='hidden' name='NewsToRead' value='<?php echo $News_Database[0]; ?>'>//News_ID is here
<input type='submit' name='AddNews' value='Read More...' >
</td></tr>
 }//end of the loop

Page 2 (with info details)

$News_id = addslashes($_POST['NewsToRead']);

$News_Query = "Select * from news where news_id = ".$News_id.";";
$GetNews = mysql_query( $News_Query, $Connection ) or die("ERROR: ".mysql_error());
$News_Database = mysql_fetch_row( $GetNews );

//Then output the query's content

It looks perfect on paper. But I really can't understand why if there are 10 news in the list of page 1 for instance, no matter which button I click the value of $News_id in PAGE 2 will be always the id of the first news in the list on the page 1.

May be I don't see something. it suppose to work ok, but it doesn't.

Thanks for any kind of help or suggestions.

meks
  • 777
  • 3
  • 12
  • 23
  • 1
    Welcome to Stack Overflow! [**Please, don't use `mysql_*` functions in new code**](http://bit.ly/phpmsql). They are no longer maintained [and are officially deprecated](https://wiki.php.net/rfc/mysql_deprecation). See the [**red box**](http://j.mp/Te9zIL)? Learn about [*prepared statements*](http://j.mp/T9hLWi) instead, and use [PDO](http://php.net/pdo) or [MySQLi](http://php.net/mysqli) - [this article](http://j.mp/QEx8IB) will help you decide which. If you choose PDO, [here is a good tutorial](http://j.mp/PoWehJ). Also, don't use tables for laying out web pages. – Madara's Ghost Dec 29 '12 at 09:23
  • yes the if the value of the first news is 1 nomatter what I click page 2 always receives 1 and therefore outputs the wrong content – meks Dec 29 '12 at 09:26
  • yes but there is the loop as you can see. raw suppose to have a new values. and the headers and body texts are actually different. – meks Dec 29 '12 at 09:27

1 Answers1

5

That's because you're having multiple inputs with the same name. You're looking at the problem wrong.

You're fetching (getting) data, you should be using a GET request, and not a POST request.

My recommendation, that each News headline would be wrapped in a link like so:

<h1>
    <a href="news.php?id=<?php //Code to output the correct ID here ?>"><?php //Code to output the correct headline here ?>
    </a>
</h1>

A few things extra

Community
  • 1
  • 1
Madara's Ghost
  • 172,118
  • 50
  • 264
  • 308
  • do you mean I have to replace the button with a link? – meks Dec 29 '12 at 09:34
  • @meks: I mean, don't use forms for something that doesn't need to be a form. (Or a table for something that doesn't need to be a table for that matter) – Madara's Ghost Dec 29 '12 at 09:36
  • Ive just tried to change the value of the "NewsToRead' from hidden to text and in the page 1 it outputs the correct Id for each news. What I can't understand why on earth the value of 'NewsToRead' in the page 2 is always 1??????????? – meks Dec 29 '12 at 09:45
  • @meks: Like I said, it's because you have a lot of hidden inputs with the same **name**. You shouldn't be having a form at all in this case. This isn't something user inputs and submits. You want a link from page to page, so use a link! – Madara's Ghost Dec 29 '12 at 09:47
  • Dude and how can I get the value in the PAGE 2 if I just use the link? – meks Dec 29 '12 at 09:53
  • @meks: Please learn about the difference between $_GET variables and $_POST variables. – Madara's Ghost Dec 29 '12 at 14:16