PHP Developers Network

A community of PHP developers offering assistance, advice, discussion, and friendship.
 
Loading
It is currently Thu Dec 14, 2017 5:11 am

All times are UTC - 5 hours




Post new topic This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 
Author Message
 Post subject: Mysql Row Deletion Fails
PostPosted: Mon Oct 16, 2017 5:56 am 
Offline
Forum Contributor

Joined: Wed Jan 18, 2017 4:43 pm
Posts: 179
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!

Syntax: [ Download ] [ Hide ]
<?php
session_start&#40;&#41;;
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&#40;$conn,"SELECT * FROM users"&#41;;
while&#40;$row=mysqli_fetch_array&#40;$res&#41;&#41;
{
        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=&#39;submit&#39; name=&#39;submit&#39; value=&#39;delete selected&#39;>"; "</td>";
        echo "</tr>";
}
?>
</table>
</form>
<?php
if&#40;isset&#40;$_POST["submit"]&#41;&#41;
{
   $box=$_POST[&#39;num&#39;];
  while &#40;list &#40;$key,$val&#41; = @each &#40;$box&#41;&#41;
        {
      mysqli_query&#40;$conn,"DELETE FROM users WHERE id=&#39;$val&#39;"&#41;;
   }
?>
       <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.

Syntax: [ Download ] [ Hide ]
<?php
session_start&#40;&#41;;
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&#40;$conn,$sql&#41;;
while&#40;$row = mysqli_fetch_array&#40;$result&#41;&#41;
        {
                echo "<tr>";
                echo "<td>".$row[&#39;ids&#39;]."</td>";
                echo "<td>".$row[&#39;usernames&#39;]."</td>";
                echo "<td>".$row[&#39;passwords&#39;]."</td>";
                echo "<td>".$row[&#39;emails&#39;]."</td>";
                echo "<td><a href=delete2a.php?id=".$row[&#39;ids&#39;].">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!


Top
 Profile  
 
PostPosted: Mon Oct 16, 2017 6:12 am 
Offline
Moderator
User avatar

Joined: Tue Nov 09, 2010 3:39 pm
Posts: 6402
Location: Montreal, Canada
Don't you already have a thread for this? viewtopic.php?f=1&t=144456

_________________
Supported PHP versions No longer supported versions


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 

All times are UTC - 5 hours


Who is online

Users browsing this forum: Bing [Bot], Yahoo [Bot] and 21 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Jump to:  
Powered by phpBB® Forum Software © phpBB Group