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
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());
        }
    }
}

Link to comment
Share on other sites

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);
?> 

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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);
?> 

Link to comment
Share on other sites

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]

Link to comment
Share on other sites

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);
?> 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.