473,383 Members | 1,862 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,383 software developers and data experts.

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 :D
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!

Expand|Select|Wrap|Line Numbers
  1. <?php
  2. session_start();
  3. require "conn.php";
  4. require "site_details.php"; ?>
  5.  
  6. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
  7. <html>
  8. <head>
  9. <title>Follow Users</title>
  10. <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
  11. </head>
  12. <body>
  13. <form name="form" action="" method="post">
  14. <table border=1 cellpadding=1 cellspacing=1>
  15.     <tr>
  16.         <th>Id</th>
  17.         <th>Username</th>
  18.         <th>Password</th>
  19.         <th>Email</th>
  20.         <th>Delete</th>
  21.     </tr>
  22. <?php
  23. $res=mysqli_query($conn,"SELECT * FROM users");
  24. while($row=mysqli_fetch_array($res))
  25. {
  26.     echo "<tr>";
  27.     echo "<td>"; echo $row["ids"]; ?> <input type="checkbox" name="num[]" class="other" value="<?php echo $row["ids"]; ?>" /> <?php echo "</td>";
  28.     echo "<td>"; echo $row["usernames"]; "</td>";
  29.     echo "<td>"; echo $row["passwords"]; "</td>";
  30.     echo "<td>"; echo $row["emails"]; "</td>";
  31.     echo "<td>"; echo "<input type='submit' name='submit' value='delete selected'>"; "</td>";
  32.     echo "</tr>";
  33. }
  34. ?>
  35. </table>
  36. </form>
  37. <?php
  38. if(isset($_POST["submit"]))
  39. {
  40.    $box=$_POST['num'];
  41.    while (list ($key,$val) = @each ($box))
  42.     {
  43.       mysqli_query($conn,"DELETE FROM users WHERE id='$val'");
  44.     }
  45. ?>
  46.        <script type="text/javascript">
  47.        window.location.href=window.location.href;
  48.        </script>
  49. <?php
  50. }
  51. ?>
  52.  
  53. </body>
  54. </html>
  55.  
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.

Expand|Select|Wrap|Line Numbers
  1. <?php
  2. session_start();
  3. require "conn.php";
  4. require "site_details.php"; ?>
  5.  
  6. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
  7. <html>
  8. <head>
  9. <title>Follow Users</title>
  10. <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
  11. </head>
  12. <body>
  13. <table border=1 cellpadding=1 cellspacing=1>
  14.     <tr>
  15.         <th>Id</th>
  16.         <th>Username</th>
  17.         <th>Password</th>
  18.         <th>Email</th>
  19.         <th>Delete</th>
  20.     </tr>
  21. <?php 
  22. $sql = "SELECT * FROM users";
  23. $result = mysqli_query($conn,$sql);
  24. while($row = mysqli_fetch_array($result))
  25.     {
  26.         echo "<tr>";
  27.         echo "<td>".$row['ids']."</td>";
  28.         echo "<td>".$row['usernames']."</td>";
  29.         echo "<td>".$row['passwords']."</td>";
  30.         echo "<td>".$row['emails']."</td>";
  31.         echo "<td><a href=delete2a.php?id=".$row['ids'].">Delete</a></td>";
  32.     }
  33.  
  34. ?>
  35. </table>
  36. </body>
  37. </html>
  38.  
  39.  
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!
Oct 16 '17 #1
1 1915
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:

Expand|Select|Wrap|Line Numbers
  1.  
  2. <?php
  3.  
  4. /*
  5. ERROR HANDLING
  6. */
  7. declare(strict_types=1);
  8. ini_set('display_errors', '1');
  9. ini_set('display_startup_errors', '1');
  10. error_reporting(E_ALL);
  11. mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
  12.  
  13. include 'config.php';
  14.  
  15. ?>
  16.  
  17. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
  18. <html>
  19. <head>
  20. <title>Follow Users</title>
  21. <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
  22. </head>
  23. <body>
  24. <form name="form" action="" method="post">
  25. <table border=1 cellpadding=1 cellspacing=1>
  26.     <tr>
  27.         <th>Id</th>
  28.         <th>Username</th>
  29.         <th>Password</th>
  30.         <th>Email</th>
  31.         <th>Delete</th>
  32.     </tr>
  33. <?php
  34. $res=mysqli_query($conn,"SELECT ids, usernames, passwords, emails FROM users");
  35. while($row=mysqli_fetch_array($res))
  36. {
  37.     echo "<tr>";
  38.     echo "<td>"; echo $row["ids"]; ?> <input type="checkbox" name="num[]" class="other" value="<?php echo $row["ids"]; ?>" /> <?php echo "</td>";
  39.     echo "<td>"; echo $row["usernames"]; "</td>";
  40.     echo "<td>"; echo $row["passwords"]; "</td>";
  41.     echo "<td>"; echo $row["emails"]; "</td>";
  42.     echo "<td>"; echo "<input type='submit' name='submit' value='delete selected'>"; "</td>";
  43.     echo "</tr>";
  44. }
  45. ?>
  46. </table>
  47. </form>
  48. <?php
  49. if(isset($_POST["submit"]))
  50. {
  51.    $box=$_POST['num'];
  52.    while (list ($key,$val) = @each ($box))
  53.     {
  54.       mysqli_query($conn,"DELETE FROM users WHERE ids='$val'");
  55.     }
  56. ?>
  57.        <script type="text/javascript">
  58.        window.location.href=window.location.href;
  59.        </script>
  60. <?php
  61. }
  62. ?>
  63.  
  64. </body>
  65. </html>
  66.  
  67.  
