如何使用PHP

时间:2017-11-26 04:09:34

标签: php mysql sql mysqli

当前更新:我已经清理了代码,但仍然存在一些问题。 注意此代码每3秒运行一次。最外面的'其他'语句似乎运行,在数据库表中将时间设置为0,但之后没有活动。 在最初的运行时间之后,最外面的“其他”'语句永远不应该运行,并且存储在用户别名下的时间值应该使用最新的时间戳保持更新,但它只是位于' 0'。

这是运行php文件的JS:

//CHECK FOR NEW CHAT MESSAGES
setInterval(function()
{
    $.post("chat_update.php",  function(data) { $("#rect_comments_text").append(data);}  );
}, 3000);

代码:

<?php
    session_start();
    $alias = $_SESSION['username'];

    $host = 'localhost';
    $user = '*';
    $pass = '*';
    $database = 'vethergen_db_accounts';
    $table = 'table_messages';
    $time_table = 'table_chat_sync';
    $connection = mysqli_connect($host, $user, $pass) or die ("Unable to connect!");
    mysqli_select_db($connection,$database) or die ("Unable to select database!");
    $timestamp =  time();

    $last_time_query = "SELECT alias FROM $time_table";
    $last_time_result = mysqli_query($connection,$last_time_query);
    $last_time_rows = mysqli_fetch_array($last_time_result);

    if ($last_time_rows['alias'] === $alias)
    {
        $last_time = $last_time_rows['time'];
        $query = "SELECT * FROM $table WHERE time > $last_time ORDER BY text_id ASC"; //SELECT NEW MESSAGES
        $result = mysqli_query($connection,$query);

        //APPEND NEW MESSAGES
        while($row = mysqli_fetch_array($result))
        {
            if ($row['alias'] === "Vether")
            {
                echo '<p id = "chat_text">'.'<b>'.$row['alias'].'</b>'.': '.$row['text']."</p>";
                echo '<p id = "time_stamp">'.$row['time'].'</p>';
                echo '<p id = "chat_number">'.$row['text_id'].'</p>';
            }
            else
            {
                echo '<p id = "chat_text">'.'<b class = "bold_green">'.$row['alias'].'</b>'.': '.$row['text']."</p>";
                echo '<p id = "time_stamp">'.$row['time'].'</p>';
                echo '<p id = "chat_number">'.$row['text_id'].'</p>';
            }
            echo '<hr class = "chat_line"></hr>';
        }

        //UPDATE LAST SYNC TIME
        $update_query = "UPDATE $time_table SET time = '$timestamp' WHERE alias = '$alias'";
        mysqli_query($connection,$update_query);
    }
    else
    {
        echo '<p> HERE </p>';
        $update_query = "INSERT INTO $time_table (alias, time) VALUES('$alias','0')";
        mysqli_query($connection,$update_query);
    }



?>

1 个答案:

答案 0 :(得分:0)

您只需要检查mysqli_num_rows是否插入或更新数据。您还必须在选择查询中在'周围添加$alias。更改您的代码如下:

//EITHER UPDATE THE EXISTING VALUE OR CREATE ONE FOR FIRST TIME VISITORS...
$last_time_query = "SELECT * FROM $time_table WHERE alias = '$alias'"; //change here add '
$last_time_result = mysqli_query($connection,$last_time_query);
if (mysqli_num_rows($last_time_result) == 0) //Only check number of rows
{
    $update_query = "INSERT INTO $time_table (alias, time) VALUES('$alias','$timestamp')";
    mysqli_query($connection,$update_query);
}
else
{
    $update_query = "UPDATE $time_table SET time = '$timestamp' WHERE alias = '$alias'";
    mysqli_query($connection,$update_query);
}