0

I got myself into a project at work that is going beyond my (very modest) coding skills. So i have a database with 6 fields. I'm able to make searchs on it using php (i followed a youtube tutorial), add records, delete records and update records. All of that is working just fine. What i need now is this:

I need to link search results (only a few, the most popular) on a image. Per example, i have a company logo on my main page (let's say company name is..i dunno.. "Google". When i click on it, i wanted it to redirect to the search results of "google", like if i had inserted that in the search field. Is that possible? (notice that i don't have companies in my database, i'm just trying to give an example so that people can understand what i intend. In my database i would need to link to reflect search results of the field "unidade")

Here is my code so far. The add / update .php i don't think are needed.

<?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>
tshepang
  • 12,111
  • 21
  • 91
  • 136

1 Answers1

0

First of all, I empathize with you regarding being tasked with a more complex task than your current ability. Been there, done that. Many companies do this.

Regarding your task, for search every website usually uses GET to do the search e.g. https://www.google.co.in/search?q=stackoverflow. Notice the q=stackoverflow part. For this you do not need to submit any form, just use the URL directly.

So, I suggest you visit the websites where you want to link directly for search. Search for some sample text, say TEST and observe the URL of the page that opens.

Then using JavaScript/jQuery/etc, on click of the logo, read whatever text you want to search, create a URL in JavaScript and redirect there.

This is the general idea, feel free to comment and ask if you face any issues in implementation.

Community
  • 1
  • 1