[英]How can I retrieve specific information from my sql query based on what information is filled in the HTML form fields?
我是PHP和其他所有事物的新手,所以请尽量使任何响应尽可能简单。 谢谢。 我只是想不通如何仅从填写的字段中获取信息。当我进行搜索时,它也会显示空白字段。 我只需要找到HTML表单中的信息。 例如,如果我输入了项目代码,则需要与该代码有关的所有信息。 或者,如果我输入了特定预算,则需要拥有该预算的所有内容。 我希望这是有道理的...我真是个菜鸟,我什至不知道该如何措辞!!!!! 我试过使用!is_null,(isset)、! empty,但它们似乎都不起作用。
<?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);
?>
您已经尝试过了。 不过,这种方法应该可以省去重复很多次的麻烦。 您开始研究isset
是正确的-问题是您需要动态构建SQL查询。
实际上,您可能需要将implode()
函数中的粘合力从“或”更改为“与”-这样,您可以找到特定预算且与特定代码相关的内容。
我已经将它切换到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);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.