Solved

Checkbox Search Help

Posted on 2008-06-09
46
315 Views
Last Modified: 2013-12-13
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
Comment
Question by:digarati
  • 24
  • 17
  • 3
  • +2
46 Comments
 

Expert Comment

by:ycgov
Comment Utility
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
 
LVL 1

Author Comment

by:digarati
Comment Utility
I like the idea of the list box.
How can you do this with php my hosting company is LINUX
0
 
LVL 48

Expert Comment

by:hernst42
Comment Utility
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
 
LVL 1

Author Comment

by:digarati
Comment Utility
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
 
LVL 1

Author Comment

by:digarati
Comment Utility
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
 
LVL 82

Expert Comment

by:hielo
Comment Utility
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
 
LVL 48

Expert Comment

by:hernst42
Comment Utility
Can you post the code of search.php the html code looks good.
0
 
LVL 1

Author Comment

by:digarati
Comment Utility
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
 
LVL 48

Expert Comment

by:hernst42
Comment Utility
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
 
LVL 1

Author Comment

by:digarati
Comment Utility
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
 
LVL 82

Expert Comment

by:hielo
Comment Utility
digarati, did you try my suggestion. It should work as is as long as you provide the corrent db info.
0
 
LVL 1

Author Comment

by:digarati
Comment Utility
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
 
LVL 1

Author Comment

by:digarati
Comment Utility
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
 
LVL 82

Expert Comment

by:hielo
Comment Utility
Sorry. Misse a parenthesis here:
if( isset($_POST['search'])

should be:
if( isset($_POST['search']) )
0
 
LVL 82

Expert Comment

by:hielo
Comment Utility
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
 
LVL 1

Author Comment

by:digarati
Comment Utility
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
 
LVL 82

Expert Comment

by:hielo
Comment Utility
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
 
LVL 1

Author Comment

by:digarati
Comment Utility
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
 
LVL 82

Expert Comment

by:hielo
Comment Utility
this:
$idList = "'" . explode("','",$_POST['id']) . "'";

should be:
$idList = "'" . implode("','",$_POST['id']) . "'";
0
 
LVL 82

Expert Comment

by:hielo
Comment Utility
BTW: you need to replace {$tableName} with your actual tablename.
0
 
LVL 1

Author Comment

by:digarati
Comment Utility
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
 
LVL 1

Author Comment

by:digarati
Comment Utility
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
 
LVL 82

Expert Comment

by:hielo
Comment Utility
echo "<div>" . $row['company'] . "</div>";
0
How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

 
LVL 1

Author Comment

by:digarati
Comment Utility
Unknown column 'company' in 'field list'
0
 
LVL 82

Expert Comment

by:hielo
Comment Utility
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
 
LVL 2

Expert Comment

by:bcmeyer1983
Comment Utility
apologies, it is company
0
 
LVL 1

Author Comment

by:digarati
Comment Utility
i am so sorry my primary key is called "key" instead of ID
:(
0
 
LVL 82

Expert Comment

by:hielo
Comment Utility
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
 
LVL 1

Author Comment

by:digarati
Comment Utility
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
 
LVL 82

Expert Comment

by:hielo
Comment Utility
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
 
LVL 1

Author Comment

by:digarati
Comment Utility
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
 
LVL 1

Author Comment

by:digarati
Comment Utility
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
 
LVL 82

Expert Comment

by:hielo
Comment Utility
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
 
LVL 1

Author Comment

by:digarati
Comment Utility
Unknown column 'company' in 'field list'
0
 
LVL 1

Author Comment

by:digarati
Comment Utility
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
 
LVL 82

Expert Comment

by:hielo
Comment Utility
This:
$idList = " OR `" . explode("`='ON'",$_POST['id']) . "'";

should be:
$idList = " OR `" . explode("`='ON'",$_POST['id']);
0
 
LVL 1

Author Comment

by:digarati
Comment Utility
hielo i have the same result
0
 
LVL 82

Expert Comment

by:hielo
Comment Utility
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
 
LVL 1

Author Comment

by:digarati
Comment Utility
SELECT `company` FROM `vendor` WHERE `ArrayUnknown column 'company' in 'field list'
0
 
LVL 1

Author Comment

by:digarati
Comment Utility
Updated:

SELECT `company` FROM `scope` WHERE `Array'Unknown column 'Array'' in 'where clause'
0
 
LVL 82

Expert Comment

by:hielo
Comment Utility
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
 
LVL 82

Accepted Solution

by:
hielo earned 500 total points
Comment Utility
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
 
LVL 1

Author Closing Comment

by:digarati
Comment Utility
Thank you for your time and solution. Looking forward to any future assistance you can provide to me, you are very knowledgable.
0
 
LVL 1

Author Comment

by:digarati
Comment Utility
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
 
LVL 1

Author Comment

by:digarati
Comment Utility
seems to be missing back ticks (`) for each or statement....
0
 
LVL 82

Expert Comment

by:hielo
Comment Utility
>>seems to be missing back ticks (`) for each or statement
Oops. should have been:

$idList = "`" . implode("`='ON' OR `",$_POST['id']) . "`='ON'";
0

Featured Post

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

Suggested Solutions

Introduction Since I wrote the original article about Handling Date and Time in PHP and MySQL (http://www.experts-exchange.com/articles/201/Handling-Date-and-Time-in-PHP-and-MySQL.html) several years ago, it seemed like now was a good time to updat…
"In order to have an organized way for empathy mapping, we rely on a psychological model and trying to model it in a simple way, so we will split the board to three section for each persona and a scenario and try to see what those personas would Do,…
This video teaches users how to migrate an existing Wordpress website to a new domain.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now