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.
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 做个有关计算的小程序
  • ¥15 MPI读取tif文件无法正常给各进程分配路径
  • ¥15 如何用MATLAB实现以下三个公式(有相互嵌套)
  • ¥30 关于#算法#的问题:运用EViews第九版本进行一系列计量经济学的时间数列数据回归分析预测问题 求各位帮我解答一下
  • ¥15 setInterval 页面闪烁,怎么解决
  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化