0

how do I display a field when I have used a GET command to get it from the address bar from the previous page and then display it in my table?

I have tried to display the field already in the table but no luck. the field is call "Reference"

using the below I want to get the reference into the table. (I have tried the following echo "" . $row['$Reference'] . "";)

http://test.com/searchresults.php?Reference=456789 $Reference=$_GET['Reference'];

search results page

<?php

require_once('auth.php');

$host=""; // Host name 
$username=""; // Mysql username
$password=""; // Mysql password 
$db_name=""; // Database name 
$tbl_name="Physio"; // Table name 

 // Connect to server and select database.
   mysql_connect($host, $username, $password)or die("cannot connect"); 
   mysql_select_db($db_name)or die("cannot select DB");

   // get value of Reference that sent from address bar
 $Reference=$_GET['Reference'];

  $sql="SELECT * FROM $tbl_name WHERE Reference='$Reference'";
 $result=mysql_query($sql);

 $rows=mysql_fetch_array($result);

    if(!isset($_POST['postcode'])) {
  header ("location:index.php");
 } 
     echo "<p> Results </p>" ;
 $search_sql = "SELECT * FROM `Physio` WHERE Postcode like '%" . $_POST['postcode'] . "%'";
$search_query = mysql_query($search_sql);

while ($search_rs = mysql_fetch_array($search_query))
{
echo"<table><tr>" ;
     echo '<th>Reference</th><th>Select</th><th>PhysioReference</th><th>Physio</th><th>Postcode</th><th>Line1</th><th>Tel</th>';
while ($search_rs = mysql_fetch_array($search_query))
     {
     echo '<tr>';
     echo "<td>" . $row['$Reference'] . "</td>";
     echo "<td><a href=\"Physiotoinstruction.php?Reference={$search_rs['Reference']}\">Select</a></td>";
     echo "<td>".$search_rs['PhysioReference'] . "</td>";
     echo "<td>".$search_rs['Name'] . "</td>";
     echo "<td>".$search_rs['Postcode'] . "</td>";
     echo "<td>".$search_rs['Line1'] . "</td>";
     echo "<td>".$search_rs['Tel'] . "</td>";
     echo '</tr>';
     }
  echo '</table>';
}
if (!empty($search_rs))
{
echo "<p> Results Found </p>" ;
}
else
{
    echo "<p> No Results Found </p>" ;
}

   ?>

3 Answers3

2

Variables will not expand when in single quotation marks, and also you're trying to access variable called $row which isn't in scope (i.e. doesn't exist at this point). The following should suffice:

$rows[$Reference]

Or change $rows to $row earlier in your code, whichever makes most sense semantically.

GMemory
  • 137
  • 9
  • same as other answer, i have tried these and they seem correct but still no data is showing, have i used the GET command correctly to enable the display the reference? – user3301611 Mar 25 '14 at 10:34
  • $row hasn't been initialized to anything at that point. $rows has, but there's no variable called $row before you try to access it. I'll update my answer to include this. – GMemory Mar 25 '14 at 10:46
  • You appear to be using $rows instead of $row. So either change $rows to $row or vice versa (considering you only appear to be fetching one result, $row would probably make the most sense) – GMemory Mar 25 '14 at 10:51
  • I might have used the wrong code here looking at it,i think i need to remove the rows. - how do i display the reference, from the $_GET['Reference']; and display it in the column of the table. many thanks in advance – user3301611 Mar 25 '14 at 11:00
  • `echo "" . $_GET['Reference'] . "";` or `echo "" . $Reference . "";` if you keep the assigned variable. – GMemory Mar 25 '14 at 11:05
  • quick question, how would I put 2 variables values into url. the following plus the reference I just put into this table "reference" --- echo "Select"; – user3301611 Mar 25 '14 at 11:16
  • GET parameters are delimited by the `&` character (and remember that `&` is a special character in HTML, so will need to be encoded as `&`) - `echo "Select";`. If you have any further questions, you might want to consider opening a new question as we're going well beyond the scope of the original question here. – GMemory Mar 25 '14 at 11:20
1

Instead of $row['$Reference'] use $row[$Reference].

Apart from that DO NOT USE mysql_* SINCE IT IS DEPRECATED. Use mysqli or PDO.

Also, escape parameters which you receive from the web. Otherwise you are vulnerable to mysql injection attacks.

dkasipovic
  • 5,930
  • 1
  • 19
  • 25
0

You can use following methods

$row["$Reference"] // with double quotes

or

$row[$Reference]  //no quotes

As with single quotes it is considered as string.

Read this for reference.

Community
  • 1
  • 1
عثمان غني
  • 2,786
  • 4
  • 52
  • 79