I am, trying to write a PHP-Mysql code which fetches the important places in a city and displays the results as links, when i click on the city it should display all the details like name, place, address, phone number etc, but in this case results are not being shown.

this is my code, table name is table1, the results(links) are being displayed on my home page, but when i click on the links details are not being displayed, please help!
Am fetching data from database and the results are created as URL's dynamically but when i click on the URLs no data is being shown.

<?php
include ('connectdb.php');
$query = "SELECT type, name, location, landmark FROM table1";
$result = @mysql_query($query);
echo mysql_error();
if ($result) {
while($arr = mysql_fetch_array($result, MYSQL_ASSOC)){
$resultlink = "\n\t<li><a href=\"posts.php?ID={$arr['id']}+{$arr['type']}+{$arr['name']}\">{$arr['name']} {$arr['location']}</a></li>";
            $end_result    .= '<li>' . $resultlink . '</li>';
$resultlink  .=     "
    \n<p>Name: {$arr['name']}</p>
    \n<p>Type: {$arr['type']}</p>
    \n<p>Location: {$arr['location']}</p>";
        }
        echo $end_result;
    }  else {
        echo "<p>Sorry no results for $word</p>";
        }
?>

Recommended Answers

All 10 Replies

Member Avatar for diafol

You see to be sending the user to posts.php on link click. But this is supposed to happen on the home page?

Where is $end_result set initally (line 12)?

Also where are the 'ul' tags?

Could you please rephrase your problem. I'm a little confused, and as you've not had a reply for 9 hours, I suspect I'm not the only one.

Hi Ardav, thanks for your concern.
Yes this will happen on the home page, I have included the posts.php on my home page.
yes $end_result is set at line 12.

My problem is that my links are displayed correctly on the home page, but when i click on the links Instead of displaying the values like Name, Place and Location from the database, no values are displayed. Instead only the link is opened in resulting.

Hope you got my problem now?

Member Avatar for diafol

Sorry ramganesht, I'm tied up at the moment, won't be able to get back to you until tomorrow night (i have to think!). In the meantime, anybody else out there?

have you tested the Database connection if it is good to go?

Try this on line 9: if ($result >= 1)

Hi,

Try changing this

 $query = "SELECT type, name, location, landmark FROM table1";

to this

$query = "SELECT id, type, name, location, landmark FROM table1";

reason? because you have id in here..

 $resultlink = "\n\t<li><a href=\"posts.php?ID={$arr['id']}+{$arr['type']}+{$arr['name']}\">{$arr['name']} {$arr['location']}</a></li>";
  $end_result .= '<li>' . $resultlink . '</li>';

and then add this on top of the while loop.. do not include inside the while loop..

 $end_result = "";

one last thing, remove your error suppressor in

 @mysql_query($query);

remove the suppressor @ for now, or until you are sure your script working properly.. some books talks about how to suppress errors, but this is not the environment to suppress them..we want errors to appear as much as possible while in developement. use this instead..

  mysql_query($query);
I have updated by what you have said and the URLs are displayed correctly. But when I click on the Url no information is being displayed.
When I click on the link I need an out put as shown below:
Name:      <name from database>
Type:       <type from database>
Location: <Location from database>

Pasted the updated code below, please let me know if you understamd my problem :-(

    <?php
    include ('connectdb.php');
    $query = "SELECT id, type, name, location, landmark FROM table1";
    $result = mysql_query($query);
    echo mysql_error();
    if ($result) {
    $end_result = "";
    while($arr = mysql_fetch_array($result, MYSQL_ASSOC)){
    $resultlink = "\n\t<li><a href=\"posts.php?ID={$arr['id']}+{$arr['type']}+{$arr['name']}\">{$arr['name']} {$arr['location']}</a></li>";
                $end_result    .= '<li>' . $resultlink . '</li>';
    $resultlink  .=     "
        \n<p>Name: {$arr['name']}</p>
        \n<p>Type: {$arr['type']}</p>
        \n<p>Location: {$arr['location']}</p>";
            }
            echo $end_result;
        }  else {
            echo "<p>Sorry no results for $word</p>";
            }
    ?>

Change this

 $resultlink = "\n\t<li><a href=\"posts.php?ID={$arr['id']}+{$arr['type']}+{$arr['name']}\">{$arr['name']} {$arr['location']}</a></li>";

to this...

 $resultlink = "\n\t<li><a href=\"posts.php?id={$arr['id']}&type={$arr['type']}&name={$arr['name']}&loc={$arr['location']}\">{$arr['name']} {$arr['location']}</a></li>";

and then on your post.php, you can do like this

   $id = $_GET['id'];
   $location = $_GET['location'];

   ## do the same for others then echo them the way you want it to be showing on the browser.

I have added this piece of code and this displays the the values directly on the page, but i what want the values only when i click on the link.
How shall i modify and where shall i write the above statements

now the links are displayed correctly, but when i click on the link again all the links are being displayed instead of on the values like location, name etc..

Member Avatar for diafol

I'm confused as hell. Which thread are we supposed to be replying to? You currently have 3 on the go, all with the same sort of issue. Perhaps if you go to the other two and post a link to this one, or any combination of those. This needs to be kept organised.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.