gpt4 book ai didi

php - 如何在不刷新文件 PHP 网页的情况下从数据库中刷新 1 个单个值

转载 作者:行者123 更新时间:2023-11-28 23:19:32 24 4
gpt4 key购买 nike

<?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的值?请帮助我,谢谢。

最佳答案

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

     $(function(){
setTimeout(notification,30000);

function notification(){
$.ajax({
data : 'get',
dataType : 'html',
url : 'test.php'
data : {check_data : 1},
success : function(data){
$('#id of your div').html(data);
},
error : function(){
console.log('Error');
}
});
}


});

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

关于php - 如何在不刷新文件 PHP 网页的情况下从数据库中刷新 1 个单个值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42434917/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com