Solved

Querying mysql with multiple checkbox values

Posted on 2004-09-07
2
1,445 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
2 Comments
 
LVL 17

Expert Comment

by:akshah123
ID: 12000941
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
ID: 12000967
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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Deprecated and Headed for the Dustbin By now, you have probably heard that some PHP features, while convenient, can also cause PHP security problems.  This article discusses one of those, called register_globals.  It is a thing you do not want.  …
This article discusses four methods for overlaying images in a container on a web page
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
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 …

740 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