Can somebody help me to retreive one or more checkbox values from the database using php and MySQL? Thanks and I appreciate that.:)

In checkbox.html file the checkbox as follow:

<form method="post" action="Process.php">

	<legend>Choose feature(s) to search for apartment</legend>
<table border="0">
<td><input type="checkbox" name="feature[]" value="walkincloset">Walk in closet</td>
<td><input type="checkbox" name="feature[]" value="storage">storage</td>
<td><input type="checkbox" name="feature[]" value="availability">availability</td>
<td><input type="submit" value="Select"></td>

The code is follow in Process.php

$featurex= $_POST['feature'];

@ $db = new mysqli('localhost', 'root', 'Password', 'Database Name');
if (mysqli_connect_errno()) {
echo 'Error: Could not connect to database. Please try again later.';
$query = "select * from apartment where WHERE walkincloset ='$featurex' or storage= ‘ $feature’ or availability=’$feature’ ";  

$result = $db->query($query);
$num_results = $result->num_rows;
echo "<p>Number of apartments found: ".$num_results."</p>";
for ($i=0; $i <$num_results; $i++) {
$row = $result->fetch_assoc();
echo "<p><strong>".($i+1).". Apartment number: ";
echo htmlspecialchars(stripslashes($row['aptno']));
echo "</strong><br />Rent Price: ";
echo stripslashes($row['pricerent']);
echo "<br />Number of rooms: ";
echo stripslashes($row['noofroom']);
echo "<br />Number of bathrooms: ";
echo stripslashes($row['noofbath']);
echo "<br />Apartment area: ";
echo stripslashes($row['area']);
echo "<br />Walk in closet: ";
echo stripslashes($row['walkincloset']);
echo "<br />Storage: ";
echo stripslashes($row['storage']);
echo "<br />Availability: ";
echo stripslashes($row['availability']);
echo "<br />Apartment photo: ";
echo stripslashes($row['aptphoto']);
echo "<br />Building number: ";
echo stripslashes($row['bldno']);
echo "</p>";

Recommended Answers

All 3 Replies

When you submit your form, your $featurex variable, will be an array of the values depending on which checkbox is selected. If all are selected the array will be


or if just walkincloset and availability are selected then the array would hold


You can create the $sql statement based on these values depending on what the columns of walkincloset, storage and availability hold in the DB.

Hope this makes sense. Let me know if you have trouble figuring it out and I'll try explaining it further.

Yes, I do have some trouble understanding how the query statment will be? Can you please modify the process.php file and show me what you prevoiusly explained? Thank you.

How would you know if the apartment will have a walkincloset, storage or availibility? I am assuming your DB is set up in such a way as to say something like

1 | y | n | y
2 | n | y | n
3 | n | n | y

Or something in that respect.

If this is the case, then you can generate the query as

$query_str=" WHERE ";
foreach ($featurex as $value){

  if ($value=="walkincloset"){
     $query_str .="walkincloset='y'";
  else if ($value=="storage"){
     $query_str .="storage='y'";
  else if ($value=="availability"){
     $query_str .="availability='y'";
  $query_str .=" AND ";
$sql="SELECT * FROM apartment".$query_str;

This will dynamically generate the query which you can use.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.