Jump to content

sql search against an array of checkboxes


glennn.php

Recommended Posts

given a Job Posting/Job Search script...

 

i have an array of checkboxes (Surgery, Optometry, Dentistry, etc...) that are imploded into an array of data and inserted into a table.field as part of a Job Posting.

 

$checkbox1 = $_POST['specialty']; 
$string1 = implode($checkbox1,", "); 
$checkbox2 = $_POST['subspecialty']; 
$string2 = implode($checkbox2,", "); 


$sql="INSERT INTO $tbl_user (username, password, specialty, subspecialty) 
VALUES (\"$_POST[username]\", \"$_POST[password]\", \"$string1\", \"$string2\")";

 

for the Job Search, i have the same array of checkboxes, of course, and i need to check the selected checkboxes against the database and return just the records that contain a particular value (Surgery, Dentistry, for instance)...

 

i know to do this:

 

$sql="SELECT * FROM $tbl_user WHERE specialty CONTAINS ($string1)";

 

 

but i don't know how to get the selected values into the Search query...

 

 

 

does that make sense?

 

appreciate any help!

 

GN

 

Hi Glenn. You need to learn to normalize your data.

You should not be storing the specialties in the main table, but creating a child table for this.

 

ie specialty_id, job_id, specialty_name.

 

That way if you wanted to get the specialtyies of a job... select specialty_name from specialties where job_id = 'job_id'

 

thanks drewbee -

 

well, i'm only looking for ALL records that would contain ANY of the selected checkboxes, not where job_id = 'job_id'...

 

i tried this that works to an extent:

 

		$checkbox1 = $_POST['specialty']; 
	// if ($checkbox1 != '') {
	$string1 = implode($checkbox1,", ");
	// } else {
	// $string1 = 'null';
	// }

	$checkbox2 = $_POST['subspecialty']; 
	// if ($checkbox2 != '') {
	$string2 = implode($checkbox2,", ");
	// } else {
	// $string2 = 'null';
	// }

	$sql="SELECT * FROM $tbl_user WHERE specialty LIKE '%$string1%' OR subspecialty LIKE '%$string2%' ORDER BY contact_name ASC";

 

but it doesn't always work... i'm not always getting a record that contains a term that IS checked, and sometimes it returns ALL records that don't match...

 

ANY help?

 

Thanks

 

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.