doupeng5320 2013-04-29 17:14
浏览 159
已采纳

删除按钮(删除phpmyadmin表中的行)

I am doing a website with login system were each user can insert and view his logs.

Now I am trying to make a delete button that when clicked an entire row from a table is deleted.

It is working fine because when I click the "DELETE BUTTON" and check the rows from thee phpmyadmin shell the row is no longer there.

The problem is that on the first click the row is still visible in the webpage and only when I click the button again it disappears!

Here is my code: -

<?php   
    $entries = mysql_query("SELECT `id`,`date`, `location`, `description` FROM `logs` WHERE `username` = '$current_user'");


if(mysql_num_rows($entries)==0){

    echo 'No entries, yet.';}
    else{


        while($entries_row = mysql_fetch_assoc($entries)){          
            echo    " 
                <hr> 
                <form action='' method='post'> 
                <input type='hidden' name='id' value='".$entries_row['id']."'> 
                <table align='center' border='1' width='80%' cellpadding='5'> 
                <tr> 
                <td width='20%'><strong>Date:</strong></td> 
                <td>" . $entries_row['date'] . "</td> 
                </tr>     
                <tr> 
                <td width='20%'><strong>Location:</strong></td> 
                <td>" . $entries_row['location'] . "</td> 
                </tr> 
                <tr> 
                <td width='20%'><strong>Description:</strong></td> 
                <td>" . $entries_row['description'] . "</td> 
                </tr> 
                <tr> 
                <td colspan='2' align='right'><input type='submit' name='delete' value='Delete Entry'></td> 
                </tr> 
                </table> 
                </form> 
                    "; 
        }
    }   

}else{
    echo 'Could not connect at this time.';
}

if(isset($_POST['delete']))
    { 
    mysql_query("DELETE FROM `logs` WHERE `id` = '".$_POST['id']."' ");
    } 


?>
  • 写回答

4条回答 默认 最新

  • dongyan5706 2013-04-29 17:21
    关注

    I think that you need to move your delete code above the rows query

    if(isset($_POST['delete']))
        { 
        mysql_query("DELETE FROM `logs` WHERE `id` = '".$_POST['id']."' ");
        } 
    
    $entries = mysql_query("SELECT `id`,`date`, `location`, `description` FROM `logs` WHERE `username` = '$current_user'");
    

    You are querying all the rows before you delete the row.

    Also...

    Please make note of Samitha's comment above.

    I would recommend adding a 'deleted' column to your table, and adding a timestamp to the 'deleted' column when a row is "deleted", rather than deleting the entire row. Then, your query for would look something like this:

    $entries = mysql_query("SELECT `id`,`date`, `location`, `description` FROM `logs` WHERE `username` = '$current_user' AND `deleted` != `NULL`");
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥15 winform的chart曲线生成时有凸起
  • ¥15 msix packaging tool打包问题
  • ¥15 finalshell节点的搭建代码和那个端口代码教程
  • ¥15 用hfss做微带贴片阵列天线的时候分析设置有问题
  • ¥15 Centos / PETSc / PETGEM
  • ¥15 centos7.9 IPv6端口telnet和端口监控问题
  • ¥20 完全没有学习过GAN,看了CSDN的一篇文章,里面有代码但是完全不知道如何操作
  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 海浪数据 南海地区海况数据,波浪数据
  • ¥20 软件测试决策法疑问求解答