duanque2413 2016-06-03 00:49
浏览 34
已采纳

如何做mysql db的多部分PHP查询

I have read through hundreds of posts on here and on other sites. I am not overly familiar with PHP and mysql, but already have my tables built in mysql, have called them using single checklists and filtered them based on date and time. However, in the multi-checkbox arena, I cannot seem to pin down what I'm doing wrong for an array code and customizing the resulting table. This is the basic html:

<html> <body> <form method="POST" action="php/minute_feedback.php">
Date: <input type="text" name="from_date" id="from_date" value="2016-04-07">
<input type="checkbox" name="check_list[]" value="gate" id="gate">
<input type="checkbox" name="check_list[]" value="pressure" id="pressure">
<input type="checkbox" name="check_list[]" value="flow" id="flow">
<input type="submit" name="submit" value="submit">

This is the basic PHP (I'll only include the date parameters vs. date & time to keep it shorter since the inclusion will be the same):

<?php
$servername = "myhostaddress.com";
$username = "myusername";
$password = "mypassword";
$dbname = "mydatabasename";
$conn=new mysqli($servername, $username, $password, $dbname);
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);}
if($_POST['submit'] == "submit") {
$from_date = ($_POST['from_date']);
$to_date = ($_POST['from_date']);}
if(isset($_POST['submit'])) {

Now ... I know there should be some sort of an array code in here like:

$checklist[] = $_POST[check_list];

But - I'm not sure what the correct syntax/code is. Next question is about my query. I've seen most queries using a WHERE + IN combo and a '*' for SELECT. However, I need something closer to:

$sql = "SELECT Fdate, Ftime, (list of variable columns from the checked options such as 'gate', 'pressure', 'flow' --> but only if they've been checked) FROM minute_db WHERE Fdate BETWEEN '$from_date' AND '$to_date'";

$result = $conn->query($sql);}
if ($result->num_rows > 0) {

Now - as for my table headers, I can use the following code if I had a single value being looked up:

echo "<table><tr><th>Date</th><th>Time</th><th>Level (ft)</th></tr>";}

But, I need the header to be dynamically populated with the results of the checked items (1, 2, or 3 or more variables as needed) plus the date and time records.

echo "<table>";}
else {
echo "0 results";}
$conn->close();
?>

My end result is that the user can enter a date and time range and choose which data to display, so for example, if they want to know just the gate setting and pressure on 4/7/2016, they'd enter that date and check those two boxes so they will get a table (no formatting below - just trying to represent), for example:

Date       |     Time |   Gate Setting  |  Pressure 
----------------------------------------------------
2016-04-07 |    11:00 |      21         |      50 

I will ultimately have a table with tens of thousands of rows of data to select from (and will include limiters to the amount of data that can be pulled), and up to 56 columns of variables to select from. I'm not using any special/fancy math functions to group time periods, etc. - I've already manually separated it all out and uploaded it so it's ready to go. I've tried a lot of different solutions from the internet, but have ultimately failed. The * option for SELECT doesn't seem like what I what, neither does the IN option - but I am not sure (just speculating as neither has worked). I'm on a netfirms mysql platform, so it's pretty updated (and I'll be changing everything to mysqli to avoid whatever problems stem from not using it - so any feedback with that in advance would be AWESOME!!. I appreciate any ideas you have!

  • 写回答

2条回答 默认 最新

  • douyi5822 2016-06-03 01:30
    关注
    $checklist     = $_POST['check_list'];
    $allowedFields = [
        'gate'     => 'Gate',
        'pressure' => 'Pressure',
        'flow'     => 'Flow',
        'etc'      => 'Other field title'
    ];
    $wantedFields   = [];
    $tableDynFields = [];
    foreach ($checklist as $field) {
        if (array_key_exists($field, $allowedFields)) {
            $wantedFields[]   = $field;
            $tableDynFields[] = $allowedFields[$field];
        }
    }
    if ($wantedFields) {
        $wantedFields = join(',', $wantedFields) . ',';
    } else {
        $wantedFields = '';
    }
    $sql = "SELECT Ftime, {$wantedFields} Fdate FROM ...';
    // and here you have $tableDynFields array for table generation
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 Mac系统vs code使用phpstudy如何配置debug来调试php
  • ¥15 目前主流的音乐软件,像网易云音乐,QQ音乐他们的前端和后台部分是用的什么技术实现的?求解!
  • ¥60 pb数据库修改与连接
  • ¥15 spss统计中二分类变量和有序变量的相关性分析可以用kendall相关分析吗?
  • ¥15 拟通过pc下指令到安卓系统,如果追求响应速度,尽可能无延迟,是不是用安卓模拟器会优于实体的安卓手机?如果是,可以快多少毫秒?
  • ¥20 神经网络Sequential name=sequential, built=False
  • ¥16 Qphython 用xlrd读取excel报错
  • ¥15 单片机学习顺序问题!!
  • ¥15 ikuai客户端多拨vpn,重启总是有个别重拨不上
  • ¥20 关于#anlogic#sdram#的问题,如何解决?(关键词-performance)