将Foreach用于网站.我想要一个使用 PHP 和 MSQL 的点击计数器


Using Foreach for website. I want a hit counter using PHP & MSQL

所以我正在为我的小表弟创建一个钢琴图书馆网站。

目前,我使用 foreach 函数来显示数据库中的所有数据。现在,这很好用,我设法让一些功能正常工作,但我遇到的一个是"计数器"。

超级简单的概念,除了我想要每个条目的计数器。

通过"计数器",我的意思是单击链接后,它会在计数中添加+1。这样每个链接都会有"访问 100 次"或"访问 34 次"等

我尝试了以下方法:

if($mysqli){
    $result = mysqli_query($mysqli,"SELECT * FROM testtable ".$orderbyfilter);
    $rows = $result->fetch_all(MYSQLI_ASSOC);
    foreach($rows as $row) {
        echo "<tr id='entry'><td>";
        echo ucwords($row['name']);
        echo "</td><td align='center'>";
        echo '<a href="' . $row['url'] . '">url</a>';
        echo "add hit:";
        echo "<a href='?action=callfunction'>Click</a>";

        //current counter script
        if(isset($_GET['action']) && $_GET['action'] == 'callfunction'){
            $hitcount = $row['hitcount'] + 1;
            $id = $row['id'];
            // why doesn't this work?
            $sql="UPDATE testtable SET hitcount='$hitcount' WHERE id='".$id."'"; 
            $result=mysqli_query($con,$sql); 
        }
        echo "</td><td align='center'>";
        echo $row['level'];
        echo "</td><td align='center'>";
        echo $row['hitcount'];
        echo "</td></tr>";
    }
        mysqli_close($mysqli);
    } else {
        echo "table did not correctly display!";
}

显然方法:

$sql="UPDATE testtable SET hitcount='$hitcount' WHERE id='".$id."'";

不起作用,因为当我单击链接时,它会以相同的命中计数更新所有条目。但是,当我将其更改为:

$sql="UPDATE testtable SET hitcount='$hitcount' WHERE id='2'";

运行良好,它只修改带有 id=2 的行的命中计数。

显然,问题与"foreach"$row[id]设置为变量有关,但老实说,我可以使用一些帮助。

它与变量的变量有关吗?我不知道。任何帮助,不胜感激。

这是对我有用的方法。您可以根据需要对其进行修改。

需要做的是为 URL 中的相关"id"传递一个额外的 GET 参数,然后在 WHERE 子句中传递该变量。

还添加了一个标题,以便在单击给定行的相关 URL 后自动重定向并显示结果并防止出现以下警告:

警告:mysqli_fetch_array() 期望参数 1 mysqli_result,布尔值在...

旁注: 阅读下面代码中留下的注释以获取更多信息。

<?php 
ob_start(); // keep this, it's for the header
$DB_HOST = 'xxx'; // Replace
$DB_USER = 'xxx'; // with
$DB_PASS = 'xxx'; // your
$DB_NAME = 'xxx'; // own
$Link = new mysqli($DB_HOST, $DB_USER, $DB_PASS, $DB_NAME);
if($Link->connect_errno > 0) {
  die('Connection failed [' . $Link->connect_error . ']');
}
// Your original query. You can replace it if needed.
// $result = mysqli_query($Link,"SELECT * FROM testtable ".$orderbyfilter);
$result = mysqli_query($Link,"SELECT * FROM testtable");
 while ($row = mysqli_fetch_array($result)) {
    echo "<tr id='entry'><td>";
    echo "ID: " . $row['id'];
    $var = $row['id']; // used for the URL
    echo "<br>";
    echo $row['hitcount'];
    echo ucwords($row['name']);
    echo "</td><td align='center'>";
    echo '<a href="' . $row['url'] . '">url</a>';
    echo " Add hit: ";
    echo "<a href='?action=callfunction&id=$var'>Click</a> ";
    if(isset($_GET['action']) && $_GET['action'] == 'callfunction'){
// Used for the query and escape the URL
$var = mysqli_real_escape_string($Link, $_GET['id']); 
$sql="UPDATE testtable SET hitcount = hitcount +1 WHERE id='".$var."'"; 
$result=mysqli_query($Link,$sql); 
// Added a header, otherwise the old method will generate the following warning
// Warning: mysqli_fetch_array() expects parameter 1 to be mysqli_result, boolean given in...
header("Location: http://www.example.com/your_counter_file.php");
exit; // Keep this in order to stop further execution
// ^ Change the url in header above to reflect your Web site's address
    } // Closing brace for isset
    echo "</td><td align='center'>";
    echo $row['level'];
    echo "</td><td align='center'>";
    echo $row['hitcount'];
    echo "</td></tr>";
} // Closing brace for while loop
    mysqli_close($Link);

只需像这样更改查询即可更新计数器

$sql="UPDATE testtable SET `hitcount`=hitcount+1 WHERE id=".$id;

不要在计数器值周围使用单引号,因为它是一个integer