当条件满足时,我需要一个代码来增加表中的列

时间:2013-09-27 10:16:41

标签: php sql

下面是我用于递增的代码,它没有显示错误,但“喜欢”列没有递增。

   <?php
      $id=$_GET['id'];
     echo $id;
     $dsn = 'mysql:host=127.0.0.1;dbname=as1';
     $user = 'root';
     $password = '';

    try{
                // Connect and create the PDO object
                $pdo = new PDO($dsn, $user, $password);
                $pdo->setAttribute(PDO::ATTR_ERRMODE,PDO::ERRMODE_EXCEPTION);
            }
         catch(PDOException $e){
                                    echo 'Database connection failed - ';
                                    echo $e->getMessage();
                                    exit;
                                }
        $sql="UPDATE photo SET likes = likes + 1 WHERE imagename=:id";
        $q=$pdo->prepare($sql);
        $q->execute(array($id));
        header("Location:upload.php");
}

2 个答案:

答案 0 :(得分:0)

$sql="UPDATE photo SET likes = likes + 1 WHERE imagename=:id";
$q=$pdo->prepare($sql);
$q->bindParam(':id', $id, PDO::PARAM_INT);
$q->execute();
从编程语言中获知的

++不适用于普通的SQL语法。

答案 1 :(得分:0)

$sql="UPDATE photo SET likes = likes + 1 WHERE imagename=':id'";