1

Im currently creating a news blog and I want to show small clips of the blog and let the user click "Read More" if they want find out more info (see image below)

enter image description here

On the Read More button I have it linked like this.

<a href="http://<?php echo $_SERVER['SERVER_NAME']; ?>/notices/notice.php?id=<?=$image["id"] ?>">Read More</a>

Then on the notice.php page I'm trying to import the post in like this: (data base name is knollsnews)

<?php
$result = mysql_query("SELECT ID FROM knollsnews WHERE id='$id'");
while ($image=mysql_fetch_array($images))
{
?>

I'm not sure what I doing wrong here. The problem is that the post is not showing up on the notice.php page.

Here is the full code:

<?php


$result = mysql_query("SELECT ID FROM knollsnews WHERE id='$id'");
while ($image=mysql_fetch_array($images))
{
    ?>
<li data-id="id-<?=$image["id"] ?>">
<article class="postwhite">
<h2 style="margin: 10px 0 !important;"><?=$image["title"] ?></h2>
<img alt="<?=$image["title"] ?>" src="http://<?php echo $_SERVER['SERVER_NAME']; ?>/knolls_file_manager/source/NoticesImages/<?=$image["file_name"] ?>" class="img-max" title="<?=$image["title"] ?>">
<div class="newsdate" style="margin: 10px 0 !important;"><?= date("F d, Y", strtotime($image["date"])); ?></div>
<p class="articletext"><?=$image["description"] ?></p>
</article>
</li>
    <?php
}
?>

Final Working CODE ON notice.php page

 <?php
$id = (int) $_GET['id'];
$result = mysql_query("SELECT * FROM knollsnews WHERE id='$id'");
while ($image = mysql_fetch_array($result))

{
?>

<li data-id="id-<?=$image["id"] ?>">
<article class="postwhite">
<h2 style="margin: 10px 0 !important;"><?=$image["title"] ?></h2>
<img alt="<?=$image["title"] ?>" src="http://<?php echo $_SERVER['SERVER_NAME']; ?>/knolls_file_manager/source/NoticesImages/<?=$image["file_name"] ?>" class="img-max" title="<?=$image["title"] ?>">
<div class="newsdate" style="margin: 10px 0 !important;"><?= date("F d, Y", strtotime($image["date"])); ?></div>
<p class="articletext"><?=$image["description"] ?></p>
</article>
</li>

    <?php 
}
?>
daugaard47
  • 1,726
  • 5
  • 39
  • 74

3 Answers3

1

You ned to get value from URL and set it to $id:

$id= $_GET['id'];

and after that you have to do:

$result = mysql_query("SELECT ID FROM knollsnews WHERE id='$id'");
while ($image=mysql_fetch_array($images))
{
    //do something
}
sergio
  • 5,210
  • 7
  • 24
  • 46
  • I've tried a few methods and still not getting the info on my notice.php page. Here is the code I'm currently using: (See EDIT at TOP) – daugaard47 Feb 08 '14 at 18:05
1

To get the id from the URL use $_GET['id']

LefterisL
  • 1,132
  • 3
  • 17
  • 37
1

As others pointed out, you have to use $_GET['id'] to get the id param from the URL. Also, your while loop says

$result = mysql_query("SELECT ID FROM knollsnews WHERE id='$id'");
while ($image=mysql_fetch_array($images))

when it should say

$result = mysql_query("SELECT ID FROM knollsnews WHERE id='$id'");
while ($image = mysql_fetch_array($result))

In addition to that, your code is open to SQL Injection if you do not sanitize the input or change the query to use prepared statements. At the very minimum, use

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

to make sure the id really is a number and not some malicious SQL.

Also, you are using the old mysql extension which is officially deprecated and will be removed from PHP soon. Consider using PDO or MySqli

Community
  • 1
  • 1
Gordon
  • 312,688
  • 75
  • 539
  • 559
  • Got it working. All I had to do was change. `$result = mysql_query("SELECT ID FROM knollsnews WHERE id='$id'"); while ($image = mysql_fetch_array($result))` to this: `$result = mysql_query("SELECT * FROM knollsnews WHERE id='$id'"); while ($image = mysql_fetch_array($result))` – daugaard47 Feb 08 '14 at 21:42