0

I have created a simple search function which looks through a few fields based on whatever the user inputs. It then returns the results as hyperlinks just displaying the titles of the schools. I am trying to then allow the user to view the rest of the information on a separate page. I have given each record a unique ID but I'm struggling to work out how to call it on the desired page. Here is the code i have used for the search results page search.php

<?php
   require_once 'connection.inc.php';

   if(isset($_GET['keywords'])){
   $keywords = $db->escape_string($_GET['keywords']);
   $query = $db->query("
    SELECT school, address_town
    FROM School
    WHERE school LIKE '%{$keywords}%'
    OR address_town LIKE '%{$keywords}%'
     ");
   ?>
     <div class ="result-count">
      Found <?php echo $query->num_rows; ?> results
      <br>
      <?php 
        if ($query->num_rows == 0)
        echo "Sory there were no results matching your search" ;{
        }
      ?>
    </div>
<?php

if($query->num_rows){
    while($r = $query->fetch_object()){
    ?>

    <div class ="result">
        <a href='school.php?id=".$row['id']."'><?php echo $r-> school; ?></a>
    </div>
    <?php
    }
}

Here is the code for the page where i would like to display all of the information that i have on that particular record. This page is school.php

<?php
 include ('connection.inc.php');
 connect();

 $sql = "SELECT * FROM School WHERE id = ".$_GET['id'];
 $result = mysql_query($sql);
 ?>

 <!doctype html>
  <html>
    <head>
        <meta charset="utf-8">
            <title>School Details</title>  
    </head>
    <body>
        <?php
            while ($record = mysql_fetch_object($result))
         {
        ?>
        <div class="post">
          <p class="school"><? echo $record->school; ?></p>
          <p class="address_1"><? echo $record->address_1; ?></p>
          <p class="address_town"><? echo $record->address_town; ?></p>
          <p class="postcode"><? echo $record->postcode; ?></p>
          <p class="website"><? echo $record->website; ?></p>
          <p class="phone"><? echo $record->phone; ?></p>
          <p class="council"><? echo $record->council; ?></p>
          <p class="pupils"><? echo $record->pupils; ?></p>
          <p class="age_range"><? echo $record->age_range; ?></p>
          <p class="gender"><? echo $record->gender; ?></p>
          <p class="school_type"><? echo $record->school_type; ?></p>
          <p class="ofsted_rating"><? echo $record->ofsted_rating; ?></p>
          <p class="ofsted_report"><? echo $record->ofsted_report; ?></p>
          <p class="gcse_percentage"><? echo $record->gcse_percentage; ?></p>
          <p class="points_score"><? echo $record->points_score; ?></p>
          <p class="pupil_teacher_ratio"><? echo $record->pupil_teacher_ratio; ?></p>
          <p class="absence_percentage"><? echo $record->absence_percentage; ?></p>
          <p class="language_not_english"><? echo $record->language_not_english; ?></p>
          <p class="free_school_meals"><? echo $record->free_school_meals; ?></p>
          <p class="happiness"><? echo $record->happiness; ?></p>
          <p class="ofsted_parent_review"><? echo $record->ofsted_parent_review; ?></p>
          <p class="events"><? echo $record->events; ?></p>
          <p class="school_description"><? echo $record->school_description; ?></p>
        <?php
        }
         mysql_free_result($result);
         mysql_close();
        ?>  
    </body>
</html>

If anybody could help that would be great, Im quite new to this and picking it up as I go.

Thanks

  • Welcome to SO! You are going to get a lot of comments about your code, and it would be in your best interest to receive them well. They are from people who are expert, and trying to save you from making serious mistakes in your code. Here's the first comment for you to consider: Your code is super vulnerable to [SQL Injection attacks](http://stackoverflow.com/questions/60174/how-can-i-prevent-sql-injection-in-php). Please STOP using `mysql_`, and instead use PDO or mysqli, and *use binding*. – random_user_name Jan 23 '17 at 17:58
  • Second comment: What exactly is the problem? You cannot generate the links? Or the links don't work? Or the "details" page doesn't show what you expect? Please read [how to create a minimal, complete, and verifiable example](http://stackoverflow.com/help/mcve). – random_user_name Jan 23 '17 at 18:00
  • Apologies, i had been working on this for quite sometime and didn't realise that I hadn't really made myself very clear. The project is just an educational one that will never be published although protecting against SQL injection attacks is something that I am aware of. – A Treddenick Jan 24 '17 at 12:18
  • My problem is that when i click on the link it does not take me to a page that contains all of the information for that particular record. I have used two different tutorials and i believe that I'm getting really confused by the two. Is it possible the way that I'm connecting to the database is wrong, for instance one uses a require_once method and the other just includes the connection page then uses the method connect(); Any advice would be great – A Treddenick Jan 24 '17 at 12:27

2 Answers2

0

You should add the target attribute in the anchor tag with the value _blank.

For example:

<a target="_blank" href="http://your_url_here.html">Link</a>

In your case:

<a target="_blank" href='add_your_base_url_here/school.php?id=".$row['id']."'><?php echo $r-> school; ?></a>

I hope that helps, cheers!

sT0n3
  • 125
  • 2
  • 13
  • Thank you for your response although I'm still slightly confused, how would i dynamically populate the target page with the information for that record that the user has clicked on? – A Treddenick Jan 24 '17 at 12:21
0

Thank you for your help, i have managed to get what i was after using the following on a page called search.php

<?php

require 'connection.inc.php';

if(isset($_GET['keywords'])){

$keywords = $db->escape_string($_GET['keywords']);

$query = $db->query("
    SELECT school, address_town, id
    FROM School
    WHERE school LIKE '%{$keywords}%'
    OR address_town LIKE '%{$keywords}%'
    OR council LIKE '%{$keywords}%'
    OR postcode LIKE '%{$keywords}%'") or die($db->error);?>

 <div class ="result-count">
    Found <?php echo $query->num_rows; ?> results
    <br>
    <?php 
        if ($query->num_rows == 0)
        echo "Sorry there were no results matching your search" ;{
        }
    ?></div>
<?php

if($query->num_rows){
    while($r = $query->fetch_object()){
    ?>
    <div class ="result">
        <a href="school.php?id=<?php echo $r->id;?>"><?php echo $r->school; ?></a>
    </div>
    <?php
    }
}

}

and on the page that i need to populate the information for the particular record that the user has selected i used the following code on a page called school.php

<?php
require 'connection.inc.php';
$id = $_GET['id'];

?>

<!DOCTYPE html>
<html>
   <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title><</title>
       <link rel="stylesheet" type="text/css" href="style.css" />
   </head>
   <body>
      <?php

if($row = $db->query("SELECT * FROM School WHERE id = $id")) {
if($count = $row->num_rows) {
    echo '<p>', '</p>';

    while ($row = $row->fetch_object()) {?>

        <div class="all_details">
          <div class="school"><?php echo $row->school;?></div>
          <div class="address_1"><? echo $row->address_1; ?></div>
          <div class="address_town"><?php echo $row->address_town;?></div>
          <div class="postcode"><? echo $row->postcode; ?></div>
          <div class="website"><? echo $row->website; ?></div>
          <div class="phone"><? echo $row->phone; ?></div>
          <div class="council"><? echo $row->council; ?></div>
          <div class="pupils"><? echo $row->pupils; ?></div>
          <div class="age_range"><? echo $row->age_range; ?></div>
          <div class="gender"><? echo $row->gender; ?></div>
          <div class="school_type"><? echo $row->school_type; ?></div>
          <div class="ofsted_rating"><? echo $row->ofsted_rating; ?></div>
          <div class="ofsted_report"><? echo $row->ofsted_report; ?></div>
          <div class="gcse_percentage"><? echo $row->gcse_percentage; ?></div>
          <div class="points_score"><? echo $row->points_score; ?></div>
          <div class="pupil_teacher_ratio"><? echo $row->pupil_teacher_ratio; ?></div>
          <div class="absence_percentage"><? echo $row->absence_percentage; ?></div>
          <div class="language_not_english"><? echo $row->language_not_english; ?></div>
          <div class="free_school_meals"><? echo $row->free_school_meals; ?></div>
          <div class="happiness"><? echo $row->happiness; ?></div>
          <div class="ofsted_parent_review"><? echo $row->ofsted_parent_review; ?></div>
          <div class="events"><? echo $row->events; ?></div>
          <div class="school_description"><? echo $row->school_description; ?></div>
    <?php
    }
}
}

?>        
</body>