#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2017
    Posts
    318
    Rep Power
    1

    Question Why Mysql Row Deletion Fails ?


    Folks,

    Here is a php code that lists all the rows and columns in your mysql tbl for you to select the rows (via check boxes) you want to delete and then delete them by clicking the appropriate "delete" buttons.
    Problem is, when I click any of the "delete" buttons, I get error flicking for a sec that there is an undefined variable $num. But, the $num has been defined. So, should not be getting this error and the selected row should get deleted.

    Code:
        <?php
    
        require "conn.php";
    
        <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" 
        "http://www.w3.org/TR/html4/loose.dtd">
        <html>
        <head>
        <title>Follow Users</title>
        <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
        </head>
        <body>
        <form name="form" action="" method="post">
        <table border=1 cellpadding=1 cellspacing=1>
    	    <tr>
    		    <th>Id</th>
    		    <th>Username</th>
    		    <th>Password</th>
    		    <th>Email</th>
    		    <th>Delete</th>
    	    </tr>
        <?php
        $res=mysqli_query($conn,"SELECT * FROM users");
        while($row=mysqli_fetch_array($res))
        {
    	    echo "<tr>";
    	    echo "<td>"; echo $row["ids"]; ?> <input type="checkbox" name="num[]" 
        class="other" value="<?php echo $row["ids"]; ?>" /> <?php echo "</td>";
    	    echo "<td>"; echo $row["usernames"]; echo "</td>";
    	    echo "<td>"; echo $row["passwords"]; echo "</td>";
    	    echo "<td>"; echo $row["emails"]; echo "</td>";
    	    echo "<td>"; echo "<input type='submit' name='submit' value='delete 
        selected'>"; echo "</td>";
    	    echo "</tr>";
        }
        ?>
        </table>
        </form>
        <?php
        if(isset($_POST["submit"]))
        {
           $box=$_POST['num'];
           while (list ($key,$val) = @each ($box))
    	    {
              mysqli_query($conn,"DELETE FROM users WHERE id='$val'");
            }
        ?>
               <script type="text/javascript">
               window.location.href=window.location.href;
               </script>
        <?php
        }
        ?>
    
        </body>
        </html>
  2. #2
  3. Banned (not really)
    Devshed Supreme Being (6500+ posts)

    Join Date
    Dec 1999
    Location
    Caro, Michigan
    Posts
    14,814
    Rep Power
    4536
    There's no $num variable in that code, so you're obviously not getting a notice about an undefined variable named $num.

    Maybe an undefined index, if no boxes were checked.

    If you want to keep pretending to be a programmer, remove the redirection until you get this error sorted, so you can actually see what's going on. Learn how to open the developer panel so you can see the actual POST request and what data was passed.
    -- Cigars, whiskey and wild, wild women. --

IMN logo majestic logo threadwatch logo seochat tools logo