samedi 24 janvier 2015

mysql query based on checkboxes and input boxes

So I'm trying to create a mysql query based on input by the user. I have a ton of checkboxes (around 50), so I don't want to do them individually. Here's just a few of my checkboxes:



<input type="checkbox" name="check_list[]" value="ki67" /> ki67<br><br>
<input type="checkbox" name="check_list[]" value="myc" /> myc<br><br>
<input type="checkbox" name="check_list[]" value="mta1" /> mta1<br><br>
<input type="checkbox" name="check_list[]" value="mvd/cd34" /> mvd/cd34<br><br>


I then run them through php to tell the user what has been checked:



if(!empty($_POST['check_list'])) {
foreach($_POST['check_list'] as $check) {
echo $check . "<br>";
}
}


Now I would like to run them through a mysql process to display how many rows have been selected. Here's my code for that part:



<?php
//perform database query
$query = "SELECT COUNT(*) ";
$query .= "FROM testdata ";

if(!empty($_POST['check_list'])) {
foreach($_POST['check_list'] as $check) {
$query .= "WHERE rd_$check = 1";
}
}


Then later:



$result = mysqli_query($link, $query);
if (!$result) {
die("Database query failed.");
}


The problem comes when I try to check multiple boxes. I don't know how to concatenate more onto $query. The command would have to look something like: SELECT COUNT(*) FROM testdata WHERE rd_ki67 = 1 AND rd_myc = 1 AND ...;


Does anyone know how to do this? Thanks so much!!!





Aucun commentaire:

Enregistrer un commentaire