Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/57.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 在新插入数据库时以及用户看到时切换通知图标_Php_Mysql_Notifications - Fatal编程技术网

Php 在新插入数据库时以及用户看到时切换通知图标

Php 在新插入数据库时以及用户看到时切换通知图标,php,mysql,notifications,Php,Mysql,Notifications,提前谢谢大家 因此,我的公司有一个内部系统,负责管理数据。有时需要修改这些数据或添加新数据,发生这种情况时,我需要通知用户。 我创建了一个通知系统,当用户点击钟形链接(有点像谷歌的通知铃)时,它会在一个模式窗口中显示数据库中的新内容。这是一个静态链接,当我在数据库中插入或修改内容时,我希望它切换颜色或另一个图标,然后在用户看到它时切换回来。可能在进行更改时变为红色,在用户看到时变为白色 首先,我必须提到我在数据库中有3个表;用户、列表和通知。notifications表用于在数据更改或新数据添加

提前谢谢大家

因此,我的公司有一个内部系统,负责管理数据。有时需要修改这些数据或添加新数据,发生这种情况时,我需要通知用户。 我创建了一个通知系统,当用户点击钟形链接(有点像谷歌的通知铃)时,它会在一个模式窗口中显示数据库中的新内容。这是一个静态链接,当我在数据库中插入或修改内容时,我希望它切换颜色或另一个图标,然后在用户看到它时切换回来。可能在进行更改时变为红色,在用户看到时变为白色

首先,我必须提到我在数据库中有3个表;用户、列表和通知。notifications表用于在数据更改或新数据添加到列表表时通知用户,这是通过我创建的表单实现的

到目前为止,我的代码是这样的:

<a href="#openModal"><i class="fa fa-bell" style="color:#fff;"></i></a>
<?php
$listnotif = "";
$sql = mysql_query("SELECT * FROM notificatios ORDER BY date DESC LIMIT 15");
$productCount = mysql_num_rows($sql);
if ($productCount > 0) {
while($row = mysql_fetch_array($sql)){
    $type = $row["type"];
    $description = $row["description"];
    $date = ucwords(strftime("%A %e/%m/%Y - %l:%M", strtotime($row["date"])));
    $listnotif .= "<tr><td><small>$date</small></td><td>$type</td><td>$description</td><tr>";
}
    } else {
    $listnotif = "<tr><td colspan='3'>There are no notifications</td></tr>";
    }
?>

<table>
<tr>
    <th>Date</th>
    <th>Type</th>
    <th>Description</th>
</tr>
<?php echo $listnotif; ?>
</table>
<?php 
$delete = "";
if (isset($_GET['deleteid'])) {
    $delete .= 'You sure you want to delete this entry? <a href="notif_admin.php?yesdelete=' . $_GET['deleteid'] . '">&ensp;<b>Yes</b></a> - <a href="notif_admin.php"><b>No</b></a></div>';
}
if (isset($_GET['yesdelete'])) {
$id_to_delete = $_GET['yesdelete'];
$sql = mysql_query("DELETE FROM notifications WHERE id='$id_to_delete' LIMIT 1") or die (mysql_error());
}
?>
<?php
if (isset($_POST['type'])){
    $type = mysql_real_escape_string($_POST['type']);
    $description = mysql_real_escape_string($_POST['description']);

    $sql = mysql_query("INSERT INTO notifications (type,description,date) VALUES('$type','$description',now())") or die (mysql_error());
}
?>
<?php
$notification = "";
$sql = mysql_query("SELECT * FROM notifications ORDER BY date DESC");
$productCount = mysql_num_rows($sql);
if ($productCount > 0) {
while($row = mysql_fetch_array($sql)){
    $id = $row["id"];
    $type = $row["type"];
    $description = $row["description"];
    $date = ucwords(strftime("%A %e/%m/%Y - %l:%M", strtotime($row["date"])));
    $notification .= "<tr><td><a href='notif_admin.php?deleteid=$id'><span class='fa fa-trash-o'></span></a></td><td>$date</td><td>$type</td><td>$description</td></tr>";
}
} else {
$notification = "<tr><td>No stuff here</td></tr>";
}
?>

