如何将每日利率存入数据库

时间:2017-06-03 06:02:13

标签: php database mysqli cron

请任何人都可以帮我解决问题,我是php开发领域的新手

我正在开发投资兴趣门户网站,每天都有基于数据库存储利率的问题,实际问题是用用户详细信息存储利息,每24小时使用cron job运行功能,但是当我登录时由于用户只存储该用户的利率值,其他用户没有存储,我认为使用会话用户登录的详细信息可能是这个问题,请任何人帮我存储每个用户的兴趣值和用户详细信息

    session_start();
    $_SESSION['sess_user'];
    $conn = mysqli_connect("localhost", "root", "", "bitmining");

    $investedusername = $_SESSION['sess_user']; 
    $sql3="SELECT sum(inv_amount) as total FROM buyinvest WHERE invested_username='$investedusername'";         

    $result = mysqli_query($conn, $sql3);           
    while($row = mysqli_fetch_assoc($result)){

        $investment = $row['total'];
        $days = 1;
        $interest_rate = (350/100/365) * $days ;
        $finalvalue = $interest_rate * $days;

    } 

    $date = "SELECT invest_timestamp FROM buyinvest ORDER BY invest_timestamp DESC LIMIT 1";
    $result = mysqli_query($conn, $date);
    $d_test = mysqli_fetch_assoc($result);

    $d_test2 = $d_test["invest_timestamp"];

    $timestamp = time();

    $investeduser = $_SESSION['sess_user'];
    $interestamount = $finalvalue;  
    $date_interest = date("y-m-d h:i:s");

    if ($d_test2  + 86400 == $timestamp){
        if ($conn){
                $sql3 = "INSERT INTO user_investments(invested_user, interest_amount, date_interest) VALUES('$investeduser','$interestamount','$date_interest')";
                if (mysqli_query($conn, $sql3))
                {
                    echo "Updated!";
                }
                else
                {
                    echo "There is an error.";
                }
            }
        }
        else {
        echo "Not yet!";
        } 

请有人帮我解决上面的代码

1 个答案:

答案 0 :(得分:0)

像这样的东西

$sql = "SELECT * from table_name";
$result = $conn->query($sql);
if($result->num_rows > 0){

while ($row = $result->fetch_assoc()) {

$sql3="SELECT sum(inv_amount) as total FROM buyinvest WHERE invested_username='".$row['username']"'";
// your code
}
}