Php 如何在单击时多次更新单击计数器?

Php 如何在单击时多次更新单击计数器?,php,mysql,Php,Mysql,我目前已经实现了一个单击计数器,如果该值设置为null,并且您单击链接,它将更新为一个。但是当它第二次被点击时,它不会更新它的状态 public function getUpdateClick($Id) { $Id=DB::escape_string($Id); $i=1; $click=0; $click=$click+$i; $updatesArray=array('clicks'=>$click);// the column which is

我目前已经实现了一个单击计数器,如果该值设置为null,并且您单击链接,它将更新为一个。但是当它第二次被点击时,它不会更新它的状态

public function getUpdateClick($Id) {
    $Id=DB::escape_string($Id);
    $i=1;
    $click=0;
    $click=$click+$i;

    $updatesArray=array('clicks'=>$click);// the column which is going to be updated

    $this->setTable('pages'); //setting the table name

    $where="id=$Id";

    $result=$this->update($updatesArray,$where); // query executed
}

删除第一行并将$click的值存储为会话变量,或将其存储在数据库中。

在函数中,您应该首先检查特定页面(
其中id=$id
)的
页面
表上是否有
单击
的值。如果没有,则可以单击1,否则获取该值并添加1

但我会这样做:我会在
单击
列中禁用ALLOW NULL来编辑表,这样在默认情况下,当创建新页面时,它的默认值为0。然后我会使用下面的代码:

public function getUpdateClick($Id){
    $Id=DB::escape_string($Id);

    //You can edit the 3 lines below to check the database 
    //  in pages table in clicks column where id=$Id in your custom way
    $fetching sql_result = mysql_query('SELECT * FROM pages where id=$Id')
    $row = mysql_fetch_assoc($fetching sql_result);
    $current_number_of_clicks = $row['clicks'];

    $click= $current_number_of_clicks;
    $click=$click+1;

    $updatesArray=array('clicks'=>$click);// the column which is going to be updated

    $this->setTable('pages'); //setting the table name
    $set="click=click+1"; // column and value
    $where="id=$Id";  // 

    $result=$this->update($updatesArray,$where); // query executed
}
} // I assume this closes your class

每次调用函数时,$click的值都设置为0,然后在下一个语句中设置为1。如果希望值保持不变,则应在表中使用属性,并在每次单击时递增该属性

为什么在紧接着添加1时设置
$click=0
?因为您从未使用
$set=“click=click+1”但始终
$click=0+1
。因为我一直收到未分配变量的错误消息Right,但您可以简单地将
$click
设置为1,因为它们似乎是静态值(未通过引用传递)
public function getUpdateClick($Id){
    $Id=DB::escape_string($Id);

    //You can edit the 3 lines below to check the database 
    //  in pages table in clicks column where id=$Id in your custom way
    $fetching sql_result = mysql_query('SELECT * FROM pages where id=$Id')
    $row = mysql_fetch_assoc($fetching sql_result);
    $current_number_of_clicks = $row['clicks'];

    $click= $current_number_of_clicks;
    $click=$click+1;

    $updatesArray=array('clicks'=>$click);// the column which is going to be updated

    $this->setTable('pages'); //setting the table name
    $set="click=click+1"; // column and value
    $where="id=$Id";  // 

    $result=$this->update($updatesArray,$where); // query executed
}
} // I assume this closes your class