查询以在某个时间戳无效后获取表

时间:2015-05-10 12:22:13

标签: php jquery mysqli

我正在尝试计算某个时间戳后的总收入。 我想做的事: 我有一个按钮,点击该按钮可以将带有时间戳的总收入添加到表格中。 一旦它被添加到数据库,从那里我想显示该时间戳之后获得的总收入。 所以,这意味着如果点击按钮后没有收入,它应该显示0。

问题: 如果数据库为空,那东西只会第一次起作用。否则,值会在数据库中正确插入,但每次按下按钮时,jquery都不会更新span标记。

HTML:

           <span id="wb_uid8">Earned: </span><span id="wb_uid9">Fetching data..</span>
       <input type="submit" id="Button1" name="Button1" value="I received my incentive today!">

           <span id="wb_uid10"><strong>Last payment receive date:</strong></span><span id="wb_uid11"> </span><span id="wb_uid12"></span><span id="wb_uid13"><br>
           </span><span id="wb_uid14"><strong>Received amount:</strong></span><span id="wb_uid15"> </span><span id="wb_uid16"></span>

jQuery的:

<script>
$(document).ready(function()
{
    $('#Button1').click(function(){
    var fullDate = new Date();
    var twoDigitMonth = fullDate.getMonth()+"";
     if(twoDigitMonth.length==1){
        twoDigitMonth="0" +twoDigitMonth;
    }
    var twoDigitDate = fullDate.getDate()+"";
    if(twoDigitDate.length==1){
       twoDigitDate="0" +twoDigitDate;
    }
    var currentdate = twoDigitDate+"/"+twoDigitMonth+"/"+fullDate.getFullYear();

    var dataString = 'date='+currentdate;
    $.ajax({
        type:"POST",
        data: dataString,
        url: "receipts.php",
        cache:false
    });
    return false;
});
});
</script>

<script type="text/javascript">
$(function(){
setInterval(function(){
    $.ajax({
        url: 'checkearnings.php',
        success: function(data){
            var data=JSON.parse(data);
            $('#wb_uid9').html('&#8377; '+ data['earned']);
            $('#wb_uid12').text(data['lastdate']);
            $('#wb_uid16').html('&#8377; '+ data['lastamt']);
        }
    });
}, 1000);
});
</script>   

checkearnings.php

<?php
include 'connect.php';
$earned = 0;
$lastdate = "";
$lastamt = "";
$timestamp = "";


$sql = "SELECT * FROM receipts ORDER BY date DESC LIMIT 1";
$result1 = mysqli_query($db, $sql);
if($details = mysqli_fetch_array($result1)){
    $lastdate = $details['date'];
    $lastamt = $details['amt'];
    $timestamp = $details['timestamp'];
}
else{
    $lastdate = "Never";
    $lastamt = "Nothing";
}


$sql = "SELECT amt FROM sales WHERE timestamp>'$timestamp'";
$result = mysqli_query($db, $sql);
while($fetch_options=mysqli_fetch_array($result)){
    $earned = $earned + $fetch_options['amt'];
}

$responses=array('earned'=>$earned, 'lastdate'=>$lastdate, 'lastamt'=>$lastamt);
$return=$responses;
//echo $earned;
echo json_encode($return);
?>

receipts.php

<?php
include 'connect.php';
include 'checkearnings.php';
if (isset($_POST['date']))
{
    $newdate = $_POST['date'];
    $time = round(microtime(true)*1000);
    $sql = "INSERT `receipts` (`date`, `amt`, `timestamp`) VALUES ('$newdate', '$earned', '$time')";
        $results = mysqli_query($db, $sql);
        mysqli_close($db);
}
?>

0 个答案:

没有答案