直接链接到搜索结果

时间:2014-03-14 15:39:03

标签: php search

我让自己进入了一个超出我(非常适度)编码技能的工作项目。 所以我有一个包含6个字段的数据库。我可以使用php(我按照youtube教程)对其进行搜索,添加记录,删除记录和更新记录。所有这一切都很好。 我现在需要的是:

我需要在图片上链接搜索结果(只有少数,最受欢迎)。 例如,我的主页上有一个公司徽标(让我们说公司名称是......我不知道......" Google"。当我点击它时,我希望它重定向到搜索结果" google",就像我在搜索字段中插入了一样。这可能吗? (请注意,我没有公司在我的数据库中,我只是想举一个例子,以便人们可以理解我的意图。在我的数据库中,我需要链接以反映该字段的搜索结果" unidade&#34)

到目前为止,这是我的代码。我认为不需要添加/更新.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>

1 个答案:

答案 0 :(得分:0)

首先,我强烈要求您承担比当前能力更复杂的任务。去过也做过。许多公司都是这样做的。

关于您的任务,对于搜索,每个网站通常使用GET进行搜索,例如 https://www.google.co.in/search?q=stackoverflow 的。请注意q=stackoverflow部分。为此,您无需提交任何表格,只需直接使用该网址即可。

因此,我建议您访问要直接链接进行搜索的网站。搜索一些示例文本,例如TEST并观察打开页面的URL。

然后使用JavaScript / jQuery / etc,点击徽标,阅读您要搜索的任何文本,在JavaScript中创建一个URL并在那里redirect

这是一般性的想法,随时评论并询问您是否在实施过程中遇到任何问题。