更新和删除MySQL数据库表中的数据

时间:2015-09-23 05:16:32

标签: mysql sql-update delete-row

我有一个数据库表。我想更新和删除该表中的数据。我是php和MySQL的新手。我试过但不能工作。请有人帮帮我,请????? 我的代码如下:

更新------------

<table border = "1">
        <tbody>
            <caption>
                Update or Delete your event.                
            </caption>
            <tr>
                <th>No</th>
                <th>Message</th>
                <th>Update/Delete</th>
            </tr>
            <?php 
                while ($row = mysql_fetch_array($sqldata)) {
                    ?>
                <tr class="grey">
                    <td class="no">1</td>
                    <td class="message"><?php echo $row['message']; ?></td>
                    <td class="button">
                        <a href="editevent.php"><input type="button" name="update" value="Update"></a>
                        <input type="button" name="delete" value="Delete">
                    </td>
                </tr>

                <tr class="white">
                    <td class="no">2</td>
                    <td class="message"><?php echo $row['message']; ?></td>
                    <td class="button">
                        <a href="editevent.php"><input type="button" name="update" value="Update"></a>
                        <a href="delete.php"><input type="button" name="delete" value="Delete"></a>
                    </td>
                </tr>

            <?php
                    break 1;
            }
             ?>
        </tbody>        
    </table>

删除:--------------

<?php 

    $id = $_GET['id'];


    $delete = "DELETE FROM calendar_event WHERE id='".$id."'";

    mysql_query($con,$delete);
    mysql_close($con);

 ?>

代码不合适。拜托,任何人都给我一个正确的代码。

4 个答案:

答案 0 :(得分:2)

您做的事情绝对是非常糟糕的如果您在公共网站上发布此消息,您将在第二天醒来,将所有数据删除,由Google删除。

但是,如果您坚持这样做,只需将id添加到查询请求中即可:

 <td class="button">
     <a href="editevent.php?id=<?php echo $row['id'] ?>"><input type="button" name="update" value="Update"></a>
     <a href="delete.php?id=<?php echo $row['id'] ?>"><input type="button" name="delete" value="Delete"></a>
 </td>

答案 1 :(得分:1)

你必须通过&#39; id&#39;在查询字符串中。取代

    <a href="editevent.php"><input type="button" name="update" value="Update"></a>
    <a href="delete.php"><input type="button" name="delete" value="Delete"></a>

    <a href="editevent.php?id=<?php echo $row['id'];?>"><input type="button" name="update" value="Update"></a>
    <a href="delete.php?id=<?php echo $row['id'];?>"><input type="button" name="delete" value="Delete"></a>

假设查询正在获取id。

答案 2 :(得分:1)

我可以看到2个错误

  1. 在第一个tr中,您使用了删除按钮而不是标签,而在第二个tr中,您使用的标签是有意或错误的我建议使用标签。

  2. 我认为您为删除编写的第二个代码块:--------来自页面delete.php?我是对的,如果是,那么看起来你似乎想要从$id = $_GET['id'];删除记录的id,但是你没有在上一页的get中传递任何id所以所有和所有更新你的第一页的代码你是按照如下方式绘制表格,并用数据库中元素的实际Id替换

    <table border = "1">
    <tbody>
        <caption>
            Update or Delete your event.                
        </caption>
        <tr>
            <th>No</th>
            <th>Message</th>
            <th>Update/Delete</th>
        </tr>
        <?php 
            while ($row = mysql_fetch_array($sqldata)) {
                ?>
            <tr class="grey">
                <td class="no">1</td>
                <td class="message"><?php echo $row['message']; ?></td>
                <td class="button">
                    <a href="editevent.php?id=<?php echo <Your element Id goes here> ?>"><input type="button" name="update" value="Update"></a>
                    <a href="delete.php?id=<?php echo <Your element Id goes here> ?>"><input type="button" name="delete" value="Delete"></a>
                </td>
            </tr>
    
            <tr class="white">
                <td class="no">2</td>
                <td class="message"><?php echo $row['message']; ?></td>
                <td class="button">
                     <a href="editevent.php?id=<?php echo <Your element Id goes here> ?>"><input type="button" name="update" value="Update"></a>
                    <a href="delete.php?id=<?php echo <Your element Id goes here> ?>"><input type="button" name="delete" value="Delete"></a>
                </td>
            </tr>
    
        <?php
                break 1;
        }
         ?>
    </tbody>        
    

答案 3 :(得分:0)

<?php 
$i=0;
while ($row = mysql_fetch_array($sqldata)) {
    $class = ($i%2==0) ? 'white':'grey';
    ?>
    <tr class="<?php echo $class; ?>">
        <td class="no"><?php echo ($i+1); ?></td>
        <td class="message"><?php echo $row['message']; ?></td>
        <td class="button">
            <a href="editevent.php?id=<?php echo $row['id']; ?>"><input type="button" name="update" value="Update"></a>
            <a href="delete.php?id=<?php echo $row['id']; ?>"><input type="button" name="delete" value="Delete"></a>
        </td>
    </tr>
<?php $i++; } ?>
============
delete
============
<?php 

    $id = $_GET['id'];
    $delete = "DELETE FROM calendar_event WHERE id='".$id."'";
    mysql_query($con,$delete);
    mysql_close($con);

 ?>
相关问题