How to delete multiple rows from mysql database with checkbox using PHP?

后端 未结 4 838
佛祖请我去吃肉
佛祖请我去吃肉 2021-01-16 22:25

I try to delete my data in \"admin\" database, but the delete button does not function.

This is my top part



        
相关标签:
4条回答
  • 2021-01-16 23:08
                             <?php
                                $args1 = array(
                                    'role' => 'Vendor',
                                    'orderby' => 'user_nicename',
                                    'exclude' => $user_id.',1',
                                    'order' => 'ASC'
                                );
                                $subscribers = get_users($args1);                                        foreach ($subscribers as $user) {
                                    $fvendorck = $wpdb->get_row("select * from wp_vandor where parent_id = '".$user_id."' and child_id = '".$user->id."'");
                                    $isfavvendor = $fvendorck->child_id;
                                 if(!empty($isfavvendor)) {
                                 ?>
                                    <li><input type="checkbox" id="listID" value='<?php echo $user->id; ?>' name="chk1[]" checked=""/><?php echo $user->headline; ?></li>
    
                            <?php  }else{ ?>
    
                                    <li><input type="checkbox" id="listID" value='<?php echo $user->id; ?>' name="chk1[]" /><?php echo $user->headline; ?></li>
    
                            <?php } }?>
                        </ul>
    
    0 讨论(0)
  • 2021-01-16 23:09
    $sql = "SELECT * FROM blacklist";
    $result = $link->query($sql);
    $count=mysqli_num_rows($result);
    
    if ($result->num_rows > 0) {
     while($row = $result->fetch_assoc()) 
    {
    echo "<table>";
    echo "<th>";
    echo    "<td>" . "ID: " . $row["id"]."</td>";
    echo    "<td>" . " Dial Target: " . $row["dial_target"]."</td>";
    echo    "<td>" . " Destination: " . $row["pozn"]."</td>";
    echo    "<td>" . " Date: " . $row["block_date"] . "</td>";
    echo    "<td>" . "<div class='background' style='position: relative; top:8px;'>" . "<form>" . "<input action='index.php' method='post' type='checkbox' name='chechbox[]' value='".$row["id"]."'/>" ."</form>" . "</div>" . "</td>";
    echo "</th>";
    echo "</table>"; 
    echo "</br>";
    }
    }
    else
    {
     echo "0 results";
    }
    
    
    if(isset($_POST['Delete']))
    {
    for($i=0;$i<$count;$i++)
    {
    $del_id = $checkbox[$i];
     $del = "DELETE FROM blacklist WHERE Delete='$del_id'";
    $result = $link->query($del);
    }
    if($result)
    {
    echo "<meta http-equiv=\"refresh\" content=\"0;URL=index.php\">";
    }
    }
    
    <!-- DELETE BUTTON -->
    <form>
    <input type='Submit' id="Delete" value='Delete' name='Delete'/>
    </form>
    
    0 讨论(0)
  • 2021-01-16 23:17

    include all the input elements within your <form> tags: <form> all inputs are here </form>

    update:

    <input name = "checkbox[]" type="checkbox"  id="checkbox[]" value="<?php echo     $rows['course_code'];?>">
    

    to (id doesn't matter here):

    <input name="checkbox[]" type="checkbox"  value="<?php echo $rows['course_code'];?>"/>
    

    and your button code:

    <input type='button' id="delete" value='Delete' name='delete'>
    

    to

    <input type="submit" value="Delete"/>
    

    set opening <form> tag to <form action="delete.php" method="post">

    Note: I assume below codes are in delete.php file. if not replace "delete.php" with that name in above opening form tag.

    your delete.php file:

    <?php
    $cheks = implode("','", $_POST['checkbox']);
    $sql = "delete from $tbl_name where course_code in ('$cheks')";
    $result = mysql_query($sql) or die(mysql_error());
    mysql_close();
    ?>
    

    Note: Since mysql_ will deprecate on future, better is use mysqli extension. But before use that, you have to enable it on your server. mysqli is a part of php and newer version of php has it but not enabled. To enable this, view php info page and find the path of php.ini file in "Loaded Configuration File" row on that page. You can see php info page by loading below php file in the browser:

    <?php
     phpinfo();
    ?>
    

    open that php.ini file in a text editor and un-comment or add a line extension=php_mysqli.dll at the extensions list there. also search for "extension_dir" and open the directory it says and make sure php_mysqli.dll file is there. (you may have .so extension if you not use windows OS)

    Then restart your server and you are done!

    By Fred -ii-

    Using mysqli_ with prepared statements is indeed a better and safer method. However, some will even suggest PDO, but even PDO doesn't have some of the functionalities that mysqli_ offers; strangely that. Even PDO needs sanitization. Many think that using PDO will solve injection issues, which is false. -Thanks Fred.

    0 讨论(0)
  • 2021-01-16 23:20

    try this code. it is working well. connection.php

    <?php $hostname_conection = "localhost"; /* this is the server name(assigned to variable) which is localhost since it runs on local machine */ 
    $database_conection = "company"; /* this is the database name( assigned to variable)*/ 
    $username_conection = "root"; /* user name (assigned to variable)*/
    $password_conection = ""; /*password (assigned to variable) */ 
    $conection = mysql_connect($hostname_conection, $username_conection, $password_conection) or trigger_error(mysql_error(),E_USER_ERROR); /* Mysql_connect function is used to conncet with database it takes three parameters server/hostname, username,and password*/ 
    mysql_select_db($database_conection,$conection) or die(mysql_error("could not connect to database!")); /* Mysql_select is used to select the database it takes two parameters databasename and connection variable in this case $conection */ 
    ?>
    

    multiple_delete.php

    <?php require_once('conection.php'); ?> 
    <?php 
    in
    /* now to display the data from the database which we inserted in above form we */ /* we make the query to select data from the table EMP */ 
                $display = "select * from test_mysql";
            $result = mysql_query($display, $conection) or die(mysql_error()); /* the query is executed and result of the query is stored in variable $result */ 
            if ($result == FALSE) {
                die(mysql_error()); /* displays error */
            } ?> <h1 align="center"> Displaying Recods in Table </h1> 
            <form method="get" action="" id="deleteform" > 
                <table width="245" border="1" align="center"> 
                    <tr>
                        <td width="51">
                            <input type="submit" name="delete" id="button" value="delete" onclick="document.getElementById('deleteform').action = 'delete.php';document.getElementById('deleteform').submit();"/> <!--- here on clicking the button the form is submitted and action is set to delete.php Here we have used javaScript document refers to this whole page and now we can access any tag that has its id with help of getElementById() method and after the we specify the operation we want to perform in this case action and submit. ---> 
                        </td> 
                        <td width="50">id</td> 
                        <td width="55">name</td>
                        <td width="47">lastname</td>
                    </tr>
     <?php 
     while ($rows = mysql_fetch_array($result)) 
            { /* here we make use of the while loop which fetch the data from the $result int array form and stores in $row now we can display each field from the table with $row[‘field_name’] as below */ 
         ?> 
                    <tr>
                        <td>
                            <input type="checkbox" name="empids[]" value="<?php echo $rows['id']; ?>" /> <!--here with each checkbox we send the id of the record in the empids[] array ---> 
                        </td>
                        <td>
                            <?php echo $rows['id'] ?>
                        </td>
                            <td>
                                <?php echo $rows['lastname'] ?>
                            </td>
                            <td><?php echo $rows['name'] ?></td> 
                                <?php } ?>
                    </tr>
                </table>
            </form> ?>
        </body>
    </html>
    

    delete.php

    <?php 
    require_once('conection.php'); 
    ?>
     <?php
     if (isset($_GET['delete'])) /* checks weather $_GET['delete'] is set*/
         {
         if (isset($_GET['empids'])) /* checks weather $_GET['empids'] is set */ 
             { 
             $checkbox = $_GET['empids']; /* value is stored in $checbox variable */ 
             if (is_array($checkbox)) 
                 { 
                 foreach ($checkbox as $key => $your_slected_id) /* for each loop is used to get id and that id is used to delete the record below */ 
                     { 
                     $q="DELETE FROM test_mysql WHERE id=$your_slected_id "; /* Sql query to delete the records whose id is equal to $your_slected_id */
                     mysql_query($q,$conection) ; /* runs the query */ 
    
                     } 
                     header("location:multiple_delete.php"); /* Goes back to index.php */ 
    
                     } 
    
                     } else 
                         { 
                         echo" you have not selected reords .. to delete"; 
    
                         } 
    
                         } ?>
    
    0 讨论(0)
提交回复
热议问题