But, how come it's working since this is deprecated:

@each


And this also working:

Expand|Select|Wrap|Line Numbers
  1. <?php
  2.  
  3. /*
  4. ERROR HANDLING
  5. */
  6. declare(strict_types=1);
  7. ini_set('display_errors', '1');
  8. ini_set('display_startup_errors', '1');
  9. error_reporting(E_ALL);
  10. mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
  11.  
  12. include 'config.php'; 
  13.  
  14. ?>
  15.  
  16. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
  17. <html>
  18. <head>
  19. <title>Follow Users</title>
  20. <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
  21. </head>
  22. <body>
  23. <table border=1 cellpadding=1 cellspacing=1>
  24.     <tr>
  25.         <th>Id</th>
  26.         <th>Username</th>
  27.         <th>Password</th>
  28.         <th>Email</th>
  29.         <th>Delete</th>
  30.     </tr>
  31. <?php 
  32. $sql = "SELECT ids, usernames, passwords, emails FROM users";
  33. $result = mysqli_query($conn,$sql);
  34. while($row = mysqli_fetch_array($result))
  35.     {
  36.         echo "<tr>";
  37.         echo "<td>".$row['ids']."</td>";
  38.         echo "<td>".$row['usernames']."</td>";
  39.         echo "<td>".$row['passwords']."</td>";
  40.         echo "<td>".$row['emails']."</td>";
  41.         echo "<td><a href=delete2a.php?ids=".$row['ids'].">Delete</a></td>";
  42.     }
  43.  
  44. ?>
  45. </table>
  46. </body>
  47. </html>
  48.  
  49.  
Also, why on the 1st code the concatenation is used while on the 2nd one not ?

Expand|Select|Wrap|Line Numbers
  1. echo "<td>"; echo $row["usernames"]; "</td>";
  2.  
Expand|Select|Wrap|Line Numbers
  1. echo "<td>"; echo .$row["usernames"]; "</td>";
  2.  
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!
Oct 16 '17 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

0
by: Fraser Hanson | last post by:
Hello, I have a table which has a foreign key relationship with itself. I want and expect my updates to cascade (deletes definitely cascade as expected) but instead I just get error 1217:...
4
by: rb | last post by:
I have two XP computers, with the same version of MySQL on each. I have the same database structure and data on each. Each computer has the same user name, and each MySQL database has the same...
0
by: Vic | last post by:
Hi all, When I test the Delete multi table function in MySQL, DELETE table_name ...] FROM table-references I accidentally delete all data in one table. All data in that table are gone...
12
by: Dan Greenblatt | last post by:
I am writing some software that, among other things, needs to track the state of database tables. This includes occasionally checking the table to see what records or added, modified, or deleted....
12
by: Martin_Hurst | last post by:
It would appear that MySQL is making great strides into the commercial and even the enterprise arena. I am not seeing the same news coverage being said about Postgresql. I believe Postgresql has...
7
by: John Salerno | last post by:
My web server told me it isn't, which is why they are sticking with MySQL 4.0 for now, but I'm obsessed with using the latest versions, so I just want to be sure. According to the mysqldb download...
1
by: thomasjbs | last post by:
I'm trying to migrate a Wiki database to another server. I have zero problems backing up the database using multiple methods. But the Mysql restore fails with: ERROR 1064 at line 52: You have...
2
by: David | last post by:
Hi, I have a MySQL database linked to an MS Access front-end. I have an app which works fine with the local copy of the Access DB, but when run with the linked tables for MySQL, it fails. ...
6
rizwan6feb
by: rizwan6feb | last post by:
I have to develop a database application in vb.net 2005 with the following rules 1. The application connects with an online mysql database (i.e database resides on a remote machine) 2. The...
0
Atli
by: Atli | last post by:
What to discuss: What is a "MySQL resource". What causes the error. How to fix it. Common Newbie Pitfalls This article is the second installment in a series of (hopefully) many, following...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.