Php isset()在这里正确吗?/还是我的if条件不对?
我正在制作一个Tube站点,但我无法完成我的代码。目前,我正在制作一个收藏夹脚本,这样注册用户就可以“收藏”视频,并可以在一个特殊的网站上看到它们 从逻辑上讲,不可能两次喜欢一个视频,这是我目前的问题 但是我认为我的if条件是错误的,因为它没有检查userid和videoid是否存在 这是我的密码:Php isset()在这里正确吗?/还是我的if条件不对?,php,if-statement,conditional-statements,isset,Php,If Statement,Conditional Statements,Isset,我正在制作一个Tube站点,但我无法完成我的代码。目前,我正在制作一个收藏夹脚本,这样注册用户就可以“收藏”视频,并可以在一个特殊的网站上看到它们 从逻辑上讲,不可能两次喜欢一个视频,这是我目前的问题 但是我认为我的if条件是错误的,因为它没有检查userid和videoid是否存在 这是我的密码: <?php session_start(); include('config.php'); $videoid = $_GET['id']; $userid = $_SES
<?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'] . "");
}
?>
更改:
if (isset($userid, $videoid))
用于:
更新
你应该考虑一下,因为MySQL函数被缓慢地删除。
无论如何,您应该使用一个名为mysql\u num\u 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'] . "");
}
isset()
的唯一目的是检查变量是否存在。它不关心变量是空的、空的还是10000项数组。我会使用isset()
检查您的索引是否存在于$\u GET或$\u SESSION数组中,因为如果未设置它们,您将得到一个错误,即该索引不存在。@MonkeyZeus,您希望使用什么来代替isset()?isset()
仍然非常有用,您应该保留它,但您还需要检查两个变量,$userid
和$videoid
,是否无效nothing@monkey但是$userid和$videoid正在插入。问题是if条件没有检查它们是否存在。所以每次它插入新行时。我真的需要它吗?如果需要,我怎么做?请参考因为var_转储(isset($a,$b));//TRUE
在那里。我试图将它们分开,但它给了我一个“null!==表达式”,它所做的是检查两个变量是否都为空。如果它们不是空的,则继续执行If语句,如果它们是空的,则转到else语句。为了进行调试,您应该更改else语句头的URL。可能会将其放在主页上,因为当前两条if&else语句都指向同一url。@user3092548请尝试更新,看看这是否对结果有任何影响。如果我对else进行了注释,但仍然不起作用。它总是插入变量。如果((trim($userid)!='')和(&($videoid)!=''),您可以尝试如果((trim($videoid)!='')
可能会有类似空格的内容。@user3092548-如果在if语句之前回显$userid和$videoid,它们会输出什么?
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 (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'] . "");
}