如何根据HTML表单字段中填写的信息从我的SQL查询中检索特定信息?

时间:2014-04-24 17:38:35

标签: php

我是PHP和其他所有新手,所以请尽量保持任何响应。谢谢。我只是无法弄清楚如何只从填​​写的字段中获取信息。当我进行搜索时,它也会显示空字段。我只需要找到HTML表单中的信息。例如,如果我输入项目代码,我需要所有与该代码相关的信息。或者,如果我投入特定预算,我需要有预算的所有内容。我希望这是有道理的...我是这样的新秀,我甚至不知道如何说出这个问题!我尝试过使用!is_null,(isset),!空,但它们似乎都没有用。

<?php

include 'connect.php';


// Get values from form 
$p_code=$_GET['projectcode'];
$p_name=$_GET['projectname'];
$budget=$_GET['budget'];
$s_date=$_GET['startdate'];
$e_date=$_GET['enddate'];
$c_year=$_GET['cyear'];
$c_qtr=$_GET['cqtr'];
$c_grp=$_GET['cgrp'];
$sponsor=$_GET['sponsor'];
$client=$_GET['client'];
$p_lead=$_GET['projectlead'];
$orig_hrs=$_GET['originalhrs'];
$risk_per=$_GET['riskpercent'];
$notes=$_GET['notes'];

if ($db_found) {



$SQL = "SELECT * FROM minimodtable 
WHERE projectcode='$p_code' 
OR projectname='$p_name'   
OR budget='$budget' 
OR startdate='$s_date' 
OR enddate='$e_date' 
OR cyear='$c_year' 
OR cqtr='$c_qtr' 
OR cgrp='$c_grp' 
OR sponsor='$sponsor' 
OR client='$client' 
OR projectlead='$p_lead' 
OR originalhrs='$orig_hrs' 
OR riskpercent='$risk_per' 
OR notes='$notes'";

$result = mysqli_query($connect, $SQL);

echo "<table border='1'>
<tr>
<th>Project Code</th>
<th>Project Name</th>
<th>Budget</th>
<th>Start Date</th>
<th>End Date</th>
<th>Year</th>
<th>Quarter</th>
<th>Group</th>
<th>Sponsor</th>
<th>Client</th>
<th>Project Lead</th>
<th>Original Hours</th>
<th>Risk Percent</th>
<th>Notes</th>
</tr>";


while ($row = mysqli_fetch_array($result, MYSQLI_BOTH)) {


echo "<tr>";
echo "<td>" . $row['projectcode'] . "</td>";
echo "<td>" . $row['projectname'] . "</td>";
echo "<td>" . $row['budget'] . "</td>";
echo "<td>" . $row['startdate'] . "</td>";
echo "<td>" . $row['enddate'] . "</td>";
echo "<td>" . $row['cyear'] . "</td>";
echo "<td>" . $row['cqtr'] . "</td>";
echo "<td>" . $row['cgrp'] . "</td>";
echo "<td>" . $row['sponsor'] . "</td>";
echo "<td>" . $row['client'] . "</td>";
echo "<td>" . $row['projectlead'] . "</td>";
echo "<td>" . $row['originalhrs'] . "</td>";
echo "<td>" . $row['riskpercent'] . "</td>";
echo "<td>" . $row['notes'] . "</td>";

}


echo "</table>";



}

mysqli_close($connect);



?>

1 个答案:

答案 0 :(得分:0)

你在那里做了第一次尝试。不过,这种方法可以让你不必重复自己。您开始查看isset是正确的 - 问题是您需要动态构建SQL查询。

事实上,您可能希望更改implode()功能中的粘合剂来自&#39; OR&#39;到&#39; AND&#39; - 这样,您就可以找到与特定代码相关的特定预算。

我现在已将其切换为AND - 但请根据您的示例随意将其切换回OR

<?php

// All possible parameters
$params = array(
    'projectcode',
    'projectname',
    'budget',
    'startdate',
    'enddate',
    'cyear',
    'cqtr',
    'cgrp',
    'sponsor',
    'client',
    'projectlead',
    'originalhrs',
    'riskpercent',
    'notes'
);

$wheres = array();
foreach ($params as $param) {

    // Is the param set?
    // If so, let's add it to our list of WHERE clauses
    if (isset($_GET[$param])) {
        $wheres[] = sprintf("%s = '%s'", $param, mysqli_real_escape_string($connect, $_GET[$param]));
    }
}

if ($db_found) {

    // Now let's make the SQL.
    $SQL = 'SELECT * FROM minimodtable';

    // We only want to add the WHERE clause if we had some parameters passed
    if (count($wheres) > 0) {
        $SQL .= ' WHERE ' . implode(' AND ', $wheres);
    }

    $result = mysqli_query($connect, $SQL);

    echo "<table border='1'>
    <tr>
    <th>Project Code</th>
    <th>Project Name</th>
    <th>Budget</th>
    <th>Start Date</th>
    <th>End Date</th>
    <th>Year</th>
    <th>Quarter</th>
    <th>Group</th>
    <th>Sponsor</th>
    <th>Client</th>
    <th>Project Lead</th>
    <th>Original Hours</th>
    <th>Risk Percent</th>
    <th>Notes</th>
    </tr>";

    while ($row = mysqli_fetch_array($result, MYSQLI_BOTH)) {

        echo "<tr>";
        echo "<td>" . $row['projectcode'] . "</td>";
        echo "<td>" . $row['projectname'] . "</td>";
        echo "<td>" . $row['budget'] . "</td>";
        echo "<td>" . $row['startdate'] . "</td>";
        echo "<td>" . $row['enddate'] . "</td>";
        echo "<td>" . $row['cyear'] . "</td>";
        echo "<td>" . $row['cqtr'] . "</td>";
        echo "<td>" . $row['cgrp'] . "</td>";
        echo "<td>" . $row['sponsor'] . "</td>";
        echo "<td>" . $row['client'] . "</td>";
        echo "<td>" . $row['projectlead'] . "</td>";
        echo "<td>" . $row['originalhrs'] . "</td>";
        echo "<td>" . $row['riskpercent'] . "</td>";
        echo "<td>" . $row['notes'] . "</td>";

    }

    echo "</table>";
}

mysqli_close($connect);