PHP Mysql Row Deletion Fails
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4

Thread: Mysql Row Deletion Fails

  1. #1
    Join Date
    Aug 2017
    Posts
    14

    Question 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. Not sure where to define it and how.
    Confesssion
    I actually play-paused 2 youtube videos and copy typed the code you see. That is one way I learn and gain a little work experience. Was not aware that the "each" function was deprecated until another told me.
    Even though, I checked the foreach loop tutorial, here I am at a loss how to make use of it. Feeling flabbergasted in my failure!

    Code:
    <?php
    session_start();
    require "conn.php";
    require "site_details.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"]; "</td>";
    	echo "<td>"; echo $row["passwords"]; "</td>";
    	echo "<td>"; echo $row["emails"]; "</td>";
    	echo "<td>"; echo "<input type='submit' name='submit' value='delete selected'>"; "</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>
    Here's another one. But, this one does not have the DELETE BUTTON. DELETE LINK instead. Same problem I'm facing on this one too. Not deleting anything.

    Code:
    <?php
    session_start();
    require "conn.php";
    require "site_details.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>
    <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 
    $sql = "SELECT * FROM users";
    $result = mysqli_query($conn,$sql);
    while($row = mysqli_fetch_array($result))
    	{
    		echo "<tr>";
    		echo "<td>".$row['ids']."</td>";
    		echo "<td>".$row['usernames']."</td>";
    		echo "<td>".$row['passwords']."</td>";
    		echo "<td>".$row['emails']."</td>";
    		echo "<td><a href=delete2a.php?id=".$row['ids'].">Delete</a></td>";
    	}
    
    ?>
    </table>
    </body>
    </html>
    Can some good Samaritan fix the 2 codes so we beginners can learn from your examples ?
    I've come this far on these 2. Don't want to quit at the end.

    Cheers!

  2. #2
    Join Date
    Aug 2017
    Posts
    14

    Re: Mysql Row Deletion Fails

    I corrected this:

    mysqli_query($conn,"DELETE FROM users WHERE id='$val'");

    to this:

    mysqli_query($conn,"DELETE FROM users WHERE ids='$val'");

    And this code is working:

    Code:
    <?php
    
    /*
    ERROR HANDLING
    */
    declare(strict_types=1);
    ini_set('display_errors', '1');
    ini_set('display_startup_errors', '1');
    error_reporting(E_ALL);
    mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
    
    include 'config.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 ids, usernames, passwords, emails 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"]; "</td>";
    	echo "<td>"; echo $row["passwords"]; "</td>";
    	echo "<td>"; echo $row["emails"]; "</td>";
    	echo "<td>"; echo "<input type='submit' name='submit' value='delete selected'>"; "</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 ids='$val'");
        }
    ?>
           <script type="text/javascript">
           window.location.href=window.location.href;
           </script>
    <?php
    }
    ?>
    
    </body>
    </html>
    But, how come it's working since this is deprecated:

    @each


    And this also working:

    Code:
    <?php
    
    /*
    ERROR HANDLING
    */
    declare(strict_types=1);
    ini_set('display_errors', '1');
    ini_set('display_startup_errors', '1');
    error_reporting(E_ALL);
    mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
    
    include 'config.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>
    <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 
    $sql = "SELECT ids, usernames, passwords, emails FROM users";
    $result = mysqli_query($conn,$sql);
    while($row = mysqli_fetch_array($result))
    	{
    		echo "<tr>";
    		echo "<td>".$row['ids']."</td>";
    		echo "<td>".$row['usernames']."</td>";
    		echo "<td>".$row['passwords']."</td>";
    		echo "<td>".$row['emails']."</td>";
    		echo "<td><a href=delete2a.php?ids=".$row['ids'].">Delete</a></td>";
    	}
    
    ?>
    </table>
    </body>
    </html>
    Also, why on the 1st code the concatenation is used while on the 2nd one not ?

    Code:
    echo "<td>"; echo $row["usernames"]; "</td>";
    Code:
    echo "<td>"; echo .$row["usernames"]; "</td>";
    Do you suggest I use the concatenator or not ?

    @anyone:
    Any chance anyone can convert those 2 codes to use not deprecated stuffs so other beginners too can learn from your input ?

    Thanks!

  3. #3
    Join Date
    Aug 2017
    Posts
    32

    Re: Mysql Row Deletion Fails

    It means you are testing, evaluating, or printing a variable that you have not yet assigned anything to. It means you either have a typo, or you need to check that the variable was initialized to something first. Check your logic paths, it may be set in one path but not in another.

  4. #4
    Join Date
    Aug 2017
    Posts
    14

    Re: Mysql Row Deletion Fails

    Quote Originally Posted by Priya456 View Post
    It means you are testing, evaluating, or printing a variable that you have not yet assigned anything to. It means you either have a typo, or you need to check that the variable was initialized to something first. Check your logic paths, it may be set in one path but not in another.
    Thanks!
    But, you answered my 1st post and missed the 2nd.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This a Codeguru.com survey!


On-Demand Webinars (sponsored)