我有一个页面,您可以在其中将视频添加到播放列表中。这是通过单击“添加”按钮来完成的。
但是问题是每次我进入此页面时,无论点击添加按钮,它都会将该视频添加到您的播放列表中,即使它已经存在。
您必须有一个帐户才能使用播放列表功能。当您登录时,添加按钮是可见的,如果没有登录,则您看不到添加按钮。
这是我添加到数据库的代码:
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/