我正在尝试将 3 年添加到从我的数据库返回的日期
Posted
技术标签:
【中文标题】我正在尝试将 3 年添加到从我的数据库返回的日期【英文标题】:I am trying to add 3 years to a date that is returned from my database 【发布时间】:2018-09-04 22:45:46 【问题描述】:我正在尝试为从 myphpadmin 数据库返回的每个日期添加 3 年。该代码提取了第一次上课的日期(date_aerial),我想返回一个值,例如 date_aerial+3 年。因此,如果给定的 date_aerial 是 1-24-1994,我希望返回值是 1-24-1997。这必须为从 phpmyadmin 数据库中提取的每个 ID 回显。所以airer_refresh所在的位置应该是date_aerial加上3年后返回的值。这是代码
<?php
$db_host = 'localhost';
$db_user = 'nick';
$db_pass = 'ramon';
$db_name = 'safetytraining';
?>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link href="safetytraining.css" rel="stylesheet" type="text/css" />
<title>Safety Training</title>
</head>
<body>
<div class="topnav">
<a href="http://152.116.203.115/safetytraining.php">Home</a>
<a href="http://152.116.203.115/selectcourse.php">Select Course</a>
<a href="http://152.116.203.115/calendar.php">Calendar</a>
<a class="active" href="http://152.116.203.115/editcourse.php">Edit Course</a>
</div>
<br>
<h1>Facilities Tradesmen</h1>
<input type="button" onclick="location.href='http://152.116.203.115/search_aerial2.php';" value="Back to Search" style="float: right;"/><br>
<iframe id="txtArea1" style="display:none"></iframe>
<button id="btnExport" onclick="fnExcelReport();" style="width:100px;height:50px;"> EXPORT </button><br>
</body>
<table align="center" id="myTable" >
<thead>
<tr>
<th onclick="sortTable(0)" >ID</th>
<th onclick="sortTable(1)" >First Name</th>
<th onclick="sortTable(2)" >Last Name</th>
<th onclick="sortTable(3)" >Supervisor Group</th>
<th onclick="sortTable(4)" >Trade</th>
<th onclick="sortTable(5)" >T-ID</th>
<th onclick="sortTable(6)" >C-ID</th>
<th onclick="sortTable(7)" >Department</th>
<th onclick="sortTable(8)" >Aerial Devices Training <br>#:NTCHST01</th>
<th onclick="sortTable(9)" >Refresh Date</th>
<th onclick="sortTable(10)" >Edit Data</th>
</tr>
</thead>
<tbody>
</html>
<?php
$conn = mysqli_connect($db_host, $db_user, $db_pass, $db_name);
if (!$conn)
die ('Fail to connect to MySQL: ' . mysqli_connect_error());
$value1 = $_POST['searchTP'];
$value2 = $_POST['searchTR'];
$value3 = $_POST['searchTD'];
$value4 = $_POST['searchCD'];
$value5 = $_POST['searchDP'];
$value6 = $_POST['searchAD'];
$value10 = $_POST['searchSG'];
$value11=$_POST['date_aerial'];
$date = date_create('2000-01-01');
date_add($date, date_interval_create_from_date_string('10 years'));
echo date_format($date, 'm-d-Y');
echo '<br>';
echo "<font color=white size='4pt'> You searched for Tradesperson:</font><font color=red size='4pt'> $value1</font>";
echo '<br>';
echo "<font color=white size='4pt'> You searched for Trade:</font><font color=red size='4pt'> $value2</font>";
echo '<br>';
echo "<font color=white size='4pt'> You searched for T-ID:</font><font color=red size='4pt'> $value3</font>";
echo '<br>';
echo "<font color=white size='4pt'> You searched for C-ID:</font><font color=red size='4pt'> $value4</font>";
echo '<br>';
echo "<font color=white size='4pt'> You searched for Department:</font><font color=red size='4pt'> $value5</font>";
echo '<br>';
// Add conditional statement here that if value1 or value is blank then search the other.
$sql = "SELECT ID, first_name, last_name, supervisor_group, trade, t_id, c_id, department_number, date_aerial, aerial_refresh
FROM peopleinfo WHERE
(last_name LIKE '%$value1%' )
and
(trade LIKE '%$value2%')
and
(t_id LIKE '%$value3%')
and
(c_id LIKE '%$value4%')
and
(department_number LIKE '%$value5%')
and
(date_aerial LIKE '%$value6%')
and
(date_lock LIKE '%$value7%')
and
(date_confine LIKE '%$value8%')
and
(date_fall LIKE '%$value9%')
and
(supervisor_group LIKE '%$value10%')";
$query = mysqli_query($conn, $sql);
if (!$query)
die ('SQL Error: ' . mysqli_error($conn));
while ($row = mysqli_fetch_array($query))
echo '<tr>
<td>'.$row['ID'].'</td>
<td>'.$row['first_name'].'</td>
<td>'.$row['last_name'].'</td>
<td>'.$row['supervisor_group'].'</td>
<td>'.$row['trade'].'</td>
<td>'.$row['t_id'].'</td>
<td>'.$row['c_id'].'</td>
<td>'.$row['department_number'].'</td>
<td>'.$row['date_aerial'].' </td>
<td>'.$row['aerial_refresh'].' </td>
<td><a href="edit.php?ID=' . $row['ID'] . '">Edit</a></td>
</tr>';
mysqli_free_result($query);
mysqli_close($conn);
?>
</tbody>
</table>
任何帮助都将提前非常感谢。
【问题讨论】:
phpMyAdmin
= 一个用 PHP 编写的应用程序,可帮助管理 MySQL 数据库。数据库是MYSQL
看看the manual里面已经有一个完美的例子
Or here
您的脚本对SQL Injection Attack 甚至if you are escaping inputs, its not safe! 都是开放的,在MYSQLI_
或PDO
API 中使用prepared parameterized statements
【参考方案1】:
您可以输入您选择的“DATE_ADD (date_aerial, INTERVAL +3 Year)”
这样
$sql = "SELECT ID, first_name, last_name, supervisor_group,
trade, t_id, c_id, department_number,
DATE_ADD (date_aerial, INTERVAL +3 Year),
aerial_refresh
FROM peopleinfo
WHERE (last_name LIKE '%$value1%' )
and (trade LIKE '%$value2%')
and (t_id LIKE '%$value3%')
and (c_id LIKE '%$value4%')
and (department_number LIKE '%$value5%')
and (date_aerial LIKE '%$value6%')
and (date_lock LIKE '%$value7%')
and (date_confine LIKE '%$value8%')
and (date_fall LIKE '%$value9%')
and (supervisor_group LIKE '%$value10%')";
使用此函数,您无需对代码执行任何操作。
让我知道它是否适合您。
【讨论】:
嗨,Marcelino,如果您将该评论的内容编辑为您的原始答案(使用答案文本下方的edit
链接)会更清楚。
和here is the doc 了解如何格式化代码
谢谢尼克,我只是没有看到链接。
它没有用,它给了我这个错误“SQL 错误:您的 SQL 语法有错误;请查看与您的 MySQL 服务器版本相对应的手册以获取在 ' 附近使用的正确语法), air_refresh FROM peopleinfo WHERE (last_name LIKE '%%' ) an' at line 1"以上是关于我正在尝试将 3 年添加到从我的数据库返回的日期的主要内容,如果未能解决你的问题,请参考以下文章
将事件添加到从服务器FullCalendar返回的事件数组中