如何在文件PHP中刷新没有刷新网页的数据库中的单个值

时间:2017-02-24 09:22:05

标签: php mysql

<?php
    session_start();

    $dbhost = 'localhost';
    $dbuser = 'root';
    $dbpass = 'kumkum09';
    $dbname =   'eoffice_db';
    $conn = new mysqli($dbhost, $dbuser, $dbpass, $dbname);
    if(! $conn )
    {
        die('Could not connect: ' . mysqli_error());
    }

    if (isset($_SESSION['user']) == '') {

        // Fungsi Validasi Login dan Create session
        if(isset($_POST['user'])){
        $username = mysqli_real_escape_string($conn, $_POST['user']);
        $password = mysqli_real_escape_string($conn, $_POST['pass']);

        $query      = mysqli_query($conn, "SELECT * FROM users WHERE  password=md5('$password') and username='$username'");
        $row          = mysqli_fetch_array($query);
        $num_row    = mysqli_num_rows($query);

        if ($num_row > 0)
        {
            $_SESSION['user']=$row['username'];
            $user=$_SESSION['user'];

            // echo "User: ".$user."<br>";

            // Fungsi Cek Token dan Create Token
            $cektoken = "SELECT * FROM users WHERE username='$user'";
            $resultcekuser = $conn->query($cektoken);
            $rowtoken = $resultcekuser->fetch_object();
            $tokennye = $rowtoken->token;

            if($tokennye==""){
                $tokenz = md5($user.time().rand());
                $updttokenuser = "UPDATE users SET token='$tokenz' WHERE username='$user'";
                $prosesupdttokenuser = $conn->query($updttokenuser);
            }
            $createsessiontoken = mysqli_query($conn, "SELECT * FROM users WHERE username='$user'");
            $sessiontoken = mysqli_fetch_array($createsessiontoken);
            $_SESSION['token']  = $sessiontoken['token'];
            header("location: tes.php");
        }
        }
        else
        {
            // header("location: index.php");
        }
    }

    else
    {
        $user  = $_SESSION['user'];
        $token = $_SESSION['token'];

        // Counting Jumlah Notifikasi Memo Belum Baca
        $notifmemo = "SELECT * FROM memo WHERE username_tujuan='$user' and status='Belum Dibaca'";
        $resultnotifmemo = $conn->query($notifmemo);
        $rowcount=mysqli_num_rows($resultnotifmemo);
    }
?>

上面的代码我放入了名为tes.php的文件中,并且我包含在index.php中。我调用$ rowcount将数字显示为新邮件的通知量。

问题是,如何在不刷新index.php页面的情况下刷新$ rowcount的值?请帮帮我,谢谢。

1 个答案:

答案 0 :(得分:1)

您可以使用Ajax编写函数并在一段时间后调用它然后向您的tes.php文件发送ajax请求计算您的数据并在您的页面中显示您想要的响应:我给你ajax示例

second_value = browser.find_by_id('resources').find_by_id('crystal_box').first.value

在你的test.php页面中计算你的记录,然后回显或返回它