PHP Sql Views counter using PDO prepare()

空扰寡人 提交于 2020-01-03 06:30:28

问题


I have a variable $id wich gives me the id of the current article and this can help me to make an update query in my database on current article.

This is my code:

 $vizualizari = $current_views+1;
    $sql1= "UPDATE detalii_cantari SET viz = viz WHERE id = {$id};";
$q1 = $dbh->prepare($sql1);
$q1->execute(array(':viz'=>$vizualizari));

I don't get any errors but my code still not working...


回答1:


Your correct code is here:

$vizualizari = $current_views+1;
    $sql1= "UPDATE detalii_cantari SET viz = :viz WHERE id = {$id}";
$q1 = $dbh->prepare($sql1);
$q1->execute(array(':viz'=>$vizualizari));

; from the end of sql is not needed here and viz = viz must become viz = :viz because of PDO.




回答2:


It seems you have to get rid of the previous query and make it in a single statement

$sql = "UPDATE detalii_cantari SET viz = viz + 1 WHERE id = ?";
$stm = $dbh->prepare($sql);
$stm->execute(array($id));


来源:https://stackoverflow.com/questions/19476351/php-sql-views-counter-using-pdo-prepare

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!