Solved

Querying mysql with multiple checkbox values

Posted on 2004-09-07
2
1,442 Views
Last Modified: 2013-12-12
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
Comment
Question by:ksecor
  • 2
2 Comments
 
LVL 17

Expert Comment

by:akshah123
Comment Utility
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
 
LVL 17

Accepted Solution

by:
akshah123 earned 250 total points
Comment Utility
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

Easy Project Management (No User Manual Required)

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Popularity Can Be Measured Sometimes we deal with questions of popularity, and we need a way to collect opinions from our clients.  This article shows a simple teaching example of how we might elect a favorite color by letting our clients vote for …
Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

744 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now