在搜索结果上创建特定的超链接

时间:2019-07-05 14:32:34

标签: php html sql

我正在做一个小的Web应用程序,其工作原理就像一个booking.com。

我已经编写了用于搜索城市的代码,并且可以在表格中看到结果,如何使所有结果(例如酒店名称)可链接,并重定向到正确的酒店页面每个结果?

我已经尝试过,但是重定向到正确的页面时遇到了一些问题,它也给我带来了一些代码错误

<?php 

    $con= new mysqli("localhost","root","","registration");
    $name = $_POST['search'];
    //$query = "SELECT * FROM hotels
   // WHERE city LIKE '%{$name}%'";

    // Check connection
    if (mysqli_connect_errno())
      {
      echo "Failed to connect to MySQL: " . mysqli_connect_error();
      }

$result = mysqli_query($con, "SELECT * FROM hotels
    WHERE city LIKE '%{$name}%'");

echo "
 <table border='1' cellpadding='0' cellspacing='0' style='border-collapse: collapse' bordercolor='#111111' width='532' height='23' id='AutoNumber1'>
    <tr>
      <td width='120' height='23' align='center'>Name</td>
      <td width='179' height='23' align='center'>Address</td>
      <td width='100' height='23' align='center'>Phone number</td>
      <td width='150' height='23' align='center'>E-mail</td>
      <td width='50' height='23' align='center'>Stars</td>
      <td width='100' height='23' align='center'>Price single room</td>
      <td width='100' height='23' align='center'>Price double room</td>
    </tr>";

while ($row = mysqli_fetch_array($result))
{

echo "
    <tr>
      <td width='120' height='23'>$row[namehotel]</td>
      <td width='179' height='23'>$row[address]</td>
      <td width='100' height='23'>$row[phonenumber]</td>
      <td width='150' height='23'>$row[email]</td>
      <td width='50' height='23'>$row[stars]</td>
      <td width='100' height='23'>$row[pricesingle]</td>
      <td width='100' height='23'>$row[pricedouble]</td>

    </tr>";
        echo "<br>";
}
    mysqli_close($con);
    ?>

同时,我无法创建链接,例如,如果我有希尔顿的话,该名称应该是可单击的,并且应重定向到希尔顿酒店的页面(由我在html中创建)

谢谢!

2 个答案:

答案 0 :(得分:0)

我不确定我是否理解正确。您是否需要html超链接?

echo '<tr>
  <td width="120" height="23">
    <a href="your/target/with'.$row[hotelID].'" >'.$row[namehotel].'</a>
  </td>
  <td width="179" height="23">'.$row[address].'</td>
  <td width="100" height="23">'.$row[phonenumber].'</td>
  ...
  ...
</tr>';

答案 1 :(得分:0)

好,我能够创建超链接,但是是否可以为每个结果创建一个不同的链接?

我使用

创建了超链接
<a href="pagehotel.php"></a>

创建此页面时,我无法带上$_POST['namehotel'],但出现未定义错误

我发布代码:

<?php 
    session_start(); 
    $con= mysqli_connect("localhost","root","","registration");


    // Check connection
    if (mysqli_connect_errno())
      {
      echo "Failed to connect to MySQL: " . mysqli_connect_error();
      }

$result = mysqli_query($con, "SELECT * FROM hotels
    WHERE namehotel LIKE '%{$_POST['namehotel']}%'");

 mysqli_close($con);
    ?>

如何从上次搜索中获得$ _POST ['namehotel']?