Please assist? I have a database with a record table. Two of the fields that I am trying to calculate to get a certain number is not working. I am really new to php an would appreciate any help.
请协助?我有一个带记录表的数据库。我试图计算得到某个数字的两个字段不起作用。我真的很新的PHP会很感激任何帮助。
My table has 2 fields named "startdate" and "terminationdate" these are the two I would like to echo out after a calculation has been done for them
我的表有两个名为“startdate”和“terminationdate”的字段,这些是我想在为它们完成计算后回应的两个字段
The Code:
<?php
include 'core/init.php';
protect_page();
include 'includes/overall/header.php';
error_reporting(1);
?>
<div class="article">
<h2>
<br>
SEARCH A REFERENCE
<br>
</h2>
</center>
<body>
<div id="formsearch">
<br>
<form method="post" action="" name="form1" id="form1" >
Enter the ID Number
<br><br>
<b>Search Record </b> <input type="text" name="term" /><br />
<br>
<input type="submit" name="submit" value="Search" class="btn" />
</form>
</div>
<h3> <center> Search Result (s) </h3>
<?php
if ($_REQUEST['submit']) {
$term = $_POST['term'];
$row ['employerid'] == $user_data ['user_id'];
$XX = "<br><br><div class='messagebox'><h2> <center> Oops! </h2> <p>We were only to retrieve a partial record on <strong>$term</strong> you have entered. Please make use of our contact form if you would like us to get you your reference. Be sure to enter the three required fields. <a href='Mailforms/refrequest.php' class='lightbox'>Click Here!</a> or to validate the id <a href='idverification.php'> Click here</a></p>
<br />
</div>";
$sql = mysql_query("SELECT idnumber,
firstname,
lastname,
companyname,
jobtitle,
dismissal,
floor(terminationdate - startdate) + ' days ' +
MOD(FLOOR ((terminationdate - startdate) * 24), 24) + 'hours' +
MOD (FLOOR ((terminationdate - startdate) * 24 * 60), 60) + ' minutes ' 'time_diff'
FROM ref_employees
WHERE idnumber= '$term'")
or die('Error in query : $sql. ' .mysql_error());
if (mysql_num_rows($sql) > 0 ) {
while ($row = mysql_fetch_array($sql)){
if ($row ['employed'] == '1') {
echo '<h4>Currently Employed By : '.$row['companyname'];
echo '</h4> ';
echo '<a href="#">Any doubts? Enquire about this candidate</a> ';
}
if ($row ['employed'] == '0') {
echo ' <h4>Some Available Options For:</h4>';
include 'includes/admenu.php';
echo '<h4>Not Currently employed '.$row['companyname'];
echo '</h4> ';
}
echo "<table border='1' cellpadding='10'>";
echo "<tr> <th>ID Number</th> <th>First Name</th> <th>Last Name</th><th>company</th> <th>Job Title</th> <th>Period</th><th>Reason for dismissal</th><th></th><th></th></tr>";
echo "<tr>";
echo '<td>' . $row['idnumber'] . '</td>';
echo '<td>' . $row['firstname'] . '</td>';
echo '<td>' . $row['lastname'] . '</td>';
echo '<td>' . $row['companyname'] . '</td>';
echo '<td>' . $row['jobtitle'] . '</td>';
echo '<td>' .($row['startdate'] + $row['terminationdate']).'</td>';
echo '<td>' . $row['dismissal'] . '</td>';
echo '<td><a href="editemp.php?idnumber=' . $row['idnumber'] . '">Achievements</a></td>';
echo '<td><a href="delete.php?idnumber=' . $row['idnumber'] . '">training</a></td>';
echo "</tr>";
}
// close table>
echo "</table>";
}
}
else
{
print ("$XX");
}
mysql_free_result($sql);
mysql_close($connection);
?>
<div>
</div>
<?php
include 'includes/overall/footer.php'; ?></div>
1 个解决方案
#1
1
You are adding the index of your array not the values here:
您正在添加数组的索引而不是此处的值:
echo '<td>' . $row['$sdate+$edate'] . '</td>';
I suggest that you do the computation on MySQL query before fetch and explicitly define all the column names like:
我建议您在获取之前对MySQL查询进行计算,并明确定义所有列名称,如:
SELECT startdate + terminationdate WHERE....
Or do it in PHP like this:
或者在PHP中这样做:
echo '<td>' .($row['startdate'] + $row['terminationdate']).'</td>';
Now from what I undestand, you are trying to compute for the difference of the two dates to get the duration using TIMEDIFF()
. Here it is:
现在,根据我的观点,您正在尝试使用TIMEDIFF()来计算两个日期的差异以获得持续时间。这里是:
SELECT idnumber, firstname, lastname, companyname,jobtitle,dismissal TIMEDIFF(startdate,terminationdate) time_diff WHERE idnumber= '$term'
In PHP:
$sql = mysql_query("SELECT idnumber,
firstname,
lastname,
companyname,
jobtitle,
dismissal,
floor(terminationdate - startdate) + ' days ' +
MOD(FLOOR ((terminationdate - startdate) * 24), 24) + 'hours' +
MOD (FLOOR ((terminationdate - startdate) * 24 * 60), 60) + ' minutes ' 'time_diff'
FROM ref_employees
WHERE idnumber= '$term'")
or die('Error in query : $sql. ' .mysql_error());
On your comment you want it to be computed using php. Here it is:
在你的评论,你希望它使用PHP计算。这里是:
$date1 = $row['startdate'];
$date2 = $row['terminationdate'];
$diff = abs(strtotime($date2) - strtotime($date1)); // change here if plus or minus
$years = floor($diff / (365*60*60*24));
$months = floor(($diff - $years * 365*60*60*24) / (30*60*60*24));
$days = floor(($diff - $years * 365*60*60*24 - $months*30*60*60*24)/ (60*60*24));
* Note that the mysql extension is now deprecated and will be removed sometime in the future. That's because it is ancient, full of bad practices and lacks some modern features. Don't use it to write new code. Use PDO or mysqli_* instead. Your query is prone to SQL Injection.
*请注意,mysql扩展现在已弃用,将来某个时候将被删除。那是因为它古老,充满了不良做法,缺乏一些现代特色。不要用它来编写新代码。请改用PDO或mysqli_ *。您的查询很容易出现SQL注入。
#1
1
You are adding the index of your array not the values here:
您正在添加数组的索引而不是此处的值:
echo '<td>' . $row['$sdate+$edate'] . '</td>';
I suggest that you do the computation on MySQL query before fetch and explicitly define all the column names like:
我建议您在获取之前对MySQL查询进行计算,并明确定义所有列名称,如:
SELECT startdate + terminationdate WHERE....
Or do it in PHP like this:
或者在PHP中这样做:
echo '<td>' .($row['startdate'] + $row['terminationdate']).'</td>';
Now from what I undestand, you are trying to compute for the difference of the two dates to get the duration using TIMEDIFF()
. Here it is:
现在,根据我的观点,您正在尝试使用TIMEDIFF()来计算两个日期的差异以获得持续时间。这里是:
SELECT idnumber, firstname, lastname, companyname,jobtitle,dismissal TIMEDIFF(startdate,terminationdate) time_diff WHERE idnumber= '$term'
In PHP:
$sql = mysql_query("SELECT idnumber,
firstname,
lastname,
companyname,
jobtitle,
dismissal,
floor(terminationdate - startdate) + ' days ' +
MOD(FLOOR ((terminationdate - startdate) * 24), 24) + 'hours' +
MOD (FLOOR ((terminationdate - startdate) * 24 * 60), 60) + ' minutes ' 'time_diff'
FROM ref_employees
WHERE idnumber= '$term'")
or die('Error in query : $sql. ' .mysql_error());
On your comment you want it to be computed using php. Here it is:
在你的评论,你希望它使用PHP计算。这里是:
$date1 = $row['startdate'];
$date2 = $row['terminationdate'];
$diff = abs(strtotime($date2) - strtotime($date1)); // change here if plus or minus
$years = floor($diff / (365*60*60*24));
$months = floor(($diff - $years * 365*60*60*24) / (30*60*60*24));
$days = floor(($diff - $years * 365*60*60*24 - $months*30*60*60*24)/ (60*60*24));
* Note that the mysql extension is now deprecated and will be removed sometime in the future. That's because it is ancient, full of bad practices and lacks some modern features. Don't use it to write new code. Use PDO or mysqli_* instead. Your query is prone to SQL Injection.
*请注意,mysql扩展现在已弃用,将来某个时候将被删除。那是因为它古老,充满了不良做法,缺乏一些现代特色。不要用它来编写新代码。请改用PDO或mysqli_ *。您的查询很容易出现SQL注入。