简体   繁体   English

PHP和MySql中的时间戳查询已更新

[英]TimeStamp Query in PHP and MySql UPDATED

I have these tables and i want to get news from News table, but the news that have been added after the user had logged in & he didn't see the data Depending on the Query that is shown in the PHP script. 我有这些表,我想从“新闻”表中获取新闻,但是在用户登录后添加的新闻&他没有看到数据,具体取决于PHP脚本中显示的“查询”。

The Users When they are logging in to the system they register their Login Time-stamp. 用户当他们登录到系统时,他们会注册他们的登录时间戳。 After a while a service asks the server about the new data for that User_ID and check the seen table if the news is not in the seen table and the Current Time-stamp is Greater than the logged in Time-stamp then the news is new. 一段时间后,服务会向服务器询问有关该User_ID的新数据,并检查可见表中的新闻是否不在可见表中,并且当前时间戳大于登录的时间戳,则该新闻为新消息。

Users Table 用户表

User_ID | User_Name   |User_Login
-------------------------------
1       | John        |2016-04-13 16:01:12
2       | Carl        |2016-04-13 16:13:22
3       | Tomas       |2016-04-13 16:01:01
4       | Adam        |2016-04-13 16:04:44
5       | Nancy       |2016-04-13 16:04:37

News Table 新闻表

News_ID | News_Text      | News_Post_TimeStamp          
----------------------------------------------
1       | Hello World    | 2016-04-13 16:09:23
2       | This is My car | 2016-04-13 16:10:24
3       | I had Ate pizza| 2016-04-13 16:11:40
4       | Leave Me Alone | 2016-04-13 16:15:30
5       | C++ Programming| 2016-04-13 16:09:50

Seen Table 见表

ID   | User_Id  | News_Id
---------------------------
1    |  1       | 2
2    |  1       | 3
3    |  4       | 1
4    |  5       | 3
5    |  1       | 4

UPDATE This is my PHP Script Code: 更新这是我的PHP脚本代码:

 <?php

require('config.php');
$conn = mysqli_connect($servername, $username, $password, $db);
$query="SELECT * FROM news,users WHERE news.news_id NOT IN (SELECT news_id FROM seen WHERE user_id = '".$_GET['id']."') AND users.user_login<Now() ";


$result = mysqli_query($conn,$query);
$rows = array();
echo mysqli_error($conn);

while($row = mysqli_fetch_assoc($result)) {

    $rows[] = $row;
}

echo json_encode($rows);
?> 

