问题
I have a table called files where there are columns such as id, name, webpage, visitors. There are list of files already in the table. Now I have created new columns called webpage and visitors. The webpage is the URI of the file and the visitors is the counter views of the file. The problem is that I wanted to update the webpage and visitors automatically using session but I can't. Can you please help me.
<?php
session_start();
include_once"config.php";
$webpage=htmlspecialchars($_SERVER["REQUEST_URI"]);
$result=mysql_query("SELECT * FROM files WHERE webpage='$webpage'");
$num_rows = mysql_num_rows($result);
if ($num_rows == 0){
mysql_query("How to update the URI ($webpage) automatically? in the files table inside the column webpage");
}else{
if (!isset($_SESSION['webpage'])){$_SESSION['webpage'] = 0;
mysql_query("UPDATE files SET visitors=visitors+1 WHERE webpage='$webpage'");}}
?>
回答1:
Try this solution:
<?php
session_start();
include_once"config.php";
$webpage=htmlspecialchars($_SERVER["REQUEST_URI"]);
$result=mysql_query("SELECT * FROM files WHERE webpage='$webpage'");
$num_rows = mysql_num_rows($result);
if ($num_rows == 0)
{
$_SESSION['webpage'][] = $webpage;
mysql_query("INSERT INTO files (`id`,`name`,`webpage`,`visitors`) VALUES (null,'$name','$webpage','1')");
}
else
{
if(is_array($_SESSION['webpage'])) && !in_array($webpage, $_SESSION['webpage'])))
{
$_SESSION['webpage'][] = $webpage;
mysql_query("UPDATE files SET visitors=visitors+1 WHERE webpage='$webpage'");
}
}
?>
来源:https://stackoverflow.com/questions/22504989/how-to-update-the-uri-of-the-webpage-automatically