dsgwdigu84950 2016-05-11 19:30
浏览 91
已采纳

PHP删除数据复选框

So im kinda trying to delete row from my mysql database using checkboxes. Here is my code. Would be glad if someone could write me down simple delete code. Tryed many but failed, stuck for hours with this :(

        <html>
    <head>
    <title>Admin, User</title>
    </head>
    <body>
    <?php include 'connect.php';?>
    <?php include 'functions.php';?>
    <?php include 'title_bar.php';?>

    <h3>Sukurti Nauja uzduoti: </h3>
    <form method='post'>
    <?php
    if(isset($_POST['submit2']))
    {
        $pav = $_POST['pav'];
        $uzduotis = $_POST['uzduotis'];
        if(empty($pav) or empty($uzduotis)){
        echo "<p>Privalomi visi langai!</p>";
       } 
       else { 
       $sql = ("INSERT INTO uzduotys VALUES ('','$pav','$uzduotis')");
       }
       if($database->query($sql) == TRUE)
    {
        header('location:kurtisalinti.php');
     }
     else {
         echo "<p>Klaida!</p>";
     }
    }
     ?>
    <p>Uzduoties pavadinimas: 
    <p><input type='text' name='pav' />
    <p>Uzduotis:
    <p><textarea name='uzduotis'></textarea>
    <p><input type='submit' name='submit2' value='Sukurti Uzduoti' />
    </form>
    </p></p></p></p>
    </form>

    <h3>Pasalinti pasirinkta uzduoti is uzduociu saraso: </h3>
    <?php

    $query = mysqli_query($database,"SELECT uzid,pav,uzduotis FROM uzduotys");
    $count=mysqli_num_rows($query);

    ?>

    <table width="400" border="0" cellspacing="1" cellpadding="0">
    <tr>
    <td><form name="form1" method="post" action="">
    <table width="400" border="0" cellpadding="3" cellspacing="1" bgcolor="#CCCCCC">
    <tr>
    <td align="center" bgcolor="#FFFFFF">#</td>
    <td align="center" bgcolor="#FFFFFF"><strong>id</strong></td>
    <td align="center" bgcolor="#FFFFFF"><strong>Pavadinimas</strong></td>
    <td align="center" bgcolor="#FFFFFF"><strong>Aprasymas</strong></td>
    </tr>

    <?php
    while($rows = mysqli_fetch_array($query)){
    ?>

    <tr>
    <td align="center" bgcolor="#FFFFFF"><input name="checkbox[]" type="checkbox" value="<?php echo $row['uzid']; ?>"></td>
    <td bgcolor="#FFFFFF"><?php echo $rows['uzid']; ?></td> 
    <td bgcolor="#FFFFFF"><?php echo $rows['pav']; ?></td>
    <td bgcolor="#FFFFFF"><?php echo $rows['uzduotis']; ?></td>
    </tr>
    <?php
    }
    ?>
    <tr>
    <td colspan="5" align="center" bgcolor="#FFFFFF"><input name="delete" type="submit" id="delete" value="Istrinti Uzduotis"></td>
    </tr>


    <?php

    ?>



    </table>
    </form>
    </td>
    </tr>
    </table>


    </body>
    </html>
  • 写回答

1条回答 默认 最新

  • dreamone5156 2016-05-11 22:59
    关注

    Welcome to StackOverflow! Generally as a rule of thumb asking to write you some code doesn't get you very far, however I know that some people learn better from seeing it implemented and being able to modify off of that.

    There is a number of ways you can do this. Using something like jquery and ajax you can post your requests without having to reload the page. Keep in mind what I have below may not fully work for you since taking the time to do this much already without a database or connection should give you an idea of what you're looking to do. Good luck!

    CAUTION: PSUEDO CODE (This may not fully work and is not intended for production) The idea is to show you an approach to achieving what you are looking for.

    <?php 
    
        include('connect.php');
        include('functions.php');
        include('title_bar.php');
    
    
        if(isset($_POST['submit-uzduotis']))
        {
            $pav = $_POST['pav'];
            $uzduotis = $_POST['uzduotis'];
    
            if(empty($pav) or empty($uzduotis))
            {
                $result = "<span>Privalomi visi langai!</span>";
            } 
            else 
            { 
                $sql = ("INSERT INTO uzduotys VALUES ('','$pav','$uzduotis')");
            }
    
            if($database->query($sql) == true)
            {
                header('location:kurtisalinti.php');
            }
            else 
            {
                $result = "<span>Įvyko klaida!</span>";
            }
        }
    
        if(isset($_POST['remove']))
        {
            $id = mysql_real_escape_string($_POST['remove']);
            mysqli_query($database, "DELETE FROM uzduotys WHERE uzid = $id");       
        }
    
        function showTableData()
        {
            $query = mysqli_query($database, "SELECT uzid, pav, uzduotis FROM uzduotys");
            $count = mysqli_num_rows($query);           
    
            while($row = mysqli_fetch_array($query))
            {
                echo '<tr id="'. $row['uzid'] .'">
                        <td>
                            <input type="checkbox" name="checkbox[]" value="'. $row['uzid'] .'">
                        </td>
                        <td>'. $row['uzid'] .'</td>
                        <td>'. $row['pav'] .'</td>
                        <td>'. $row['uzduotis'] .'</td>
                        <td>
                            <button id="remove-single" data-id="'. $row['uzid'] .'">REMOVE</button>
                        </td>
                    </tr>';
            }
        }
    ?>
    <html>
       <head>
          <title>Admin, User</title>
          <style>
    
            .uzduotis-table  {
                background-color: #ccc;
                border-collapse: collapse;
            }
    
            .uzduotis-table thead > td {
                background-color: #333;
                font-weight: bold;
            }
    
            .uzduotis-table td {
                padding: 3px;
                text-align: center;
                background-color: #fff;
            }
    
            label {
                display:block;
                position:relative;
                font-weight: bold;
            }
    
          </style>
       </head>
       <body>  
          <h3>Sukurti Nauja uzduoti: </h3>
          <form method='post'>
            <?php echo $result; ?>
            <div>
                <label>Uzduoties pavadinimas:</label>
                <input type='text' name='pav' />
            </div>
            <div>
                <label>Uzduoties:</label>
                <textarea name='uzduotis'></textarea>
            </div>
            <div>
                <input type='submit' name='submit-uzduoties' value='Sukurti Uzduoti' />
            </div>
          </form>
          <h3>Pasalinti pasirinkta uzduoti is uzduociu saraso: </h3>
          <table class="uzduotis-table">
            <thead>
                <tr>
                    <td>#</td>
                    <td>Id</td>
                    <td>Pavadinimas</td>
                    <td>Aprasymas</td>
                </tr>
            </thead>
            <tbody>
                <?php showTableData(); ?>
                <tr>
                    <td colspan="5">
                        <button id="remove-selected" data-id="'. $row['uzid'] .'">REMOVE SELECTED</button>
                    </td>
                </tr>
             </tbody>
          </table>
          <script src="https://code.jquery.com/jquery-2.2.3.min.js" integrity="sha256-a23g1Nt4dtEYOj7bR+vTu7+T8VP13humZFBJNIYoEJo=" crossorigin="anonymous"></script>
          <script>
            $(document).ready(function(){
    
                // Remove Single (Button)
                $('body').on('click', '#remove-single', function() {
                    remove($(this).data('id')); 
                });
    
                // Remove All Checked
                $('body').on('click', '#remove-selected', function() {
    
                    $('.uzduotis-table tr').filter(':has(:checkbox:checked)').each(function() {
                        remove(this.id);
                    });
                });
    
                function remove(id)
                {
                    console.log("Remove: " + id);
                    alert('Removing: ' + id);
    
                    $.ajax({
                        type: "POST",
                        url: '<?php echo $url; ?>',
                        data: { remove: id },
                        success: function (data) {
                            $('#' + id).remove();                       
                        }
                    });
                }
    
            })
          </script>
       </body>
    </html>
    
    • I cleaned up a bit of the html and styles, just made it easier for me to read. Also I would recommend looking into a library such as PDO to handle your database queries as there currently isn't much in place to protect or safe guard against sql injection.
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 求数据集和代码#有偿答复
  • ¥15 关于下拉菜单选项关联的问题
  • ¥20 java-OJ-健康体检
  • ¥15 rs485的上拉下拉,不会对a-b<-200mv有影响吗,就是接受时,对判断逻辑0有影响吗
  • ¥15 使用phpstudy在云服务器上搭建个人网站
  • ¥15 应该如何判断含间隙的曲柄摇杆机构,轴与轴承是否发生了碰撞?
  • ¥15 vue3+express部署到nginx
  • ¥20 搭建pt1000三线制高精度测温电路
  • ¥15 使用Jdk8自带的算法,和Jdk11自带的加密结果会一样吗,不一样的话有什么解决方案,Jdk不能升级的情况
  • ¥15 画两个图 python或R