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
Solved

Querying mysql with multiple checkbox values

Posted on 2004-09-07
2
1,444 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
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: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

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

Suggested Solutions

Title # Comments Views Activity
PHP Syntax Error 4 32
Currency in SQL? 2 30
Why will this INSERT statement not insert the date? 2 19
$_SERVER Variable question 29 18
Author Note: Since this E-E article was originally written, years ago, formal testing has come into common use in the world of PHP.  PHPUnit (http://en.wikipedia.org/wiki/PHPUnit) and similar technologies have enjoyed wide adoption, making it possib…
Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
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…

789 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