0

I have a website where the user enters the name of a movie, a search is done on an SQL db and all movies with a similar name are displayed in a table format.
The name of each movie is also a link to a second page which displays further details about this movie.

However in order to get the further details, I need to do another query on this movie on the second page, so ideally when the user clicks on the movie name on the first page, the link should also post the name of the movie to the second page as a variable.
I can then take that variable and use it in the query on the second page.

I dont want to add invisible forms as this still adds buttons to the table, I need the table to stay as it is.

<?php

    $username = "root";
    $password = "";
    $hostname = "localhost";

    $db_handle = mysql_connect($hostname, $username, $password) or die ("Could not connect to database");

    $selected= mysql_select_db("login", $db_handle);

    $output='';
    $output1='';
    $tablehead='';

    if(isset($_POST['search'])){
        $searchq = $_POST['search'];

        $query = mysql_query("SELECT * FROM PHP_Item WHERE Name LIKE '%$searchq%'") or die ("Could not search");
        $count = mysql_num_rows($query);

        if($count == 0){
            $output = 'there were no search results';
        }else{
            $tablehead.= "<table id='searchtable'>
                           <tr>
                              <th>
                                 Name
                              </th>
                              <th>
                                 Price
                              </th>
                           </tr>
                        </table>";
            while($row = mysql_fetch_array($query)){
                $mName = $row['Name'];
                $price = $row['Price'];
                $id= $row['ItemID'];         
                 $output1.= 
                     "<table id='searchtable'>
                        <tr>
                           <td>
                              <a href='searchresult.php'>$mName</a>
                           </td>
                           <td>
                              £$price
                           </td>
                        </tr>
                     </table>";
            }
        }
    }
?>
David Ansermot
  • 6,052
  • 8
  • 47
  • 82
tinOfBeans
  • 707
  • 1
  • 9
  • 22
  • 5
    Please, [stop using `mysql_*` functions](http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php). They are no longer maintained and are [officially deprecated](https://wiki.php.net/rfc/mysql_deprecation). Learn about [prepared statements](http://en.wikipedia.org/wiki/Prepared_statement) instead, and use [PDO](http://jayblanchard.net/demystifying_php_pdo.html). – Jay Blanchard May 04 '15 at 16:06

1 Answers1

4

Site the name or id of the movie in the url of each link like this :

<a href='searchresult.php?mName=".urlencode($mName)."'>$mName</a>

Like this in your details page, you can get the name of the movie inside the variable mName :
$mName = $_GET['mName'];

Update :
You should avoid using mysql_* methods since they're deprecated. Use mysqli_* or PDO.

David Ansermot
  • 6,052
  • 8
  • 47
  • 82