使用PHP制作youtube播放列表。我收到错误。我不确定出了什么问题

时间:2012-12-16 20:22:02

标签: php youtube

我正在建立一个网站,人们可以搜索音乐(来自YouTube),并在同一个网站上观看。 (无需访问YouTube。) 在我的数据库中,我有一个播放列表表。该表有4个值。 ID,用户ID,艺术家和歌曲。

id是唯一ID - 我正在使用YouTube ID。 userID是用户的id。如果他们已登录,则他们无法使用播放列表功能。我已经有了一个数据库。

我有以下PHP代码,这些信息必须保存在数据库中,但它不起作用。

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

                        echo '<button type="add to playlist" action="post">ADD</button>';


                        include 'playlistdb.php';
                        $sql = "insert into playlist (i,d,a,t) values ('$_POST[ID]','$_POST[userID]','$_POST[artist]','$_POST[title]')";
                        $result = mysql_query($sql,$conn) or die(mysql_error());
                    }
                    else{
                        echo "You are not logged in, you can use the playlist.";
                    }

但这不起作用。我收到以下错误。

注意:未定义的索引:第112行的C:\ wamp \ www \ IKEPROJECT GROUP 9 \ video.php中的ID

和userID,艺术家和标题相同。

我不确定这里出了什么问题。

错误来自这一行:

$sql = "insert into playlist (i,d,a,t) values ('$_POST[ID]','$_POST[userID]','$_POST[artist]','$_POST[title]')";

3 个答案:

答案 0 :(得分:1)

我认为按钮实体是错误的。使用这样的格式 <button type="submit" formmethod="post" formaction="submit.php">ADD</button>

答案 1 :(得分:1)

考虑使用PDO并以可读的方式格式化代码。

$sth = $dbh->prepare("insert into playlist (i,d,a,t) values (?, ?, ?, ?)");
$sth->execute(array($_POST['ID'], $_POST['userID'], $_POST['artist'], $_POST['title']));

如果仍有错误,请确保您的名称为“ID”的输入字段有效。

答案 2 :(得分:0)

你需要引号 - 它不是$ _POST [ID],它是$ _POST ['ID']。请参阅Saeed Najafi对PHP格式的属性的评论。

此外,您的代码中确实存在严重的SQL注入漏洞...您修复该漏洞非常重要,否则有人可能会破解您的网站,删除您的数据库等。