• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 338
  • Last Modified:

Checkbox Search Help

I have a search page (search.html) which is snipped.
I have a database with the following 12 Fields:
id (primary key)
company_name (Company Name)
a2098 (has value of ON or is null)
a2100 (has value of ON or is null)
a2500 (has value of ON or is null)
a2510 (has value of ON or is null)
a2550 (has value of ON or is null)
a2610 (has value of ON or is null)
a2620 (has value of ON or is null)
a2710 (has value of ON or is null)
a2750 (has value of ON or is null)
a2800 (has value of ON or is null)



I am trying to create a search function that will query the database based on the search.html checkboxes and echo company_name if values exist for that record.

Example:

From search.html i will check the box 2098
The database contains one record with 2098 having the value of ON
I would like for my search to echo the company_name when queried.
<html>
 
<head>
<meta http-equiv="Content-Language" content="en-us">
<meta http-equiv="Content-Type" content="text/html; charset=windows-1252">
<title>Search</title>
</head>
 
<body>
 
<form method="POST" action="search.php">
 
<table border="0" width="100%" id="table1">
	<tr>
		<td colspan="3">
		<p align="center"><font size="4">Database Search</font></td>
	</tr>
	<tr>
		<td width="42">2098</td>
		<td width="118">Sheet Piling</td>
		<td><input type="checkbox" name="a2098" value="ON"></td>
	</tr>
	<tr>
		<td width="42">2100</td>
		<td width="118">Exc./Grade/Clear</td>
		<td><input type="checkbox" name="a2100" value="ON"></td>
	</tr>
	<tr>
		<td width="42">2500</td>
		<td width="118">Site Drainage</td>
		<td><input type="checkbox" name="a2500" value="ON"></td>
	</tr>
	<tr>
		<td width="42">2510</td>
		<td width="118">Walks</td>
		<td><input type="checkbox" name="a2510" value="ON"></td>
	</tr>
	<tr>
		<td width="42" height="24">2550</td>
		<td height="24" width="118">Site Utilities</td>
		<td height="24"><input type="checkbox" name="a2550" value="ON"></td>
	</tr>
	<tr>
		<td width="42">2610</td>
		<td width="118">Concrete Paving</td>
		<td><input type="checkbox" name="a2610" value="ON"></td>
	</tr>
	<tr>
		<td width="42">2620</td>
		<td width="118">Curb/Gutter</td>
		<td><input type="checkbox" name="a2620" value="ON"></td>
	</tr>
	<tr>
		<td width="42">2710</td>
		<td width="118">Fence/Gates</td>
		<td><input type="checkbox" name="a2710" value="ON"></td>
	</tr>
	<tr>
		<td width="42">2750</td>
		<td width="118">Irrigation</td>
		<td><input type="checkbox" name="a2750" value="ON"></td>
	</tr>
	<tr>
		<td width="42">2800</td>
		<td width="118">Landscape</td>
		<td><input type="checkbox" name="a2800" value="ON"></td>
	</tr>
</table>
 
<input type="submit" value="Search" name="search"></body>
 
</html>

Open in new window

0
digarati
Asked:
digarati
  • 24
  • 17
  • 3
  • +2
1 Solution
 
ycgovCommented:
I would suggest trying it with a checkboxlist so you can do:

HTML----
        <asp:CheckBoxList ID="checkboxlist" runat="server" Font-Names="Calibri" RepeatColumns="2"
                            Width="257px" Height="103px" ForeColor="White">
                            <asp:ListItem Value="2098">2098</asp:ListItem>
                            <asp:ListItem Value="2099">2099</asp:ListItem>
                            <asp:ListItem Value="2100">2100</asp:ListItem>
                        </asp:CheckBoxList>

VB.NET -
Dim qryStr as String = "SELECT * FROM tableName"
Dim whereStr as String = ""

        For i As Integer = 0 To checkboxlist.Items.Count - 1
            If checkboxlist.Items(i).Selected = True Then
                 if whereStr <> "" then
                    whereStr &= "  AND 2098 = " & 'VALUE HERE
                else
                    whereStr  = "WHERE 2098 = " & 'VALUE HERE
            End If
        Next
0
 
digaratiAuthor Commented:
I like the idea of the list box.
How can you do this with php my hosting company is LINUX
0
 
