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

报告相同问题?

悬赏问题

  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘
  • ¥15 perl MISA分析p3_in脚本出错
  • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
  • ¥15 ubuntu虚拟机打包apk错误
  • ¥199 rust编程架构设计的方案 有偿
  • ¥15 回答4f系统的像差计算
  • ¥15 java如何提取出pdf里的文字?