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

网站使用Foreach。我想要一个使用PHP和MSQL的点击计数器,php,counter,hit,hitcounter,Php,Counter,Hit,Hitcounter,所以我正在为我的表弟创建一个钢琴图书馆网站 目前,我使用foreach函数来显示数据库中的所有数据。现在,这很好用,我设法让一些功能正常工作,但我遇到的一个问题是计数器 超级简单的概念,除了一个事实,我想为每一个条目计数器 我所说的计数器是指在点击一个链接后,它会将计数加上+1。因此,每个链接将访问100次或访问34次,等等 我尝试了以下方法: if($mysqli){ $result = mysqli_query($mysqli,"SELECT * FROM testtable ".

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

目前,我使用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='2'";
它工作得很好,只修改id为2的行的命中率

显然,问题与foreach和将$row[id]设置为变量有关,但老实说,我需要一些帮助


它和变量中的变量有关吗?我不知道。非常感谢您的帮助。

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

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

不要在计数器的值周围使用单引号,因为它是一个整数。您可以根据需要进行修改

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

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

警告:mysqli_fetch_数组期望参数1为mysqli_结果,布尔值在

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

<?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);

更新表集合col_x=col_x+1,其中col_y='xyz'-问题是它在您的循环中,为什么首先使用一个?嗨,Fred,我知道我需要使用该字符串来获得我想要的。我遇到的问题是WHERE部分。我需要它,以便在foreach循环期间,WHERE被自动修改为与ID相同的ID。当前,其中ID='$ID'不起作用。您还在$result=mysqli\u query$con,$sql;当它应该是$mysqliThanks fred时,它是整个php的新手。将立即更新到mysqli。我正在使用循环来列出我的数据库,我不确定你说的我首先使用循环是什么意思?请记下我的评论。更改变量并重试,看看这是否会产生影响。感谢您对语法的输入,我们将更新它。它仍然不起作用。所有链接都会更新整个表的命中计数器。no am使用列名值并向其中添加1,这与“$hitcount=$row['hitcount']+1;”相同但是在mysql中
<?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);