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
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

 

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.