0

ive just started PHP and MYSQL, and been trying to figure out how i can redirect to a page and display the content.

i have this PHP code that lists all my topic rows from my database

while ($row = $result->fetch_array()) {
    echo '<a href="#"><p class="list" st>' . $row['topic'] . '<br /></a>';
    }

it will list all the topics inside the <a href> markup.

when link is clicked, i will be redirected to a page where the content is displayed based on the topic link.

i just need the logic how to create that page. basically i have page.php which i plan to put the logic. but i dont know how to start.

bobbyjones
  • 2,029
  • 9
  • 28
  • 47

3 Answers3

2

I suggest to use GET method to send ID of content to your page.php , url should be something like this :

  http://site.com/page.php?id=120

And in your page.php get the id using $_GET['id'],i admit you have content in database , so you will display data based on this id.

PHP code :

   while ($row = $result->fetch_array()) {
      echo '<a href="page.php?id='.$id.'"><p class="list" st>' . $row['topic'] . '<br /></a>';
    }

An important thing , pay attention to sql injection especially when you get id.

Useful links about protecting from SQL injection:

-How can I prevent SQL injection in PHP?

-What's the best method for sanitizing user input with PHP?

-Protect against SQL injection

Community
  • 1
  • 1
Charaf JRA
  • 8,249
  • 1
  • 34
  • 44
  • oh, so thats how you do it.. so i use GET to identify the ID and display the appropriate content for that ID. how clever :) thanks. also, sorry for my ignorance, if i use GET, how do i protect myself from those sql injections? – bobbyjones Sep 11 '13 at 14:43
0

Essentially each link is going to need to carry a piece of information to page.php in order to tell that page what content to display. This is likely going to be some sort of identifier for the specific record. Something like this:

echo '<a href="page.php?id=' . $row['ID'] . '"><p class="list" st>' . $row['topic'] . '<br /></a>';

This will make each link unique with an id parameter. Then, in the code on page.php you would check for that parameter as a query string value, which would be available here:

$_GET['id']

Remember that users can spoof this value and essentially put whatever they want into that link, so make sure it's a valid ID and make sure you don't use it directly in a database query without sanitizing the input first, otherwise users will have an open door to your database. (Read up on SQL Injection for more information.)

With that value, page.php will query the database for the one record which matches that ID and display the data from that record accordingly.

David
  • 208,112
  • 36
  • 198
  • 279
0
while ($row = $result->fetch_array()) {
echo '<a href="page.php?topic="'.$row['topic'].'><p class="list" st>' . $row['topic'] . '<br /></a>';
}

in the page.php make functions of one topic each and call that respective function for which the request has come

bhawin
  • 267
  • 1
  • 6
  • 15