Jump to content

Delete member from databse


Russia

Recommended Posts

Hello I am working on a script to delete a member from an admin area, basicly a checkbox and a delete button so I can delete multiple at a time.

 

Here is the Php that shows all the members:

You will see a checkbox for delete.

<?php
require("inc/config.php");
$result = mysql_query("SELECT * FROM persons");
while($row = mysql_fetch_array($result))
  {
  echo "First Name: " . $row['FirstName'] . "";
  echo "<br>";
  echo "Last Name: " . $row['LastName'] . "";
  echo "<br>";
  echo "<a target=frame2 href='" ."profile.php?user1=". $row['FirstName'] ."'>Profile</a>";
  echo "<br>";
  echo "Date: " . $row['AddedDate'] . "";
  echo "<br>";
  echo "IP Address: " . $row['Ip'] . "";
  echo "<br>";
  echo '<input type="checkbox" name="delchk[]" value="'.$row['id'].'" />';  
  echo "<br>";
  echo "***********************************************";
  echo "<br>";
  }
echo "</table>";
mysql_close($con);
?> 

 

Here is the code that when u press delete:

 

if (is_array($_POST['delchk'])) {
    foreach ($_POST['delchk'] as $delId) {
        $query = "DELETE FROM persons WHERE person_id = $delId";
        $result = mysql_query($query);
        if (!$result) {
            die("Error deleting persons! Query: $query<br />Error: ".mysql_error());
        }
    }
}  

 

I currently need like a button that when clicked will delete all rows and somehow refresh the page.

Link to comment
https://forums.phpfreaks.com/topic/171701-delete-member-from-databse/
Share on other sites

change the name of checkbox to delchk[] to make it inserted to array

and from your php script do this

if (is_array($_POST['delchk'])) {
    for ($count = 0;count<count(delchk);count++) 
{
        $query = "DELETE FROM persons WHERE person_id = '$delchk[$count]'";
        $result = mysql_query($query);
        if (!$result) {
            die("Error deleting persons! Query: $query<br />Error: ".mysql_error());
        }
    }
}

try this

<?php

require("inc/config.php");
if (isset($_POST['del'])) 
{

    for ($count = 0;$count<count($_POST[delchk]);$count++)
{
        	$query = "DELETE FROM persons WHERE person_id = '$_POST[delchk][$count]'";
        	$result = mysql_query($query);
        	if (!$result) 
{
            die("Error deleting persons! Query: $query<br />Error: ".mysql_error());
        }
    }
}
$result = mysql_query("SELECT * FROM persons");
echo "<form action='' method='post'>";
while($row = mysql_fetch_array($result))
  {
  echo "First Name: " . $row['FirstName'] . "";
  echo "<br>";
  echo "Last Name: " . $row['LastName'] . "";
  echo "<br>";
  echo "<a target=frame2 href='" ."profile.php?user1=". $row['FirstName'] ."'>Profile</a>";
  echo "<br>";
  echo "Date: " . $row['AddedDate'] . "";
  echo "<br>";
  echo "IP Address: " . $row['Ip'] . "";
  echo "<br>";
  echo '<input type="checkbox" name="delchk[]" value="'.$row['id'].'" />';  
  echo "<br>";
  echo "***********************************************";
  echo "<br>";
  }
echo "</table><input type='submit' name = 'del' value='Delete'></form>";
mysql_close($con);
?> 

It is not deleting for some reason...

I have tried your exact code.

 

I just changed

 

$query = "DELETE FROM persons WHERE person_id = '$_POST[delchk][$count]'";

to

$query = "DELETE FROM persons WHERE id = '$_POST[delchk][$count]'";

 

Because this is the id name.

 

I click delete after I selected the ones I want to delete and it wont delete them.

i forgot something on the code try this

 

<?php

require("inc/config.php");
if (isset($_POST['del'])) 
{

    for ($count = 0;$count<count($_POST[delchk]);$count++)
   {
           $delete = $_POST[delchk][$count];
           $query = "DELETE FROM persons WHERE person_id = '$delete'";
           $result = mysql_query($query);
           if (!$result) 
   {
            die("Error deleting persons! Query: $query<br />Error: ".mysql_error());
        }
    }
}
$result = mysql_query("SELECT * FROM persons");
echo "<form action='' method='post'>";
while($row = mysql_fetch_array($result))
  {
  echo "First Name: " . $row['FirstName'] . "";
  echo "<br>";
  echo "Last Name: " . $row['LastName'] . "";
  echo "<br>";
  echo "<a target=frame2 href='" ."profile.php?user1=". $row['FirstName'] ."'>Profile</a>";
  echo "<br>";
  echo "Date: " . $row['AddedDate'] . "";
  echo "<br>";
  echo "IP Address: " . $row['Ip'] . "";
  echo "<br>";
  echo '<input type="checkbox" name="delchk[]" value="'.$row['id'].'" />';  
  echo "<br>";
  echo "***********************************************";
  echo "<br>";
  }
