Jump to content

sql search with multiple checkboxes


brooksh

Recommended Posts

I'm not sure why this isn't working. I am selecting multiple checkboxes, and having the next step search based on those selections.

 

The first step I select States and want to find out counties associated with the states selected. But it doesn't work.

 

for( $i=0; $i<count($_POST['State']); $i++ ) {
$State1 .= $_POST['State'][$i] . ",";
}
$str_temp_len=strlen($State1);
$State1=substr($State1,0,($str_temp_len-1));
$sql = "select DISTINCT county from zip_codes where state_prefix = '$State1' order by county";
        $result = mysql_query ($sql);

Link to comment
https://forums.phpfreaks.com/topic/97734-sql-search-with-multiple-checkboxes/
Share on other sites

Couple of things:

 

Can you edit this line

$result = mysql_query ($sql);

 

to

 

$result = mysql_query ($sql) or die( $sql."<br />".mysql_error() );

 

Run that and then copy/paste the error msg here.

 

--

I just guessing now, but if you are looking for a distinct county based on state prefix you might want to make a couple changes to your code, fun it and post results here.

 


for( $i=0; $i<count($_POST['State']); $i++ ) {
  if( !isSet( $State1 ) ){
      $State1 = "( '{$_POST['State'][$i]}' ";
  }else{
       $State1 .= ", '{$_POST['State'][$i]}' ";
  }//end if
}
$State1 .= " ) ";
$sql = "SELECT DISTINCT z.county, z.state_prefix FROM zip_codes z WHERE z.state_prefix in {$State1} GROUP BY z.count, z.state_prefix";
$result = mysql_query ($sql) or die( $sql."<br />".mysql_error() );

while( $row = mysql_fetch_array( $result, MYSQL_ASSOC() ) {
  echo $row['county']." ".$row['state;]."<br />";
}//end while loop

mysql_free_result( $result );

$row = array();
$State1 = $sql = $result = $row = null; 
unset( $State1, $sql, $result, $row );
exit('fin');


 

Let me know how that turns out!

 

You can also get low cost php support at www.phpsupportnow.com

 

Good luck

 

 

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.