Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/shell/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 动态创建mysql选择查询_Php_Html_Mysql - Fatal编程技术网

Php 动态创建mysql选择查询

Php 动态创建mysql选择查询,php,html,mysql,Php,Html,Mysql,我的网站中有多个复选框: 复选框1 复选框2 复选框3等 我想基于上述复选框动态生成mysql查询 i:e如果选中第一个复选框,则查询应为: $mysql="Select * from mytable where colname=" . $checkbox1 .; $mysql="Select * from mytable where colname=" . $checkbox1 ."AND colname=" . $checkbox2 ." ; 如果选中第一个和第二个复选框,则查询应为:

我的网站中有多个复选框: 复选框1 复选框2 复选框3等

我想基于上述复选框动态生成mysql查询

i:e如果选中第一个复选框,则查询应为:

$mysql="Select * from mytable where colname=" . $checkbox1 .;
$mysql="Select * from mytable where colname=" . $checkbox1 ."AND colname=" . $checkbox2 ." ;
如果选中第一个和第二个复选框,则查询应为:

$mysql="Select * from mytable where colname=" . $checkbox1 .;
$mysql="Select * from mytable where colname=" . $checkbox1 ."AND colname=" . $checkbox2 ." ;
如果选择了“全部”,则应为:

$mysql="Select * from mytable where colname=" . $checkbox1 . "AND colname=" . $checkbox2 ."AND colname=" . $checkbox3 .  ;
有人能帮忙吗:

有什么事吗

<? if ($_POST[option]==1) { 
      //query 
   } 
?>

<form method="post">
   <input type="checkbox" name="option" value="1">1
   <input type="checkbox" name="option" value="2">2
   <input type="checkbox" name="option" value="3">3
</form>

1.
2.
3.

只需检查您的复选框是否通过(isset($\u POST[checkbox1])选择即可

所以你可以做一些像

if (isset($_POST[checkbox1])
{
//statement
}
else if (isset($_POST[checkbox2])
{
//statement2...
}
...


干杯

你必须像follow那样改变你的表单,因为它有多个值,应该以数组的形式发布

<form action="register.php" method="POST"> 
  <input type="checkbox" name="rating[]" value="5">5 Star 
  <input type="checkbox" name="rating[]" value="4">4 Star 
  <input type="checkbox" name="rating[]" value="3">3 Star 
  <input type="checkbox" name="rating[]" value="2">2 Star 
  <input type="checkbox" name="rating[]" value="1">Less than 2 Star 
</form>

您可以使用字符串连接和一些技巧来完成这项工作。不要依赖isset,而是使用!空的

<form action="example.php" method="post">
    <input type="checkbox" name="Col1" value="colname" />Col 1</br>
    <input type="checkbox" name="Col2" value="colname" />Col 2</br>
    <input type="checkbox" name="Col3" value="colname" />Col 3</br>
</form>
<?php
if(!empty($_POST)) {
    $string = '';
    if(!empty($_POST['col1'])) {

        $string.= "column1 ='".$_POST['col1']."'";
    }   
    if(!empty($_POST['col2'])){

        if(!empty($string)) {

            $string.=" AND ";

        } 
        $string.= "column2 ='".$_POST['col2']."'";

    }
    if(!empty($_POST['col3'])){
        if(!empty($string)) {

            $string.=" AND ";

        } 

        $string .= "column3 ='".$_POST['col3']."'";
    }

    if(!empty($string)) 
    {
        //execute your query here.
    }
}

第1列
第2列
第3列

从下面提供的代码可以明显看出,我当然不是PHP程序员。但无论如何,这里有一个想法要考虑

使用0到127之间的
输入
的不同值进行尝试,例如
?输入=66

<?php

if(isset($_GET['input'])){

   $input = $_GET['input'];

   }else{

   $input=0;
}

$days = array('mon', 'tue', 'wed', 'thu', 'fri', 'sat', 'sun');

for( $i=0; $i<7; $i++ ) {
    $daybit = pow(2,$i);
    if( $input & $daybit ) {
        echo "<input type = checkbox checked/>$days[$i]";
    }else{
        echo "<input type = checkbox>$days[$i]";
}
}
?>


你几乎已经把一切都弄明白了。现在你只需要写它!谢谢,但我的问题是针对选中多个复选框的情况。您能为复选框指定html吗?这里是:5星4星3星2星小于2星,但当选中多个复选框时会发生什么。例如,有10个复选框:1,2,3…10。