单击按钮时,将数据添加到MySQL,而不是加载页面时



我有一个页面,您可以在其中将视频添加到播放列表中。这是通过单击添加按钮来完成的。

但是,问题是每次我都进入此页面,无论挂断添加按钮,它都会在播放列表中添加该视频,即使它已经存在。

您必须有一个帐户才能使用播放列表功能。登录时可见"添加"按钮,如果不在登录时,您将看不到"添加"按钮。

这是我添加到数据库的代码:

if (isset($_SESSION['username'])){

                echo '<button type="submit" formmethod="post" formaction="video.php" onClick="Confirm(this.form)">ADD</button>';
                $userID = $_SESSION['username'];
                $ID = $watch;
                $artist = $new[0];
                $title = $new[1];
                $youtubeID = $code;
                include 'opendb.php';
                $sql = "insert into youtube_playlist (userid,youtubeID,artist,title) values ((select id from users where username = "$userID"),"$youtubeID","$artist","$title")";
                $result = mysql_query($sql,$conn) or die(mysql_error());
            }
            //if not logged in.
            else{
                echo "You are not logged in, you cant use the playlist feature.";
            }

因此,即使您登录并尝试观看视频时,它也将其添加到播放列表中,即使您不想。您如何确保这永远不会发生?

您总是可以尝试这样的事情:

<html>
<head>
<title>My Website</title>
</head>
<body>
<?php
    if (isset($_POST['add'])) {
        $userID = $_SESSION['username'];
        $ID = $watch;
        $artist = $new[0];
        $title = $new[1];
        $youtubeID = $code;
        include 'opendb.php';
        $sql = "insert into youtube_playlist (userid,youtubeID,artist,title) values ((select id from users where username = "$userID"),"$youtubeID","$artist","$title")";
        $result = mysql_query($sql,$conn) or die(mysql_error());
    }
    if (isset($_SESSION['username'])){
        echo "<form action='' method='post'>
            <input name='add' type='submit' value='Add'/>
        </form>";
    } else {
        echo "You are not logged in, you cant use the playlist feature.";
    }
?>
</body>
</html>

相关内容

  • 没有找到相关文章

最新更新