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

标签 php mysql youtube-api

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

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

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

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

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>

关于php - 单击按钮时而不是加载页面时将数据添加到 MySQL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13968040/

相关文章:

php - MYSQL 查询最近的条目

youtube-api - Youtube API 搜索

php - Youtube 直播 API : liveStreamingNotEnabled

PHP getimagesize - 无法打开流。错误的请求

php - Laravel 5 数据库播种中的命名冲突

mysql - MySQL 错误 1215

php - 未从 PHP/MySQL NuSOAP 获得响应

php - 如何使用php Youtube Api获取所有用户订阅?

php - 比较2个表的海量数据

javascript - 将正则表达式作为字符串存储在数据库中,然后检索以在 Javascript 和 PHP 中使用