PHP在<a> tag

时间:2015-05-04 16:02:53

标签: php html mysql post

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>";
            }
        }
    }
?>

1 个答案:

答案 0 :(得分:4)

将电影的名称或ID放在每个链接的网址中,如下所示:

mName

在您的详细信息页面中,您可以在变量$mName = $_GET['mName'];中获取电影的名称:
&&

更新:您应该避免使用mysql_ *方法,因为它们已被弃用。使用mysqli_ *或PDO。