通过传递相同参数的多个值来搜索Mysql Db中的值

时间:2018-05-09 12:54:31

标签: php mysql

我想从我的网站生成某些条件的报告,该网站是一个Web应用程序,用于节省故障时间和公司机器的详细信息。这里我想根据故障类型,故障机器,故障等产生报告,。

所以我创建了一个搜索表单,我已经使用了数据库中的动态下拉菜单,并成功创建了一个代码,可以选择多个值(在机器下 - 我们可以选择机器名称,如machine1,machine2和等等)按下搜索后,它只显示最后一个选定值的结果,但我想显示所有选定值的结果(详细信息)。

**form.php**

<?php
//index.php
include('database_connection.php');
$country = '';
$query = "
SELECT type FROM reporttype
";
$statement = $connect->prepare($query);
$statement->execute();
$result = $statement->fetchAll();
foreach($result as $row)
{
$country .= '<option value="'.$row["type"].'">'.$row["type"].'</option>';
}
?>
<div class="container">
<h2 align="center"></h2><br /><br />
<form method="post" id="insert_data" action="insert.php">
<label class="col-md-2 bdlabel">Break Down</label>
<select name="country" id="country" class="col-md-10 form-control action">
<option value="">Choose a Category</option>
<?php echo $country; ?>
</select>
<br />
<select name="city" id="city" multiple class="form-control">
</select>
<br />
<input type="hidden" name="hidden_city" id="hidden_city" />
<div class="form-group">
<div class="datepic">
<label class="col-md-2 bdlabel bdfrom">From</label>
<input type="date" name="bdfrom" class="col-md-4">
</div>
<div class="datepic1">
<label class="col-md-2 bdlabel bdto">To</label>
<input type="date" name="bdto" class="col-md-4">
</div>
</div>
<br/>
<input type="submit" name="search" id="action" class="btn btn-info" 
value="Search" />
</form>
</div>
<script>
// JavaScript Document
$(document).ready(function(){
$('#city').lwMultiSelect();
$('.action').change(function(){
if($(this).val() != '')
{
var action = $(this).attr("id");
var query = $(this).val();
var result = '';
if(action == 'country')
{
result = 'city';
}
$.ajax({
url:'fetch.php',
method:"POST",
data:{action:action, query:query},
success:function(data)
{
$('#'+result).html(data);
if(result == 'city')
{
$('#city').data('plugin_lwMultiSelect').updateList();
}
}
})
}
});
});
</script>

insert.php

