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>
donglian7879
donglian7879 将表格移到桌子外面。stackoverflow.com/questions/5967564/form-inside-a-table
4 年多之前 回复
dongwei5740
dongwei5740 试了很多例子。他们都没有为我工作...vineetsaini.wordpress.com/2013/09/06/...等
4 年多之前 回复
douwu3763
douwu3763 给你写一些代码?呃......不。尝试一下。
4 年多之前 回复

1个回答

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.
Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问