0

我制作了一个表单,它采用发布的 youtube 网址并将视频 ID 保存到数据库中。然后它从数据库中检索 ID,并将其插入 iframe。

只显示了 6 个视频,如何让用户在发布 6 个视频后,每次发布新视频时删除第一个视频?

基本上我希望它只有 6 个存储在数据库中,并且每次提交新帖子时,都会从数据库中删除第一个帖子。

这就是我现在所拥有的。

<div style="position:absolute; top:100px; left:830px; height:120px; width:680px; background-color:#d0d0d0; font-size:18px; color:#f1f1f1; text-align:center; z-index:0; overflow:none; color:#111;  -moz-box-shadow:inset 0px 1px 0px 0px #ffffff;
    -webkit-box-shadow:inset 0px 1px 0px 0px #ffffff;
    box-shadow:inset 0px 1px 0px 0px #ffffff;
    background:-webkit-gradient( linear, left top, left bottom, color-stop(0.05, #ededed), color-stop(1, #dfdfdf) );
    background:-moz-linear-gradient( center top, #ededed 5%, #dfdfdf 100% );
    filter:progid:DXImageTransform.Microsoft.gradient(startColorstr='#ededed', endColorstr='#dfdfdf');
    background-color:#ededed;
    -moz-border-radius:6px;
    -webkit-border-radius:6px;">

<form action="index.php" method="post">
<label for="song">What are you listening to?:</label><br>
<textarea name="song" onfocus="this.select()" id="song" rows="3" cols="71" style="text-align:center;">Post a youtube link here.</textarea>
<input type="submit" class="personal" value="Share" /> 
</form>
</div>



<?php
if ($_POST['song'])
{
$yvid = $_POST['song'];
parse_str( parse_url( $yvid, PHP_URL_QUERY ), $my_array_of_vars );
$yid = $my_array_of_vars['v'];
mysqli_query ($con,"INSERT INTO songs (user_name, ytid) VALUES ('".$_SESSION['user_name']."','".$yid."')");
}
?>
<div style="position:absolute; display:inline-block; background-color:transparent; height:560px; width:800px; left:810px; top:200px; border:0px; color:#dedede; overflow:auto;">
<?php

$result = mysqli_query($con,"SELECT * FROM songs WHERE user_name='".$_SESSION['user_name']."' ORDER BY time DESC LIMIT 6");
while($row = mysqli_fetch_array($result))
  {
        $ytsong = $row['ytid'];
        $ytun = $row['user_name'];
        $ytt = $row['time'];
        echo "<p style=\"display:inline-block; height:220px; width:200px; text-align:center; padding:20px; border:0px; background-color:transparent;\">";
        echo "<a class=\"vname\" href=\"http://s3v3nl3tt3r5.com/profile.php?username=$ytun\">$ytun</a> posted this at $ytt";
    echo "<iframe style=\"display:inline-block;\" width=\"200\" height=\"200\" src=\"http://www.youtube.com/embed/$ytsong\" frameborder=\"0\" allowfullscreen></iframe>";
    echo "<a class=\"personal\" href=\"http://www.youtube-mp3.org/get?video_id=$ytsong\" target=\"_blank\">Download Mp3</a>";
}
?>
</div>
4

3 回答 3

1

id在表中创建一个自动增量列songs

然后插入新值。插入后。获取行数和最小 id :

'select count(*), min(id) from songs where user_name = '.$user_name;

检查计数是否超过 6 对 min id 执行删除语句

if($count > 6)
{
"delete from songs where id = $min_id "
}
于 2013-04-08T08:47:59.140 回答
0

使用主键自动递增创建一个新列“id”并尝试以下查询。

DELETE FROM songs WHERE id = (SELECT id FROM songs WHERE 
user_name='".$_SESSION['user_name']."' ORDER BY TIME ASC LIMIT 1)
于 2013-04-08T08:44:33.513 回答
0
I think you need to take new column in songs table
for example you can take id column auto-increment  and primary 

After Insertion check count of row for that user

SELECT count(*) as total FROM songs WHERE user_name='".$_SESSION['user_name']."' 

if total come greater than six then get minimum  id from songs table and 

SELECT min(ID) FROM songs WHERE user_name='".$_SESSION['user_name']."' 

after getting minimum id you can run delete query 

delete from song where id=$minid
于 2013-04-08T08:41:24.980 回答