<?php
//insert.php
include('db.php');
if(isset($_POST['search']))
$city=$_POST['city'];
$from=$_POST['bdfrom'];
$to=$_GET['bdto'];
{
$raw_results = mysql_query("SELECT * FROM breakdownentry
WHERE bdmwc in ('$city') || bdmname in ('$city') || bdaname in ('$city') ") 
or die(mysql_error());         
if(mysql_num_rows($raw_results) > 0){ // if one or more rows are returned do following         
while($results = mysql_fetch_array($raw_results)){             
echo "<p>".$results['bdmwc']."</p>";
}
}
else{ 
echo "No results";
}
}
?>

Actual Form

Output I got

Db table value

What i want to show is wire drawing and nail but only nail value is been shown.

2 个答案:

答案 0 :(得分:2)

要在表单上获取多个值,您应在NSPredicate属性的末尾添加[]

对于您的示例:

name

答案 1 :(得分:0)

终于找到了答案,我必须使用implode函数来通过数组获取多个值。在搜索查询中使用implode函数后,我能够获得多个值并得到我想要的结果。感谢@RiggsFolly和@OlgertElezi的帮助,select标签中的方括号有助于传递数组值。

我附上了我的工作代码。再次感谢evryone

<?php
                                    //insert.php
                                        include('db.php');
                                        if(isset($_POST['search'])) 
                                            $category=($_POST["country"]);                                  
                                            $machine=($_POST["city"]);
                                            $from=($_POST["bdfrom"]);
                                            $to=($_POST["bdto"]);

                                                $raw_results = mysql_query("SELECT * FROM breakdownentry WHERE ((bddate BETWEEN '$from' AND '$to')) && (bdmname IN ('".implode("','",$machine)."') || bdmwc IN ('".implode("','",$machine)."') || bdaname IN ('".implode("','",$machine)."')) ") or die(mysql_error());

                                                if(mysql_num_rows($raw_results) > 0){
                                    ?>
                                    <div class="widget-content2 resulttable">
                                        <label class="tableheadtb"><?php echo $category.' Break Down details';?></label>
                                        <div class="table-responsive">
                                            <table class="table table-bordered ">
                                                <?php

                                                    // if one or more rows are returned do following
                                                    $limit = 10;  
                                                    if (isset($_GET["page"])) { $page  = $_GET["page"]; } else { $page=1; };  
                                                        $start_from = ($page-1) * $limit;  

                                                        $sql = "SELECT * FROM breakdownentry LIMIT $start_from, $limit";  
                                                        $rs_result = mysql_query ($sql);    
                                                        $a=$start_from + 0;
                                                        $efslno=$start_from + 1354;

                                                ?>

                                                 <thead>
                                                        <tr style="background-color:#FDFE90">
                                                        <th><strong>S.No</strong></th>
                                                        <th><strong>Complaint-Date</strong></th>
                                                        <th><strong>B/D S.No</strong></th>
                                                        <th><strong>Machine ID</strong></th>
                                                        <th><strong>Machine Name</strong></th>
                                                        <th><strong>Work Centre</strong></th>
                                                        <th><strong>B/D ID</strong></th>
                                                        <th><strong>Attended by(ID)</strong></th>
                                                        <th><strong>Attended by Name</strong></th>
                                                        <th><strong>B/D Type</strong></th>                                                  
                                                        <th><strong>B/D Time (Hours)</strong></th>
                                                        <th><strong>B/D Time (Minutes)</strong></th>
                                                        <th><strong>B/D Time (Total)</strong></th>
                                                        <th><strong>B/D Details</strong></th>
                                                        <th><strong>Work Done</strong></th>
                                                        <th><strong>Complaint Status</strong></th>                  
                                                    </tr>
                                                </thead>

                                                <?php   
                                                    while($results = mysql_fetch_array($raw_results)){ 

                                                ?>                                                                                              
                                                <tbody>

                                                    <tr>
                                                        <td><?php echo ++$a;?></td>
                                                        <td><?php echo $results['bddate'];?></td>
                                                        <td><?php 
                                                                $slno = ++$efslno;
                                                                echo $slno;?>
                                                        </td>
                                                        <td><?php echo $results['bdmid'];?></td>
                                                        <td><?php echo $results['bdmname'];?></td>
                                                        <td><?php echo $results['bdmwc'];?></td>
                                                        <td><?php echo $results['bdbdid'];?></td>
                                                        <td><?php echo $results['bdtid'];?></td>
                                                        <td><?php echo $results['bdtname'];?></td>
                                                        <td><?php echo $results['bdbdname'];?></td>                                                         
                                                        <td><?php 
                                                                $bdbreakoccur1=$results['bdbreakreport'];
                                                                $bdamotime1=$results['bdamotime'];
                                                                $time=mysql_fetch_array(mysql_query("SELECT (HOUR (TIMEDIFF ('$bdamotime1','$bdbreakoccur1')))"));                                                              
                                                                echo $time[0] ;
                                                            ?>
                                                        </td>
                                                        <td><?php 
                                                                $bdbreakoccur1=$results['bdbreakreport'];
                                                                $bdamotime1=$results['bdamotime'];
                                                                $time=mysql_fetch_array(mysql_query("SELECT (MINUTE (TIMEDIFF ('$bdamotime1','$bdbreakoccur1')))"));                                                                
                                                                echo $time[0] ;
                                                            ?>
                                                        </td>
                                                        <td><?php 
                                                                $bdbreakoccur1=$results['bdbreakreport'];
                                                                $bdamotime1=$results['bdamotime'];
                                                                $time=mysql_fetch_array(mysql_query("SELECT ((HOUR (TIMEDIFF ('$bdamotime1','$bdbreakoccur1'))*(60)) + (MINUTE (TIMEDIFF('$bdamotime1','$bdbreakoccur1'))))/60"));
                                                                echo $time[0] ;
                                                            ?>
                                                        </td>
                                                        <td><?php echo $results['bdacause'];?></td>                                                         
                                                        <td><?php echo $results['bdawdc'];?></td>
                                                        <td><?php echo $results['bdaradio'];?></td>
                                                    </tr>
                                                </tbody>
                                                <?php           
                                                    }
                                                ?>
                                            </table>
                                        </div>
                                        <?php  
                                            $sql = "SELECT COUNT(id) FROM breakdownentry";  
                                            $rs_result = mysql_query($sql);  
                                            $row = mysql_fetch_row($rs_result);  
                                            $total_records = $row[0];  
                                            $total_pages = ceil($total_records / $limit);  
                                            $pagLink = "<div class='widget-foot'>";  
                                            for ($i=1; $i<=$total_pages; $i++) {  
                                                $pagLink .= "<a href='break-down-list.php?page=".$i."'>".$i."</a>";  
                                                };  
                                            echo $pagLink . "</div>";
                                            }
                                            else{ // if there is no matching rows do following
                                                    echo "<p class='ep'>No Results to Show</p>";
                                            }  
                                        ?>
                                    </div>