<table>
<tr>
    <th>delete</th>
    <th width="200">Date</th>
    <th>Type</th>
    <th>Description</th>
</tr>
<?php echo $notification; ?>
</table>
</div>

<form action="notif_admin.php" enctype="multipart/form-data" name="myForm" id="myForm" method="post">
<table>
<tr>
<td width="150">Type</td>
<td>
    <select name="type" id="type">
        <option selected="selected">Select</option>
        <option value="Addition">Additio</option>
        <option value="Removal">Removal</option>
        <option value="Modification">Modification</option>
        <option value="Correction">Correction</option>
        <option value="General">General</option>
    </select>
</td>
</tr>
<tr>
<td>Description</td>
<td><input name="description" id="description" type="text"></td>
</tr>
<tr>
<td>&nbsp;</td>
<td><input name="button" id="button" type="submit" value="Add Notification" class="boton"></td>
</tr>
</table>
</form>
这是用户的代码:

<?php
session_start();
if (!isset($_SESSION["manager"])){
header("location: admin_login.php");
exit();
}
$managerID = preg_replace('#[^0-9]#i', '', $_SESSION["id"]);
$manager = preg_replace('#[^A-Za-z0-9]#i', '', $_SESSION["manager"]);
$password = preg_replace('#[^A-Za-z0-9]#i', '', $_SESSION["password"]);
include "scripts/connect.php";
$sql = mysql_query("SELECT * FROM users WHERE id='$managerID' AND username='$manager' AND password='$password' LIMIT 1");
$existCount = mysql_num_rows($sql);
if ($existCount == 0){
    echo "The login info you entered does not exist in the database.";
exit();
}
$header ="";
while ($row = mysql_fetch_array($sql)){
$name = $row["name"];
$accounttype = $row["accounttype"];
if($accounttype == "a"){
    include_once "menu_a.php";
}else{
    include_once "menu_b.php";
}
}
?>

如果您想要实时通知,有几个选项

  • 你可以用
  • 您可以在前端使用AJAX轮询服务器
  • 如果您使用的是JQuery,那么看起来应该是这样的

    setInterval(function() {
        $.get('/notification_url', function(res) {
            $('.fa-bell').css({'color': 'red'});
        });    
    }, 1000);
    
    这将每秒轮询服务器上的某些页面以检查更新


    您有很多选择,但无论哪种方式,您都需要通过Javascript进行更新。

    前面的答案很好,但我认为最好增加ajax请求之间的间隔。1秒的间隔非常低,可能会导致拥塞,甚至冲突。。请求之间至少间隔5秒

    var interval = setInterval(function() {
        $.get('/get_xyz_status_color.php?anyparam=paramvalue', function(colorResponse) {
            $('.fa-bell').css({'color': colorResponse});
        });    
    }, 1000);
    
    如果您想更新多个状态,我建议您在同一个请求中输出每个状态的json,以避免导致过多的请求

    因此我建议使用$.getJSON而不是$.get

    var interval = setInterval(function() {
        $.getJSON(url,function(response){
            $('.stat1').css({'color': response.status1});
            $('.stat2').css({'color': response.status2});
            ...
        });
    }, 1000);
    

    谢谢你的密码。你只是忘了提你真正的问题是什么。这段代码有什么问题吗?如果当前用户有消息,可以使用JavaScript和Ajax每隔30秒检查一次。在“通知”表中添加一个额外的列,如果未看到通知,则为false;如果用户看到通知,则为true。@GolezTrol,感谢您抽出时间。我想知道,当我在通知表中插入一个新条目时,如何切换通知图标,比如说切换为红色,当用户看到它时,如何切换为白色。最好使用事先准备好的语句。即使您使用的是
    mysql\u real\u escape\u string()
    ,这仍然不是防弹的。看这个关于Jose的问答,听起来很有趣。你能举例说明如何做到这一点吗?我对JavaScript和Ajax知之甚少。