直接链接到搜索结果


Linking to search results directly

我在工作中投入了一个超出我(非常普通的)编码技能的项目。所以我有6个字段的数据库。我能够使用php进行搜索(我跟随youtube教程),添加记录,删除记录和更新记录。所有这些都工作得很好。我现在需要的是:

我需要在图像上链接搜索结果(只有少数,最流行的)。例如,我有一个公司的标志在我的主页上(让我们说公司的名字是…我不知道…)"谷歌"。当我点击它时,我希望它重定向到"google"的搜索结果,就像我在搜索字段中插入它一样。这可能吗?(请注意,我的数据库中没有公司,我只是试图给出一个例子,以便人们可以理解我的意图。在我的数据库中,我需要链接以反映字段"unidade"的搜索结果)

这是我到目前为止的代码。我认为不需要添加/更新。php。

<?php
// include the connection file
include "connection.php";
$sql = "SELECT * FROM usuariotb";
if (isset($_POST['search'])) {
$search_term = mysql_real_escape_string($_POST['search_box']);
$sql .=" WHERE nome LIKE '%".$search_term."%'";
$sql .=" OR posto = '{$search_term}' ";
$sql .=" OR nim = '{$search_term}' ";
$sql .=" OR unidade LIKE '%".$search_term."%'";
$sql .=" OR codigoueo LIKE '%".$search_term."%'";
}

$query = mysql_query($sql) or die (mysql_error());
if (isset($_GET['recordId'])) {
$id = mysql_real_escape_string($_GET['recordId']);
$sql_delete = "DELETE FROM usuariotb WHERE id = {$id}";
mysql_query($sql_delete) or die(mysql_error());
header("location:display_data.php");
exit();
}
?>
<html>
<head>
<meta charset="iso-8859-1">
<title></title>
<link rel="stylesheet" type="text/css" href="format.css" />
</head>
<body>
<div class="container">

<form name="search_form" method="POST" action="display_data.php">
Search: <input type="text" name="search_box" value=""/>
<input type="submit" name="search" value="Procurar">
</form>
<div class="container">
<div class="content">
<div class="toolbar"><a href="form_display.php">Adicionar Novo Militar</a></div>
</div>
<div class="toolbar"><a href="search.php">Voltar à página de pesquisa</a></div>
  </div>
</div>
<div class="container">
<div class="content">
<table width="90%" cellpadding="5" cellspacing="5">
<tr>
<td><strong>Nome</strong></td>
<td><strong>Nim</strong></td>
<td><strong>Posto</strong></td>
<td><strong>Unidade</strong></td>
<td><strong>Sigla Unidade</strong></td>
<td><strong>Observa&ccedil;&otilde;es</strong></td>
<td><strong>Ac&ccedil;&otilde;es</strong></td>
</tr>
<?php if (mysql_num_rows($query)) { ?>
<?php while ($row=mysql_fetch_array($query)) {?>
<tr>
<td><?php echo $row['nome'];?></td>
<td><?php echo $row['nim'];?></td>
<td><?php echo $row['posto'];?></td>
<td><?php echo $row['unidade'];?></td>
<td><?php echo $row['codigoueo'];?></td>
<td><?php echo $row['observacoes'];?></td>
<td><a href="display_data.php?recordId=<?php echo $row['id']; ?>">Delete</a></td>
<td><a href="edit.php?edit=<?php echo $row['id']; ?>">Edit</a></td>
</tr>
<?php } /* end loop  */ ?>
<?php } else { ?>
<h2> Nothing to display!</h2>
<?php } /* end rows checking */ ?>
</table>
</div>
</div>
</body>
</html>

首先,我理解你被指派了一个比你目前能力更复杂的任务。我也经历过。许多公司都这样做。

关于你的任务,对于搜索,每个网站通常使用GET来做搜索,例如https://www.google.co.in/search?q=stackoverflow。注意q=stackoverflow部分。因此,您不需要提交任何表单,只需直接使用URL。

所以,我建议你直接访问你想要链接的网站进行搜索。搜索一些示例文本,输入TEST,并观察打开的页面的URL。

然后使用JavaScript/jQuery/等,点击logo,读取你想要搜索的任何文本,在JavaScript中创建一个URL并重定向到那里。

这是总体思路,如果你在执行中遇到任何问题,请随时评论和询问。