?
Solved

Querying mysql with multiple checkbox values

Posted on 2004-09-07
2
Medium Priority
?
1,448 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 1000 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

WordPress Tutorial 2: Terminology

An important part of learning any new piece of software is understanding the terminology it uses. Thankfully WordPress uses fairly simple names for everything that make it easy to start using the software.

Question has a verified solution.

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

Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
There are times when I have encountered the need to decompress a response from a PHP request. This is how it's done, but you must have control of the request and you can set the Accept-Encoding header.
The viewer will learn how to count occurrences of each item in an array.
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…
Suggested Courses

752 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