当用户通过PHP更新他/她的状态时,如何删除mysql中的额外行

时间:2013-04-08 08:28:52

标签: php mysql database

我制作了一个表单,其中包含已发布的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>

3 个答案:

答案 0 :(得分: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 "
}

答案 1 :(得分: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

答案 2 :(得分:0)

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

DELETE FROM songs WHERE id = (SELECT id FROM songs WHERE 
user_name='".$_SESSION['user_name']."' ORDER BY TIME ASC LIMIT 1)