[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1452
  • Last Modified:

Querying mysql with multiple checkbox values

I'm trying to query a table named property with two fields names city and rent.
The user will select multiple checkboxes with a maximum rent they are willing to pay to see if there are any matching properties. On the form page, the name of the checkbox options are city[] while the name of the rent field is rent. I've written the code to pass the checkbox values to an array, but when I query the db, I get a negative result if I select more than one check box. If I select just one box, I get a successful result.

Here is my code:
<?php
$city = $_POST['city'];
$rent = $_POST['rent'];
foreach ($city as $value){
$search=$search.",".$value;
}
/* Remove Leading comma*/
$searchit=substr($search,1);
/*Show the selected fields*/
$db="petfri_petrent";
$link = mysql_connect("localhost","user","pass");
if (! $link)
die("Couldn't connect to MySQL");
mysql_select_db($db , $link)
or die("Couldn't open $db: ".mysql_error());


$result=mysql_query("SELECT * FROM property WHERE city LIKE '%$searchit%' AND rent <= '$rent'")
or die("SELECT Error: ".mysql_error());
$numrows=mysql_num_rows($result);
if ($numrows > 0)
{
echo "we've found one or more matches, please continue to sign up <a href=\"join_form.html\"> Here </a>";
}
else
{
echo "I am sorry we do not have any listings in the area(s) or rent range you chose.";
echo  "You will need to widen your search area and raise your rent maximum.<br \> Click <a href=\"join_form1.php\">HERE</a> to try again.";
}
?>

Thank you for your time....
0
ksecor
Asked:
ksecor
  • 2
1 Solution
 
akshah123Commented:
the problem is with your query. Try this.

$result=mysql_query("SELECT * FROM property WHERE city IN ('%$searchit%') AND rent <= '$rent'")
or die("SELECT Error: ".mysql_error());
0
 
akshah123Commented:
sorry above is wrong. You need two things

first:

foreach ($city as $value){
$search=$search.",\"".$value . "\"";
}

second your query:


$result=mysql_query("SELECT * FROM property WHERE city IN ($searchit) AND rent <= '$rent' ")
or die("SELECT Error: ".mysql_error());

0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now