PHP:点赞/点赞计数器 [英] PHP: like/dislike counter

查看:382
本文介绍了PHP:点赞/点赞计数器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个漫画网站,我希望允许用户对每幅漫画进行一次一次投票,对每件艺术品进行一次投票.

I have a comics website where I'd like to allow users to vote once per comic and once per piece of artwork.

我的代码似乎有两个问题:

There seems to be two problems with my code:

1)我只希望一个用户为每个图像投票一次...所以我想捕获他们的信息并将其存储在数据库中.我有一个ON DUPLICATE KEY UPDATE,但是即使没有发现任何错误,它也会给我带来以下语法错误:

1) I only want one user voting once per image... so I want to capture their information and store it in a database. I have a ON DUPLICATE KEY UPDATE, but it gives me the following syntax error even though I haven't found ANYTHING wrong with it:

Error: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'table = VALUES(table), imgid = VALUES(imgid)' at line 7 

其中一个示例允许针对同一IP的数据库中有多个条目:

An example of it allowing multiple entries into the database for the same IP:

2)仍然允许一个用户多次投票.

2) It's still allowing one user to vote multiple times.

    $sql = "SELECT ip FROM votes WHERE ip = \"".$_SERVER['REMOTE_ADDR']."\" AND table_name = $table AND imgid = $imgid";

$result = $mysqli->query($sql);
var_dump($result);

完整代码:

<?php 
include 'dbconnect.php';
$site = $_GET['_site'];
$imgid = intval($_GET['_id']);
$input = $_GET['_choice'];


if ($site == "artwork") {
   $table = "artwork";
}
else {
   $table = "comics";
}

$result = $mysqli->query("SELECT like_count, dislike_count FROM $table WHERE id = $imgid");

list($likes, $dislikes) = $result->fetch_array(MYSQLI_NUM);

$sql = "INSERT INTO 
            votes (ip, table_name, imgid) 
        VALUES 
            (\"".$_SERVER['REMOTE_ADDR']."\", \"$table\", $imgid)
        ON DUPLICATE KEY UPDATE
            ip = VALUES(ip),
            table = VALUES(table),
            imgid = VALUES(imgid)";

$mysqli->query($sql);
echo $mysqli->error;
echo "<br/>";

$sql = "SELECT ip FROM votes WHERE ip = '".$_SERVER['REMOTE_ADDR']."' AND table_name = '$table' AND imgid = $imgid";

$result = $mysqli->query($sql);
echo $mysqli->error;

if ($result->num_rows == 0) { 
    if ($input == "like") {
        $sql = "UPDATE $table SET like_count = like_count + 1 WHERE id = $imgid";
        $mysqli->query($sql);           
        $likes++;
    }
    else if ($input == "dislike") {
        $sql = "UPDATE $table SET dislike_count = dislike_count + 1 WHERE id = $imgid";
        $mysqli->query($sql);
        $dislikes++;        
    }
echo "Likes: " . $likes . ", Dislikes: " . $dislikes;
}
else {
    echo "You have already voted";
}
mysqli_close($mysqli);

?>

回显sql:

echo "sql: ". $sql;

产生:

sql: INSERT INTO votes (ip, table_name, imgid) VALUES ("127.0.0.1", "comics", 34) ON DUPLICATE KEY UPDATE ip = VALUES(ip), table = VALUES(table), imgid = VALUES(imgid)

任何帮助将不胜感激!

推荐答案

您看到的是table

What you're seeing is table is one of the MySQL reserved words but you're trying to use it as a column name. Your column is actually called table_name based on your question, though.

带有占位符的查询如下:

A query with placeholders looks like:

INSERT INTO votes (ip, table_name, imgid) 
  VALUES (?, ?, ?)
  ON DUPLICATE KEY UPDATE
    ip=VALUES(ip),
    table_name= VALUES(table_name),
    imgid=VALUES(imgid)

记住mysqli,您可以执行以下查询:

Remember with mysqli you can execute this query by doing this:

$sth = $mysqli->prepare("...");
$sth->bind_param("sss", $_SERVER['REMOTE_ADDR'], $table, $imgid);
$sth->execute();

文档更详细地描述了此过程,但是"sss"涉及三个字符串,并且三个值作为参数传递.

The documentation describes this process in more detail, but the "sss" thing refers to three strings, and the three values are passed in as parameters.

您可能应该使用 PDO ,因为它比mysqli容易使用.甚至更好的方法是使用 Doctrine 这样的数据库框架为您做很多SQL肮脏的工作.甚至更好的是使用类似 CodeIgnighter CakePHP FuelPHP 为您提供基础.从头开始手动构建应用程序非常耗时,而且容易出错.

You should probably be using PDO as it's a lot less fussy to use than mysqli. Even better would be to use a database framework like Doctrine to do a lot of the SQL dirty work for you. Even better still would be to use a framework like CodeIgnighter, CakePHP or FuelPHP to give you a foundation to build on. Constructing applications by hand from the ground up is extremely time-consuming and significantly more error prone.

要注意的另一件事是,您应该尝试在代码中使用一致的命名.您将$table称为table_name的值,因此应该以$table_name开头.

Another thing to note is you should try and use consistent naming in your code. You refer to $table as a value for table_name, so it should presumably be $table_name to start with.

这篇关于PHP:点赞/点赞计数器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