Jump to content

[SOLVED] Search DB with multiple fields.


lynxus

Recommended Posts

Hi guys,

Hopefully this is simples.

 

Ive got a bunch of fields that a user can search with.

IE:

Company name

Email

Username

etc

 

They fill in what they want and then hit submit.

 

How do i do a search on the db based on whatever they entered?

I just want the page to return a rough list based on what they entered?

 

Any ideas would be great.

Thanks

G

 

<?php
session_start();

$siteid = $_SESSION['siteid'];
if ($siteid == ""){
die('Error: Not logged in');
}


$username = $_SESSION['username'];
$ticketid = $_POST['ticketid'];
$phonenumber = $_POST['phonenumber'];
$email = $_POST['email'];
$companyname = $_POST['companyname'];
$contactname = $_POST['contactname'];


?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Create support ticket.</title>
<style type="text/css">
<!--
body {
background-color: #FFF;
text-align: left;
background:url(bg.png);
background-repeat:no-repeat;
}
#apDiv1 {
position:absolute;
left:9px;
top:157px;
width:587px;
height:121px;
z-index:1;
overflow: hidden;
}
#apDiv2 {
position:absolute;
left:114px;
top:116px;
width:153px;
height:127px;
z-index:2;
}
body form table tr {
text-align: right;
}
body form table tr td {
text-align: left;
}
body form table {
text-align: left;
}
#apDiv3 {
position:absolute;
left:17px;
top:16px;
width:754px;
height:561px;
z-index:1;
overflow:scroll;
}
-->
</style></head>

<body>

<div id="apDiv3" >

<table width="100%" border="0" cellspacing="0" cellpadding="0">
  <tr>
    <td>Ticket ID</td>
    <td>Company</td>
    <td>Name</td>
    <td>Phone #</td>
    <td>Last Modified</td>
    <td>Status</td>
    <td>View</td>
  </tr>

  <?php
  	$con = mysql_connect("localhost","xxxxxx","xxxxxx");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }

mysql_select_db("xxxxxx", $con);

$query = "SELECT * FROM tickets WHERE siteid = '$siteid' and companyname like '$company' order by created desc"; 

$result = mysql_query($query) or die(mysql_error());


while($row = mysql_fetch_array($result)){
echo '<tr style="font-size:11px; font-style:normal; font-weight:100; "><td>'.$row['ticketid'].'</td>
    <td>'.$row['companyname'].'</td>
    <td>'.$row['name'].'</td>
    <td>'.$row['phone'].'</td>
    <td>'.$row['created'].'</td>
    <td>'.$row['status'].'</td>
    <td><a href = "viewticket.php?ticketid='.$row['ticketid'].'">View</a></td></tr>
';
}

mysql_close($con);
?>

</table>

</div>

</body>
</html>

Link to comment
https://forums.phpfreaks.com/topic/174066-solved-search-db-with-multiple-fields/
Share on other sites

Hi,

At first glance your query looks ok, you just need to add the fields that the user has selected.

 

Something like this:

$query = "SELECT * FROM tickets WHERE siteid = '$siteid'";
if($companyname!="") $query.=" AND companyname LIKE '".$company."'";
if($email!="") $query.=" AND email='".$email."'";
etc.

$sql.=" ORDER BY created DESC";

 

Chris

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.