我正在制作一个Tube站点,但我无法完成我的代码。目前,我正在制作一个"收藏夹脚本",这样注册的人就可以"收藏夹"视频,并可以在一个特殊的网站上看到它们。
从逻辑上讲,不可能两次喜欢一个视频,这是我目前的问题。
但我认为我的if条件是错误的,因为它没有检查用户ID和视频ID是否存在!
这是我的代码:
<?php
session_start();
include('config.php');
$videoid = $_GET['id'];
$userid = $_SESSION["username"];
$result = mysql_query("SELECT `id` FROM `users` WHERE `username` = '$userid'") or die (mysql_error());
$row = mysql_fetch_assoc($result);
$usernameid = $row['id'];
if (isset($userid, $videoid))
{
$row = "INSERT INTO favorites (userid, videoid, time)
VALUES
('$usernameid','$videoid', now())";
mysql_query("$row");
header ("Location: http://localhost/video.php?id=" . $_GET['id'] . "");
}
else
{
header ("Location: http://localhost/video.php?id=" . $_GET['id'] . "");
}
?>
我的收藏夹表:favoriteid|userid|videoid|time
所以它每次都应该检查是否已经设置了视频和用户名。
我也试过了!isset,但它不起作用
您应该考虑研究MySQLI,因为mysql_*函数正在慢慢被弃用。
无论如何,您应该使用一个名为mysql_num_rows的函数,并检查结果是0还是1。如果返回的结果是1,你就会知道视频已经被偏爱了,不应该重复这个动作。如果你得到一个0,那么插入它!
if (mysql_num_rows($result) == 0 && isset($userid) && isset($videoid)) {
$row = "INSERT INTO favorites (userid, videoid, time)
VALUES ('$usernameid','$videoid', now())";
mysql_query("$row");
header ("Location: http://localhost/video.php?id=" . $_GET['id'] . "");
}
更新
if($userid != '' && $videoid != ''){
$check = mysql_query("SELECT * FROM `favorites` WHERE `userid` = '$usernameid' AND `videoid` = '$videoid'");
if (mysql_num_rows($check) == 0) {
$row = "INSERT INTO favorites (userid, videoid, time)
VALUES ('$usernameid','$videoid', now())";
mysql_query("$row");
header ("Location: http://localhost/video.php?id=" . $_GET['id'] . "");
}else{
echo "You've already voted!";
}
}else{
header("Location: http://localhost/");
}
更改:
if (isset($userid, $videoid))
用于:
if (isset($userid) && isset($videoid))