首先,如果这个问题看似重复,或者你认为我没有做过尽职调查,那么道歉。我以前通常会在互联网上搜索,但我觉得好像我需要问一下。
所以,基本上,我建立了一个单页的动态网站,列出了我当地城市的一些景点。那部分我没问题 - 我使用MySQL创建了一个数据库,我有1个数据库,4个不同的表,每个表都有不同的ID名称。
我的问题是,我的搜索仅限于用户输入的内容,即景点的名称,我努力将其实际链接到任何动态网址(循环查询的第一个表除外)< / p>
下面我的代码解释了为什么会这样做,我希望上帝有人可以引导我找到答案,无论是外部链接等等。我将非常感激并永远感激不尽。
<?php
error_reporting(0);
?>
<?php
mysqli_connect("localhost", "root", "", "everydayleeds") or die(mysqli_error());
mysqli_select_db($con, "everydayleeds") or die(mysqli_error()); /* everydayleeds is the name of database we've created */
$query = $_GET['query']; // 'query' is the variable name given to the $_GET action, which will take whatever is typed into the search box, indicating this via the URL paramater'
$min_length = 3; // You can set minimum length of the query if you want
if(strlen($query) >= $min_length){ // If query length is more or equal minimum length then
$query = htmlspecialchars($query); // This changes characters used in HTML to their equivalents, for example: < to >
$query = mysqli_real_escape_string($con, $query); // Makes sure nobody uses SQL injection
$raw_results = mysqli_query ($con, "SELECT foodndrink.Name AS fdName, nightlife.Name AS nightName, culture.Name AS cultName, placestostay.Name AS pName, foodndrink.fdID AS foodID, nightlife.nightID AS nID, cultID AS cID, placesID AS pID FROM foodndrink, nightlife, culture, placestostay
WHERE (foodndrink.Name LIKE '%".$query."%') OR (nightlife.Name LIKE '%".$query."%') OR (culture.Name LIKE '%".$query."%') OR (placestostay.Name LIKE '%".$query."%') ") OR die (mysqli_error($con));
if(mysqli_num_rows($raw_results) > 0){ // If one or more rows are returned do following
while($results = mysqli_fetch_assoc($raw_results)){ // $results = mysql_fetch_assoc($raw_results) puts data from database into array, while it's valid it does the loop
echo "Yes, we have a".$_GET['query']."<a href=attractions.php?category&fdID=".$results['foodID'];
break;
}
}
else{ // if there is no matching rows do following
echo "<p>Nope, Leeds probably doesn't have a </p>".$_GET['query'];
}
}
else{ // if query length is less than minimum
echo "Minimum length is ".$min_length;
}
?>
因此,用户在找到结果后可以点击的链接仅返回到Food N Drinks页面,因为我已经设置了?category&fdID=".$results['foodID'];
。
我尝试过循环,加入,我似乎在转圈。我相信我的问题只是我有4个不同的表,包含所有4个不同的ID。
任何建议,帮助将非常感激。
谢谢。
答案 0 :(得分:0)
我不确定,但我认为如果您使用&#34; Union all&#34;方法,您可以将静态文本添加到每个表的结果。有一个示例in here。
之后,你可以做出类似的东西
echo "<a href=attractions.php?category&".$results['tableName']."&ID=".$results['foodID'];