PHP - Select sql from html checkbox -
i want write sql include checkbox.
for example on database:
column: stato
options: 0,1,2,3
where:
- 0 = aperto,
- 1 = stampato,
- 2 = bloccato,
- 3 = ❤ (favorite),
so, if selected must show me kinds, otherwise not.
how can write sql?
this checkbox:
<div id="radioset"> <input type="checkbox" id="radio1" name="aperto" checked="checked"><label for="radio1">aperto</label> <input type="checkbox" id="radio2" name="stampato" checked="checked"><label for="radio2">stampato</label> <input type="checkbox" id="radio3" name="bloccato" checked="checked"><label for="radio3">bloccato</label> <input type="checkbox" id="radio4" name="favorite" checked="checked"><label for="radio4">♥</label> </div>
and php sql:
$input=$_post['input']; $id=$_post['id']; $tipo=$_post['tipo']; $numero1=$_post['numero1']; $numero2=$_post['numero2']; $data1=date('d/m/y', strtotime($_post['from']));; $data2=date('d/m/y', strtotime($_post['to']));; $sql="select [id ord] [id], [tipo ord] [tipo], [n ord] [numero], [data ord] [data], [ragione sociale], [indirizzo], [totimp] [importo totale], [totiva] [importo iva] [ordini] [id ord] '$id' or [tipo ord] '$tipo' or [data ord] between #$data1# , #$data2#"; $rs = $con->execute($sql);
thanks!
firstly, define checkboxes array (don't use terminology "radio" - radio buttons quite different!), , change values match database records:
<div id="checkboxes"> <input type="checkbox" id="checkbox1" name="options[]" value="0" checked="checked"><label for="checkbox1">aperto</label> <input type="checkbox" id="checkbox2" name="options[]" value="1" checked="checked"><label for="checkbox2">stampato</label> <input type="checkbox" id="checkbox3" name="options[]" value="2" checked="checked"><label for="checkbox3">bloccato</label> <input type="checkbox" id="checkbox4" name="options[]" value="3" checked="checked"><label for="checkbox4">♥</label> </div>
then can use each loop loop through them, , if option present (checkboxes values don't exist in postback if box wasn't checked) add bit of sql include option in search:
$optionssql = ""; foreach($_post["options"] $index => $option) { if ($optionssql == "") $optionssql = "and stato in ("; //if it's first detected option, add in clause string $optionssql .= $option.","; } //trim trailing comma , add closing bracket of in clause instead if ($optionssql != "") { $optionssql = rtrim($optionssql, ","); $optionssql .= ")"; }
then lastly, append $optionssql
string query:
$sql="select [id ord] [id], [tipo ord] [tipo], [n ord] [numero], [data ord] [data], [ragione sociale], [indirizzo], [totimp] [importo totale], [totiva] [importo iva] [ordini] ([id ord] '$id' or [tipo ord] '$tipo' or ([data ord] between #$data1# , #$data2#))".$optionssql;
n.b. since quick example have not taken steps here guard against sql injection. should how guard against - malicious user tamper option values , try submit problematic data. should validate each option value before use in sql statement.
Comments
Post a Comment