and This is the JSON Format: 这是JSON格式:

 [{"News_id":"1","News_Text":"C++programming","news_post_timestamp":"2016-     04-13 16:09:41","User_Id":"14","User_Name":"John","user_login":"2016-04-13 16:04:37"},{"News_id":"1","News_Text":"C++programming","news_post_timestamp":"2016-04-13 16:09:41","User_Id":"15","User_Name":"Carl","user_login":"2016-04-13 16:04:37"},{"News_id":"2","News_Text":"Pizza","news_post_timestamp":"2016-04-13 16:09:41","User_Id":"14","User_Name":"John","user_login":"2016-04-13 16:04:37"},{"News_id":"2","News_Text":"Pizza","news_post_timestamp":"2016-04-13 16:09:41","User_Id":"15","User_Name":"Carl","user_login":"2016-04-13 16:04:37"},{"News_id":"3","News_Text":"Android","news_post_timestamp":"2016-04-13 16:09:41","User_Id":"14","User_Name":"John","user_login":"2016-04-13 16:04:37"},{"News_id":"3","News_Text":"Android","news_post_timestamp":"2016-04-13 16:09:41","User_Id":"15","User_Name":"Carl","user_login":"2016-04-13 16:04:37"},{"News_id":"4","News_Text":"Ahmad","news_post_timestamp":"2016-04-13 16:09:41","User_Id":"14","User_Name":"John","user_login":"2016-04-13 16:04:37"},{"News_id":"4","News_Text":"Ahmad","news_post_timestamp":"2016-04-13 16:09:41","User_Id":"15","User_Name":"Carl","user_login":"2016-04-13 16:04:37"},{"News_id":"5","News_Text":"Toto","news_post_timestamp":"2016-04-13 16:09:41","User_Id":"14","User_Name":"John","user_login":"2016-04-13 16:04:37"},{"News_id":"5","News_Text":"Toto","news_post_timestamp":"2016-04-13 16:09:41","User_Id":"15","User_Name":"Carl","user_login":"2016-04-13 16:04:37"},{"News_id":"6","News_Text":"JaVA","news_post_timestamp":"2016-04-13 16:09:41","User_Id":"14","User_Name":"John","user_login":"2016-04-13 16:04:37"},{"News_id":"6","News_Text":"JaVA","news_post_timestamp":"2016-04-13 16:09:41","User_Id":"15","User_Name":"Carl","user_login":"2016-04-13 16:04:37"},{"News_id":"7","News_Text":"Computer","news_post_timestamp":"2016-04-13 16:09:41","User_Id":"14","User_Name":"John","user_login":"2016-04-13 16:04:37"},{"News_id":"7","News_Text":"Computer","news_post_timestamp":"2016-04-13 16:09:41","User_Id":"15","User_Name":"Carl","user_login":"2016-04-13 16:04:37"},{"News_id":"8","News_Text":"Test","news_post_timestamp":"2016-04-13 16:09:41","User_Id":"14","User_Name":"John","user_login":"2016-04-13 16:04:37"},{"News_id":"8","News_Text":"Test","news_post_timestamp":"2016-04-13 16:09:41","User_Id":"15","User_Name":"Carl","user_login":"2016-04-13 16:04:37"},{"News_id":"9","News_Text":"Test","news_post_timestamp":"2016-04-13 16:09:41","User_Id":"14","User_Name":"John","user_login":"2016-04-13 16:04:37"},{"News_id":"9","News_Text":"Test","news_post_timestamp":"2016-04-13 16:09:41","User_Id":"15","User_Name":"Carl","user_login":"2016-04-13 16:04:37"},{"News_id":"10","News_Text":"Al Hasasd","news_post_timestamp":"2016-04-13 16:47:44","User_Id":"14","User_Name":"John","user_login":"2016-04-13 16:04:37"},{"News_id":"10","News_Text":"Al Hasasd","news_post_timestamp":"2016-04-13 16:47:44","User_Id":"15","User_Name":"Carl","user_login":"2016-04-13 16:04:37"}]

There is a duplicate in the Results , if anyone can tell me how to remove duplicate ? 结果中有重复项,如果有人可以告诉我如何删除重复项?

Your SQL should be this (exemple with User_ID 1) 您的SQL应该是这样(例如User_ID 1)

SELECT * 
FROM news n
WHERE news_id NOT IN (SELECT news_id FROM news_seen WHERE user_id = 1)
AND News_Post_TimeStamp > (SELECT User_Login FROM user_table WHERE User_ID = 1)

(adapt user_table accordingly) (相应地调整user_table


But prior to simply do this, you should adapt your PHP code because what you are doing is an invitation to a SQL injection. 但是在执行此操作之前,您应该修改PHP代码,因为您所做的是邀请SQL注入。

Ideally you should use parameterized queries . 理想情况下,您应该使用参数化查询

But, assuming your USER_ID is CHAR type as you had quotes in your query, you can just make your code a bit safer using this simple sanitize function that I added to your code. 但是,假设您的USER_ID是CHAR类型(如查询中的引号一样),则可以使用我添加到代码中的这个简单的sanitize函数,使代码更安全一些。

<?php

require('config.php');
$conn = mysqli_connect($servername, $username, $password, $db);

$userid = sanitize($_GET['id']);
$query="SELECT * FROM news WHERE news_id NOT IN (SELECT news_id FROM news_seen WHERE user_id = '$userid') AND News_Post_TimeStamp > (SELECT User_Login FROM user_table WHERE User_ID = '$userid')";

$result = mysqli_query($conn,$query);
$rows = array();
echo mysqli_error($conn);

while($row = mysqli_fetch_assoc($result)) { 
    $rows[] = $row;
}

echo json_encode($rows);




function sanitize($data)
{
    // remove whitespaces (not a must though)
    $data = trim($data); 

    // apply stripslashes if magic_quotes_gpc is enabled
    if(get_magic_quotes_gpc()) 
    {
    $data = stripslashes($data); 
    }

    // a mySQL connection is required before using this function
    $data = mysql_real_escape_string($data);

    return $data;
}



?> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM