PHP在标签内发布变量

时间:2022-11-27 21:11:10

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.

我有一个网站,用户输入电影的名称,在SQL数据库上进行搜索,所有具有相似名称的电影都以表格格式显示。每部电影的名称也是指向第二页的链接,该第二页显示有关此电影的更多详细信息。

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 个解决方案

#1


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

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

<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'];

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

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

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

#1


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

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

<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'];

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

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

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