echo "</table><input type='submit' name = 'del' value='Delete'></form>";
mysql_close($con);
?> 

use JavaScript here is the code

<script type="text/javascript">
function checkall(chek)
{
	for (i = 0; i < chek.length; i++)
	chek[i].checked = true;
}
</script>
<?php

require("inc/config.php");
if (isset($_POST['del'])) 
{

    for ($count = 0;$count<count($_POST[delchk]);$count++)
   {
           $delete = $_POST[delchk][$count];
           $query = "DELETE FROM persons WHERE person_id = '$delete'";
           $result = mysql_query($query);
           if (!$result) 
   {
            die("Error deleting persons! Query: $query<br />Error: ".mysql_error());
        }
    }
}
$result = mysql_query("SELECT * FROM persons");
echo "<form name = 'myform' action='' method='post'>";
while($row = mysql_fetch_array($result))
  {
  echo "First Name: " . $row['FirstName'] . "";
  echo "<br>";
  echo "Last Name: " . $row['LastName'] . "";
  echo "<br>";
  echo "<a target=frame2 href='" ."profile.php?user1=". $row['FirstName'] ."'>Profile</a>";
  echo "<br>";
  echo "Date: " . $row['AddedDate'] . "";
  echo "<br>";
  echo "IP Address: " . $row['Ip'] . "";
  echo "<br>";
  echo '<input type="checkbox" id="delchk" name="delchk[]" value="'.$row['id'].'" />';  
  echo "<br>";
  echo "***********************************************";
  echo "<br>";
  }
echo "</table><input type='submit' name = 'del' value='Delete'><input type='button' onclick='checkall(document.myform.delchk);' value='CHECK ALL'></form>";
mysql_close($con);
?> 
[code]

Ok I did that, I need one more thing:

 

Here is the code:

 

I added a Unselect Button also.

 

Now what I need is that if there are no members it says 'No Members Registered'.

 

Like an else statement.

 

<script type="text/javascript">
   function checkall(chek)
   {
      for (i = 0; i < chek.length; i++)
      chek[i].checked = true;
   }
</script>

<script type="text/javascript">
   function uncheckall(chek)
   {
      for (i = 0; i < chek.length; i++)
      chek[i].checked = false;
   }
</script>

<?php

echo "<input type='button' onclick='checkall(document.myform.delchk);' value='Select All'>";
echo "<input type='button' onclick='uncheckall(document.myform.delchk);' value='UnSelect All'>";
require("inc/config.php");
if (isset($_POST['del'])) 
{

    for ($count = 0;$count<count($_POST[delchk]);$count++)
   {
           $delete = $_POST[delchk][$count];
           $query = "DELETE FROM persons WHERE id = '$delete'";
           $result = mysql_query($query);
           if (!$result) 
   {
            die("Error deleting persons! Query: $query<br />Error: ".mysql_error());
        }
    }
}
$result = mysql_query("SELECT * FROM persons");
echo "<form name = 'myform' action='' method='post'>";
while($row = mysql_fetch_array($result))
  {
  echo "First Name: " . $row['FirstName'] . "";
  echo "<br>";
  echo "Last Name: " . $row['LastName'] . "";
  echo "<br>";
  echo "<a target=frame2 href='" ."profile.php?user1=". $row['FirstName'] ."'>Profile</a>";
  echo "<br>";
  echo "Date: " . $row['AddedDate'] . "";
  echo "<br>";
  echo "IP Address: " . $row['Ip'] . "";
  echo "<br>";
  echo 'Select to delete: <input type="checkbox" id="delchk" name="delchk[]" value="'.$row['id'].'" />';  
  echo "<br>";
  echo "***********************************************";
  echo "<br>";
  }
echo "<input type='submit' name = 'del' value='Delete Selected'></form>";
mysql_close($con);
?> 

Use mysql_num_rows for counting the list of members inside your database

 

example

 

$query = mysql_query("SELECT username FROM members");

 

$result = mysql_num_rows($query);

 

if($result>0)

{

      // Do Something

}

else

{

      echo "No Members Registered";

}

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.