有计算两个字段的问题

时间:2013-10-03 06:58:23

标签: php mysql

请帮忙?我有一个带记录表的数据库。我试图计算得到某个数字的两个字段不起作用。我真的很新的PHP会很感激任何帮助。

我的表格中有两个名为“startdate”的字段和“terminationdate”这两个字段是我想在为他们完成计算后回应的 p>

守则:

    <?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 个答案:

答案 0 :(得分:1)

您正在添加数组的索引而不是此处的值:

echo '<td>' . $row['$sdate+$edate'] . '</td>';

我建议您在获取之前对MySQL查询进行计算并明确定义所有列名称,如:

SELECT startdate + terminationdate WHERE....

或者在PHP中这样做:

echo '<td>' .($row['startdate'] + $row['terminationdate']).'</td>';

现在,根据我的观点,您正在尝试使用TIMEDIFF()计算两个日期的差异以获得持续时间。这是:

SELECT idnumber, firstname, lastname, companyname,jobtitle,dismissal TIMEDIFF(startdate,terminationdate) time_diff WHERE idnumber= '$term'

在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());

在您的评论中,您希望使用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));

*请注意mysql扩展名is now deprecated and will be removed sometime in the future。那是因为它古老,充满了不良做法,缺乏一些现代特色。不要用它来编写新代码。请改用PDOmysqli_*。您的查询很容易SQL Injection