【发布时间】:2020-02-27 13:09:21
【问题描述】:
我想创建一个表单,用户可以在其中搜索某个人在特定月份的记录。 这是我的表格:
<div class="form-row">
<div class="col-sm-4">
<select class="form-control" name="name" id="name">
<option selected="selected" style="display:none" value="">Select Employee</option>
<?php echo fill_employees($connect); ?> <!---option list--->
</select>
</div>
<div class="col-sm-2">
<select class="form-control" name="month" id="month">
<option selected="selected" style="display:none" value="0">Month</option>
<option value="1">January</option>
<option value="2">February</option>
<option value="3">March</option>
<option value="4">April</option>
<option value="5">May</option>
<option value="6">June</option>
<option value="7">July</option>
<option value="8">August</option>
<option value="9">September</option>
<option value="10">October</option>
<option value="11">November</option>
<option value="12">December</option>
</select>
</div>
<div class="col-sm-2">
<select class="form-control" name="year" id="year">
<option selected="selected" style="display:none" value="">Year</option>
<?php echo fill_year($connect); ?> <!---option list--->
</select>
</div>
</div>
<div class="row" id="show_data">Search Results</div>
下面是我的脚本:
<script>
$(document).ready(function(){
$('#name'),$('#month'),$('#year').change(function(){
var name = $(this).val();
var month = $(this).val() ;
var year = $(this).val();
$.ajax({
url:"search.php",
method:"POST",
data:{name:name,month:month,year:year},
success:function(data){
$('#show_data').html(data);
}
});
});
});
</script>
这是我的 search.php 文件
<?php
$connect = mysqli_connect("localhost", "root", "", "test");
$output = '';
echo '<div class="fixed-header col-sm-12">';
echo '<table class="table table-hover table-sm">';
echo '<thead class="thead-dark">';
echo '<th style="width:15%; text-align:center;">Day</th>';
echo '<th style="width:25%; text-align:center;">Date</th>';
echo '<th style="width:20%; text-align:center;">Time In</th>';
echo '<th style="width:20%; text-align:center;">Time Out</th>';
echo '<th style="width:20%; text-align:center;">Total Hours</th>';
echo '</thead>';
$qname = "SELECT * FROM employees";
$valid_nm = array($qname);
$valid_mo = array('1', '2', '3', '4', '5', '6', '7', '8', '9', '10', '11', '12');
$qyear = "SELECT year(timeIn) FROM attendance GROUP BY year";
$valid_yr = array($qyear);
$q = "SELECT name, timeIn, timeOut,
date(timeIn) as date,
month(timeIn) as month,
year(timeIn) as year,
TIMESTAMPDIFF(MINUTE, timeIn, timeOut)/60 AS total_hrs
FROM attendance";
// initialize array for WHERE clause conditions
$where = array('TRUE');
if (in_array($_POST['name'], $valid_nm))
{
$where[] = 'name = "' . $_POST['name'] . '"';
}
if (in_array($_POST['month'], $valid_mo))
{
$where[] = 'month(timeIn) = "' . $_POST['month'] . '"';
}
if (in_array($_POST['year'], $valid_yr))
{
$where[] = 'year(timeIn) = "' . $_POST['year'] . '"';
}
$output = '';
$sql = 'SELECT name, timeIn, timeOut,
date(timeIn) as date,
month(timeIn) as month,
year(timeIn) as year,
TIMESTAMPDIFF(MINUTE, timeIn, timeOut)/60 AS total_hrs
FROM attendance
WHERE ' . implode(' AND ', $where);
$result = mysqli_query($connect, $sql);
while ($row = mysqli_fetch_array($result))
{
$output .= '<tr>';
$output .= '<td style="width:15%; text-align:center;">'. date('l', strtotime($row["timeIn"])) .'</td>';
$output .= '<td style="width:25%; text-align:center;">'. date('d-M-Y', strtotime($row["timeIn"])) .'</td>';
$output .= '<td style="width:20%; text-align:center;">'. date('h:i A', strtotime($row["timeIn"])) .'</td>';
$int = strtotime($row["timeOut"]);
if ($int < 0)
{
$output .= '<td style="width:20%; text-align:center">NA</td>';
$output .= '<td style="width:20%; text-align:center; color:red">NA</td>';
} else {
$output .= '<td style="width:20%; text-align:center;">'. date('h:i A', strtotime($row["timeOut"])) .'</td>';
$output .= '<td style="width:20%; text-align:center;">'. number_format($row['total_hrs'],2) .'</td>';
};
$output .= '</tr>';
}
echo $output;
echo '</table>';
echo '</div>';
?>
据说它需要显示某个人的结果并选择月份和年份,但相反,它显示了我表上的所有数据。我认为错误在我的标准上,但我不知道具体在哪里。这是我第一次尝试 AJAX。
【问题讨论】:
-
警告:您对SQL Injections 持开放态度,应该使用参数化的prepared statements,而不是手动构建查询。它们由PDO 或MySQLi 提供。永远不要相信任何形式的输入!即使您的查询仅由受信任的用户执行,you are still in risk of corrupting your data。 Escaping is not enough!