hernst42Commented:
1st I would name the checkboxes different. use searchfield[id] like
<input type="checkbox" name="searchfield[a2800]" value="ON">

The in PHP you can realy easy build the query like this:
if (empty($_POST['searchfield'])) {
 echo "you didn't check a box for search";
}
$searches = array();
foreach (array_keys($_POST['searchfield']) as $f) {
   $searches = "$f = 'ON'";
}
$searchcondition = implode (' OR ', $searches);
$sql = "SELECT company_name FROm table WHERE $searchcondition";
0
Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

 
digaratiAuthor Commented:
hernst42 you seem to be in my direction.
snipped is my updated html search file.

When i perform the search i am getting:

Parse error: parse error, unexpected T_IF in /home/content/h/a/l/username/html/beta/search/search.php on line 4

Can you assist me further?
<html>
 
<head>
<meta http-equiv="Content-Language" content="en-us">
<meta http-equiv="Content-Type" content="text/html; charset=windows-1252">
<title>Search</title>
</head>
 
<body>
 
<form method="POST" action="search.php">
 
<table border="0" width="100%" id="table1">
	<tr>
		<td colspan="3">
		<p align="center"><font size="4">Database Search</font></td>
	</tr>
	<tr>
		<td width="42">2098</td>
		<td width="118">Sheet Piling</td>
		<td><input type="checkbox" name="searchfield[a2098]" value="ON"></td>
	</tr>
	<tr>
		<td width="42">2100</td>
		<td width="118">Exc./Grade/Clear</td>
		<td><input type="checkbox" name="searchfield[a2100]" value="ON"></td>
	</tr>
	<tr>
		<td width="42">2500</td>
		<td width="118">Site Drainage</td>
		<td><input type="checkbox" name="searchfield[a2500]" value="ON"></td>
	</tr>
	<tr>
		<td width="42">2510</td>
		<td width="118">Walks</td>
		<td><input type="checkbox" name="searchfield[a2510]" value="ON"></td>
	</tr>
	<tr>
		<td width="42" height="24">2550</td>
		<td height="24" width="118">Site Utilities</td>
		<td height="24"><input type="checkbox" name="searchfield[a2550]" value="ON"></td>
	</tr>
	<tr>
		<td width="42">2610</td>
		<td width="118">Concrete Paving</td>
		<td><input type="checkbox" name="searchfield[a2610]" value="ON"></td>
	</tr>
	<tr>
		<td width="42">2620</td>
		<td width="118">Curb/Gutter</td>
		<td><input type="checkbox" name="searchfield[a2620]" value="ON"></td>
	</tr>
	<tr>
		<td width="42">2710</td>
		<td width="118">Fence/Gates</td>
		<td><input type="checkbox" name="searchfield[a2710]" value="ON"></td>
	</tr>
	<tr>
		<td width="42">2750</td>
		<td width="118">Irrigation</td>
		<td><input type="checkbox" name="searchfield[a2750]" value="ON"></td>
	</tr>
	<tr>
		<td width="42">2800</td>
		<td width="118">Landscape</td>
		<td><input type="checkbox" name="searchfield[a2800]" value="ON"></td>
	</tr>
</table>
 
<input type="submit" value="Search" name="search"></body>
 
</html>

Open in new window

0
 
digaratiAuthor Commented:
sorry was missing a semicolon on my DB connection.
here is my current error when i run the search:

Warning: implode(): Bad arguments. in /home/content/h/a/l/username/html/beta/search/search.php on line 11
0
 
hieloCommented:
My suggestion is to give all your fields the same name. What you originally had as name would actually be the value of the checkboxes, and all the checkboxes would have the same name="id[]". That way you can easily access the data as an array. Save the attched code as hielo.php, provide the correct db info and try it.
<?php
$dbserver="localhost";
$username="John";
$password="testing";
$dbname="products";
$tableName="table1";
?>
<html>
 
<head>
<meta http-equiv="Content-Language" content="en-us">
<meta http-equiv="Content-Type" content="text/html; charset=windows-1252">
<title>Search</title>
</head>
 
<body>
 
