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

 

Link to comment
Share on other sites

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'

 

Link to comment
Share on other sites

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

 

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.