<div>
<?php
if( isset($_POST['search'])
{
	// we connect to example.com and port 3307
	$link = mysql_connect($dbserver, $dbusername, $dbpassword);
	if (!$link)
	{
		die('Could not connect: ' . mysql_error());
	} 
	$db_selected = mysql_select_db($dbname, $link);
	if (!$db_selected){
		die ("Can't use {$dbname} : " . mysql_error());
	}
	$idList = "'" . explode("','",$_POST['id']) . "'";
	$sql = "SELECT `company_name` FROM `{$tableName}` WHERE `id` IN ({$idList})";
	$result = mysql_query(sql) or die(mysql_error());
	if( 0 < mysql_num_rows($result) )
	{
		while( $row = mysql_fetch_assoc($result) )
		{
			echo "<div>$row['company_name']</div>";
		}
	}
	else
		echo "<div>No matching records found.</div>";
}
?>
</div>
<form method="POST" action="<?=$_SERVER['PHP_SELF']?>"> 
<table border="0" width="100%" id="table1">
      <tr>
            <td colspan="3">
            <p align="center"><font size="4">Database Search</font></td>
      </tr>
      <tr>
            <td width="42">2098</td>
            <td width="118">Sheet Piling</td>
            <td><input type="checkbox" value="a2098" name="id[]"></td>
      </tr>
      <tr>
            <td width="42">2100</td>
            <td width="118">Exc./Grade/Clear</td>
            <td><input type="checkbox" value="a2100" name="id[]"></td>
      </tr>
      <tr>
            <td width="42">2500</td>
            <td width="118">Site Drainage</td>
            <td><input type="checkbox" value="a2500" name="id[]"></td>
      </tr>
      <tr>
            <td width="42">2510</td>
            <td width="118">Walks</td>
            <td><input type="checkbox" value="a2510" name="id[]"></td>
      </tr>
      <tr>
            <td width="42" height="24">2550</td>
            <td height="24" width="118">Site Utilities</td>
            <td height="24"><input type="checkbox" value="a2550" name="id[]"></td>
      </tr>
      <tr>
            <td width="42">2610</td>
            <td width="118">Concrete Paving</td>
            <td><input type="checkbox" value="a2610" name="id[]"></td>
      </tr>
      <tr>
            <td width="42">2620</td>
            <td width="118">Curb/Gutter</td>
            <td><input type="checkbox" value="a2620" name="id[]"></td>
      </tr>
      <tr>
            <td width="42">2710</td>
            <td width="118">Fence/Gates</td>
            <td><input type="checkbox" value="a2710" name="id[]"></td>
      </tr>
      <tr>
            <td width="42">2750</td>
            <td width="118">Irrigation</td>
            <td><input type="checkbox" value="a2750" name="id[]"></td>
      </tr>
      <tr>
            <td width="42">2800</td>
            <td width="118">Landscape</td>
            <td><input type="checkbox" value="a2800" name="id[]"></td>
      </tr>
</table>
 
<input type="submit" value="Search" name="search">
</form>
</body>
 
</html>

Open in new window

0
 
hernst42Commented:
Can you post the code of search.php the html code looks good.
0
 
digaratiAuthor Commented:
search.php is snipped:


<?
require_once "dbconnection.php";
 
if (empty($_POST['searchfield'])) {
 echo "you didn't check a box for search";
}
$searches = array();
foreach (array_keys($_POST['searchfield']) as $f) {
   $searches = "$f = 'ON'";
}
$searchcondition = implode (' OR ', $searches);
$sql = "SELECT company FROM scope WHERE $searchcondition";
?>

Open in new window

0
 
hernst42Commented:
Sorry my mistake. Should be:
 $searches[] = "$f = 'ON'";

Attached code with merge of hielo's output code
require_once "dbconnection.php";
 
if (empty($_POST['searchfield'])) {
 echo "you didn't check a box for search";
}
$searches = array();
foreach (array_keys($_POST['searchfield']) as $f) {
   $searches[] = "$f = 'ON'";
}
$searchcondition = implode (' OR ', $searches);
$sql = "SELECT company FROM scope WHERE $searchcondition";
 
$result = mysql_query($sql) or die(mysql_error());
	if( 0 < mysql_num_rows($result) )
	{
		while( ($row = mysql_fetch_assoc($result)) !== false )
		{
			echo "<div>$row['company_name']</div>";
		}
	}
	else
		echo "<div>No matching records found.</div>";

Open in new window

0
 
digaratiAuthor Commented:
hernst42
code is snipped
error:

Parse error: parse error, unexpected T_ENCAPSED_AND_WHITESPACE, expecting T_STRING or T_VARIABLE or T_NUM_STRING in /home/content/h/a/l/username/html/beta/search/search.php on line 18
<?require_once "dbconnection.php";
 
if (empty($_POST['searchfield'])) {
 echo "you didn't check a box for search";
}
$searches = array();
foreach (array_keys($_POST['searchfield']) as $f) {
   $searches[] = "$f = 'ON'";
}
$searchcondition = implode (' OR ', $searches);
$sql = "SELECT company FROM scope WHERE $searchcondition";
 
$result = mysql_query($sql) or die(mysql_error());
	if( 0 < mysql_num_rows($result) )
	{
		while( ($row = mysql_fetch_assoc($result)) !== false )
		{
			echo "<div>$row['company']</div>";
		}
	}
	else
		echo "<div>No matching records found.</div>";
?>

Open in new window

0
 
hieloCommented:
digarati, did you try my suggestion. It should work as is as long as you provide the corrent db info.
0
 
digaratiAuthor Commented:
hielo your code is snipped
error:
Parse error: parse error, unexpected '{' in /home/content/h/a/l/username/html/beta/search/hielo.php on line 17
0
 
digaratiAuthor Commented:
sorry failed to attach code:
<?php
require_once "dbconnection.php";
?>
<html>
 
<head>
<meta http-equiv="Content-Language" content="en-us">
<meta http-equiv="Content-Type" content="text/html; charset=windows-1252">
<title>Search</title>
</head>
 
<body>
 
<div>
<?php
if( isset($_POST['search'])
{
	// we connect to example.com and port 3307
	$link = mysql_connect($dbserver, $dbusername, $dbpassword);
	if (!$link)
	{
		die('Could not connect: ' . mysql_error());
	} 
	$db_selected = mysql_select_db($dbname, $link);
	if (!$db_selected){
		die ("Can't use {$dbname} : " . mysql_error());
	}
	$idList = "'" . explode("','",$_POST['id']) . "'";
	$sql = "SELECT `company` FROM `{$tableName}` WHERE `id` IN ({$idList})";
	$result = mysql_query(sql) or die(mysql_error());
	if( 0 < mysql_num_rows($result) )
	{
		while( $row = mysql_fetch_assoc($result) )
		{
			echo "<div>$row['company']</div>";
		}
	}
	else
		echo "<div>No matching records found.</div>";
}
?>
</div>
<form method="POST" action="<?=$_SERVER['PHP_SELF']?>"> 
<table border="0" width="100%" id="table1">
      <tr>
            <td colspan="3">
            <p align="center"><font size="4">Database Search</font></td>
      </tr>
      <tr>
            <td width="42">2098</td>
            <td width="118">Sheet Piling</td>
            <td><input type="checkbox" value="a2098" name="id[]"></td>
      </tr>
      <tr>
            <td width="42">2100</td>
            <td width="118">Exc./Grade/Clear</td>
            <td><input type="checkbox" value="a2100" name="id[]"></td>
      </tr>
      <tr>
            <td width="42">2500</td>
            <td width="118">Site Drainage</td>
            <td><input type="checkbox" value="a2500" name="id[]"></td>
      </tr>
      <tr>
            <td width="42">2510</td>
            <td width="118">Walks</td>
            <td><input type="checkbox" value="a2510" name="id[]"></td>
      </tr>
      <tr>
            <td width="42" height="24">2550</td>
            <td height="24" width="118">Site Utilities</td>
            <td height="24"><input type="checkbox" value="a2550" name="id[]"></td>
      </tr>
      <tr>
            <td width="42">2610</td>
            <td width="118">Concrete Paving</td>
            <td><input type="checkbox" value="a2610" name="id[]"></td>
      </tr>
      <tr>
            <td width="42">2620</td>
            <td width="118">Curb/Gutter</td>
            <td><input type="checkbox" value="a2620" name="id[]"></td>
      </tr>
      <tr>
            <td width="42">2710</td>
            <td width="118">Fence/Gates</td>
            <td><input type="checkbox" value="a2710" name="id[]"></td>
      </tr>
      <tr>
            <td width="42">2750</td>
            <td width="118">Irrigation</td>
            <td><input type="checkbox" value="a2750" name="id[]"></td>
      </tr>
      <tr>
            <td width="42">2800</td>
            <td width="118">Landscape</td>
            <td><input type="checkbox" value="a2800" name="id[]"></td>
      </tr>
</table>
 
<input type="submit" value="Search" name="search">
</form>
</body>
 
</html>

Open in new window

0
 
hieloCommented:
Sorry. Misse a parenthesis here:
if( isset($_POST['search'])

should be:
if( isset($_POST['search']) )
0
 
hieloCommented:
and also a $ sign infront of sql here:
$result = mysql_query(sql) or die(mysql_error());

should be:
$result = mysql_query($sql) or die(mysql_error());
0
 
digaratiAuthor Commented:
hielo
getting this error:
Parse error: parse error, unexpected T_ENCAPSED_AND_WHITESPACE, expecting T_STRING or T_VARIABLE or T_NUM_STRING in /home/content/h/a/l/username/html/beta/search/search.php on line 18
0
 
hieloCommented:
since you have a dbconnection.php file, use the attached code instead (copy and paste from attached code)
<?php
require_once "dbconnection.php";
?>
<html>
 
<head>
<meta http-equiv="Content-Language" content="en-us">
<meta http-equiv="Content-Type" content="text/html; charset=windows-1252">
<title>Search</title>
</head>
 
<body>
 
<div>
<?php
if( isset($_POST['search']) )
{ 
      $idList = "'" . explode("','",$_POST['id']) . "'";
      $sql = "SELECT `company` FROM `{$tableName}` WHERE `id` IN ({$idList})";
      $result = mysql_query($sql) or die(mysql_error());
      if( 0 < mysql_num_rows($result) )
      {
            while( $row = mysql_fetch_assoc($result) )
            {
                  echo "<div>$row['company']</div>";
            }
      }
      else
            echo "<div>No matching records found.</div>";
}
?>
</div>
<form method="POST" action="<?=$_SERVER['PHP_SELF']?>"> 
<table border="0" width="100%" id="table1">
      <tr>
            <td colspan="3">
            <p align="center"><font size="4">Database Search</font></td>
      </tr>
      <tr>
            <td width="42">2098</td>
            <td width="118">Sheet Piling</td>
            <td><input type="checkbox" value="a2098" name="id[]"></td>
      </tr>
      <tr>
            <td width="42">2100</td>
            <td width="118">Exc./Grade/Clear</td>
            <td><input type="checkbox" value="a2100" name="id[]"></td>
      </tr>
      <tr>
            <td width="42">2500</td>
            <td width="118">Site Drainage</td>
            <td><input type="checkbox" value="a2500" name="id[]"></td>
      </tr>
      <tr>
            <td width="42">2510</td>
            <td width="118">Walks</td>
            <td><input type="checkbox" value="a2510" name="id[]"></td>
      </tr>
      <tr>
            <td width="42" height="24">2550</td>
            <td height="24" width="118">Site Utilities</td>
            <td height="24"><input type="checkbox" value="a2550" name="id[]"></td>
      </tr>
      <tr>
            <td width="42">2610</td>
            <td width="118">Concrete Paving</td>
            <td><input type="checkbox" value="a2610" name="id[]"></td>
      </tr>
      <tr>
            <td width="42">2620</td>
            <td width="118">Curb/Gutter</td>
            <td><input type="checkbox" value="a2620" name="id[]"></td>
      </tr>
      <tr>
            <td width="42">2710</td>
            <td width="118">Fence/Gates</td>
            <td><input type="checkbox" value="a2710" name="id[]"></td>
      </tr>
      <tr>
            <td width="42">2750</td>
            <td width="118">Irrigation</td>
            <td><input type="checkbox" value="a2750" name="id[]"></td>
      </tr>
      <tr>
            <td width="42">2800</td>
            <td width="118">Landscape</td>
            <td><input type="checkbox" value="a2800" name="id[]"></td>
      </tr>
</table>
 
<input type="submit" value="Search" name="search">
</form>
</body>
 
</html>

Open in new window

0
 
digaratiAuthor Commented:
hielo using snipped code with
error:
Parse error: parse error, unexpected T_ENCAPSED_AND_WHITESPACE, expecting T_STRING or T_VARIABLE or T_NUM_STRING in /home/content/h/a/l/username/html/beta/search/search.php on line 18
<?php
require_once "dbconnection.php";
?>
<html>
 
<head>
<meta http-equiv="Content-Language" content="en-us">
<meta http-equiv="Content-Type" content="text/html; charset=windows-1252">
<title>Search</title>
</head>
 
<body>
 
<div>
<?php
if( isset($_POST['search']) )
{ 
      $idList = "'" . explode("','",$_POST['id']) . "'";
      $sql = "SELECT `company` FROM `{$tableName}` WHERE `id` IN ({$idList})";
      $result = mysql_query($sql) or die(mysql_error());
      if( 0 < mysql_num_rows($result) )
      {
            while( $row = mysql_fetch_assoc($result) )
            {
                  echo "<div>$row['company']</div>";
            }
      }
      else
            echo "<div>No matching records found.</div>";
}
?>
</div>
<form method="POST" action="<?=$_SERVER['PHP_SELF']?>"> 
<table border="0" width="100%" id="table1">
      <tr>
            <td colspan="3">
            <p align="center"><font size="4">Database Search</font></td>
      </tr>
      <tr>
            <td width="42">2098</td>
            <td width="118">Sheet Piling</td>
            <td><input type="checkbox" value="a2098" name="id[]"></td>
      </tr>
      <tr>
            <td width="42">2100</td>
            <td width="118">Exc./Grade/Clear</td>
            <td><input type="checkbox" value="a2100" name="id[]"></td>
      </tr>
      <tr>
            <td width="42">2500</td>
            <td width="118">Site Drainage</td>
            <td><input type="checkbox" value="a2500" name="id[]"></td>
      </tr>
      <tr>
            <td width="42">2510</td>
            <td width="118">Walks</td>
            <td><input type="checkbox" value="a2510" name="id[]"></td>
      </tr>
      <tr>
            <td width="42" height="24">2550</td>
            <td height="24" width="118">Site Utilities</td>
            <td height="24"><input type="checkbox" value="a2550" name="id[]"></td>
      </tr>
      <tr>
            <td width="42">2610</td>
            <td width="118">Concrete Paving</td>
            <td><input type="checkbox" value="a2610" name="id[]"></td>
      </tr>
      <tr>
            <td width="42">2620</td>
            <td width="118">Curb/Gutter</td>
            <td><input type="checkbox" value="a2620" name="id[]"></td>
      </tr>
      <tr>
            <td width="42">2710</td>
            <td width="118">Fence/Gates</td>
            <td><input type="checkbox" value="a2710" name="id[]"></td>
      </tr>
      <tr>
            <td width="42">2750</td>
            <td width="118">Irrigation</td>
            <td><input type="checkbox" value="a2750" name="id[]"></td>
      </tr>
      <tr>
            <td width="42">2800</td>
            <td width="118">Landscape</td>
            <td><input type="checkbox" value="a2800" name="id[]"></td>
      </tr>
</table>
 
<input type="submit" value="Search" name="search">
</form>
</body>
 
</html>

Open in new window

0
 
hieloCommented:
this:
$idList = "'" . explode("','",$_POST['id']) . "'";

should be:
$idList = "'" . implode("','",$_POST['id']) . "'";
0
 
hieloCommented:
BTW: you need to replace {$tableName} with your actual tablename.
0
 
digaratiAuthor Commented:
hielo:

Parse error: parse error, unexpected T_ENCAPSED_AND_WHITESPACE, expecting T_STRING or T_VARIABLE or T_NUM_STRING in /home/content/h/a/l/username/html/beta/search/search.php on line 18

code snipped
<?php
require_once "dbconnection.php";
?>
<html>
 
<head>
<meta http-equiv="Content-Language" content="en-us">
<meta http-equiv="Content-Type" content="text/html; charset=windows-1252">
<title>Search</title>
</head>
 
<body>
 
<div>
<?php
if( isset($_POST['search']) )
{ 
      $idList = "'" . implode("','",$_POST['id']) . "'";
      $sql = "SELECT `company` FROM `{$tableName}` WHERE `id` IN ({$idList})";
      $result = mysql_query($sql) or die(mysql_error());
      if( 0 < mysql_num_rows($result) )
      {
            while( $row = mysql_fetch_assoc($result) )
            {
                  echo "<div>$row['company']</div>";
            }
      }
      else
            echo "<div>No matching records found.</div>";
}
?>
</div>
<form method="POST" action="<?=$_SERVER['PHP_SELF']?>"> 
<table border="0" width="100%" id="table1">
      <tr>
            <td colspan="3">
            <p align="center"><font size="4">Database Search</font></td>
      </tr>
      <tr>
            <td width="42">2098</td>
            <td width="118">Sheet Piling</td>
            <td><input type="checkbox" value="a2098" name="id[]"></td>
      </tr>
      <tr>
            <td width="42">2100</td>
            <td width="118">Exc./Grade/Clear</td>
            <td><input type="checkbox" value="a2100" name="id[]"></td>
      </tr>
      <tr>
            <td width="42">2500</td>
            <td width="118">Site Drainage</td>
            <td><input type="checkbox" value="a2500" name="id[]"></td>
      </tr>
      <tr>
            <td width="42">2510</td>
            <td width="118">Walks</td>
            <td><input type="checkbox" value="a2510" name="id[]"></td>
      </tr>
      <tr>
            <td width="42" height="24">2550</td>
            <td height="24" width="118">Site Utilities</td>
            <td height="24"><input type="checkbox" value="a2550" name="id[]"></td>
      </tr>
      <tr>
            <td width="42">2610</td>
            <td width="118">Concrete Paving</td>
            <td><input type="checkbox" value="a2610" name="id[]"></td>
      </tr>
      <tr>
            <td width="42">2620</td>
            <td width="118">Curb/Gutter</td>
            <td><input type="checkbox" value="a2620" name="id[]"></td>
      </tr>
      <tr>
            <td width="42">2710</td>
            <td width="118">Fence/Gates</td>
            <td><input type="checkbox" value="a2710" name="id[]"></td>
      </tr>
      <tr>
            <td width="42">2750</td>
            <td width="118">Irrigation</td>
            <td><input type="checkbox" value="a2750" name="id[]"></td>
      </tr>
      <tr>
            <td width="42">2800</td>
            <td width="118">Landscape</td>
            <td><input type="checkbox" value="a2800" name="id[]"></td>
      </tr>
</table>
 
<input type="submit" value="Search" name="search">
</form>
</body>
 
</html>

Open in new window

0
 
digaratiAuthor Commented:
new error:
Parse error: parse error, unexpected T_ENCAPSED_AND_WHITESPACE, expecting T_STRING or T_VARIABLE or T_NUM_STRING in /home/content/h/a/l/username/html/beta/search/hielo.php on line 25
0
 
hieloCommented:
echo "<div>" . $row['company'] . "</div>";
0
 
digaratiAuthor Commented:
Unknown column 'company' in 'field list'
0
 
hieloCommented:
OK, my question to you is what id the name of your fields on your table. My initial query was
SELECT company_name...

but you changed it to:
SELECT company...

So, what is it? You need to use the correct field name. I don't know what it is. I don't know how your db table is defined. You should know that.
0
 
bcmeyer1983Commented:
apologies, it is company
0
 
digaratiAuthor Commented:
i am so sorry my primary key is called "key" instead of ID
:(
0
 
hieloCommented:
OK, then modify your sql accordingly (assuming the company name is in the field comapany, you will need)::
 $sql = "SELECT `company` FROM `{$tableName}` WHERE `key` IN ({$idList})";

and
echo "<div>" . $row['company'] . "</div>";
0
 
digaratiAuthor Commented:
OK almost there:

it tells me theres no results, however i have logged into the database and know results exist for these entries...any ideas?
0
 
hieloCommented:
what does your table look like this:

key  company
1      a2098
2      a2100


OR like this:
key            company
a2098     Alpha
a2100     Beta

The query I gave if you is for the second example. If your table looks like the first then you need:
 $sql = "SELECT `key` FROM `{$tableName}` WHERE `company` IN ({$idList})";

and
echo "<div>" . $row['key'] . "</div>";
0
 
digaratiAuthor Commented:
Updated code snipped
My results:

No matching records found.
You search: 'a2620'

<?php
require_once "dbconnection.php";
?>
<html>
 
<head>
<meta http-equiv="Content-Language" content="en-us">
<meta http-equiv="Content-Type" content="text/html; charset=windows-1252">
<title>Search</title>
</head>
 
<body>
 
<div>
<?php
if( isset($_POST['search']) )
{ 
		$idList = "'" . implode("','",$_POST['id']) . "'";
		$sql = "SELECT `company` FROM `scope` WHERE `key` IN ({$idList})";
		$result = mysql_query($sql) or die(mysql_error());
      if( 0 < mysql_num_rows($result) )
      {
            while( $row = mysql_fetch_assoc($result) )
            {
                  echo "<div>" . $row['company'] . "</div>";
            }
      }
      else
            echo "<div>No matching records found.</div>";
}
echo "You search: $idList";
?>

Open in new window

0
 
digaratiAuthor Commented:
not sure how we strayed here, ill go ahead and take the wrap since ima newbie but
my table looks like this:

key            company            a2098      a2100      a2500    a2510    a2550     a2610    a2620   a2710   a2750   a2800
1               mcompany            ON                                         ON                                       ON
2               anothername                                        ON                                       ON
3               billscompany                                                                                     ON                                  ON
4               tomscompany                       ON
5               george                 ON                            ON
0
 
hieloCommented:
OK. update the following two lines as shown below (again, copy and paste from here):
      $idList = " OR `" . explode("`='ON'",$_POST['id']) . "'";
      $sql = "SELECT `company` FROM `{$tableName}` WHERE " . substr($idList,3);
0
 
digaratiAuthor Commented:
Unknown column 'company' in 'field list'
0
 
digaratiAuthor Commented:
btw code snipped
<?php
if( isset($_POST['search']) )
{ 
		$idList = " OR `" . explode("`='ON'",$_POST['id']) . "'";
		$sql = "SELECT `company` FROM `vendor` WHERE " . substr($idList,3);
		$result = mysql_query($sql) or die(mysql_error());
      if( 0 < mysql_num_rows($result) )
      {
            while( $row = mysql_fetch_assoc($result) )
            {
                  echo "<div>" . $row['company'] . "</div>";
            }
      }
      else
            echo "<div>No matching records found.</div>";
}
echo "You searched: $idList";
?>

Open in new window

0
 
hieloCommented:
This:
$idList = " OR `" . explode("`='ON'",$_POST['id']) . "'";

should be:
$idList = " OR `" . explode("`='ON'",$_POST['id']);
0
 
digaratiAuthor Commented:
hielo i have the same result
0
 
hieloCommented:
right before:
$result = mysql_query($sql) or die(mysql_error());

put:
echo $sql;

and report with the output. Also make sure you have the correct fieldname on your db.
0
 
digaratiAuthor Commented:
SELECT `company` FROM `vendor` WHERE `ArrayUnknown column 'company' in 'field list'
0
 
digaratiAuthor Commented:
Updated:

SELECT `company` FROM `scope` WHERE `Array'Unknown column 'Array'' in 'where clause'
0
 
hieloCommented:
At this point I don't know what you are doing. You wrote:
SELECT `company` FROM `scope`
and then you wrote:
SELECT `company` FROM `vendor`

could be your table name, but the point is that you are not paying attentiont to what you are doing.
0
 
hieloCommented:
assuming you specify the correct tablename and that it actually contains the field 'company', then the query you need to execute should be similar to this:
SELECT `company` FROM `vendor` WHERE `a2098`='ON' OR a2500`='ON' OR a2510`='ON' OR a2550`='ON' OR a2610`='ON'

Update your code to this:
      $idList = "`" . implode("`='ON' OR ",$_POST['id']) . "`='ON'";
      $sql = "SELECT `company` FROM `vendor` WHERE " . $idList;
      echo $sql;

and try it, but make sure you specify your actual table name.
0
 
digaratiAuthor Commented:
Thank you for your time and solution. Looking forward to any future assistance you can provide to me, you are very knowledgable.
0
 
digaratiAuthor Commented:
one thing, when i select more than 1 checkbox it gives me this:
SELECT `company` FROM `scope` WHERE `a2710`='ON' OR a2750`='ON'You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '`='ON'' at line 1
0
 
digaratiAuthor Commented:
seems to be missing back ticks (`) for each or statement....
0
 
hieloCommented:
>>seems to be missing back ticks (`) for each or statement
Oops. should have been:

$idList = "`" . implode("`='ON' OR `",$_POST['id']) . "`='ON'";
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 24
  • 17
  • 3
  • +2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now