Russia Posted August 24, 2009 Share Posted August 24, 2009 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. Quote Link to comment https://forums.phpfreaks.com/topic/171701-delete-member-from-databse/ Share on other sites More sharing options...
merck_delmoro Posted August 24, 2009 Share Posted August 24, 2009 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()); } } } Quote Link to comment https://forums.phpfreaks.com/topic/171701-delete-member-from-databse/#findComment-905402 Share on other sites More sharing options...
Russia Posted August 24, 2009 Author Share Posted August 24, 2009 It is already like that: I just need a button to delete it to go to the page that has the php to delete it. Quote Link to comment https://forums.phpfreaks.com/topic/171701-delete-member-from-databse/#findComment-905417 Share on other sites More sharing options...
merck_delmoro Posted August 25, 2009 Share Posted August 25, 2009 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); ?> Quote Link to comment https://forums.phpfreaks.com/topic/171701-delete-member-from-databse/#findComment-905440 Share on other sites More sharing options...
Russia Posted August 25, 2009 Author Share Posted August 25, 2009 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. Quote Link to comment https://forums.phpfreaks.com/topic/171701-delete-member-from-databse/#findComment-905444 Share on other sites More sharing options...
merck_delmoro Posted August 25, 2009 Share Posted August 25, 2009 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); ?> Quote Link to comment https://forums.phpfreaks.com/topic/171701-delete-member-from-databse/#findComment-905459 Share on other sites More sharing options...
Russia Posted August 25, 2009 Author Share Posted August 25, 2009 How do I add a SELECT all button? Quote Link to comment https://forums.phpfreaks.com/topic/171701-delete-member-from-databse/#findComment-905480 Share on other sites More sharing options...
merck_delmoro Posted August 25, 2009 Share Posted August 25, 2009 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] Quote Link to comment https://forums.phpfreaks.com/topic/171701-delete-member-from-databse/#findComment-905503 Share on other sites More sharing options...
Russia Posted August 25, 2009 Author Share Posted August 25, 2009 Also, am I able to make the buttons above the List of members. I have tried making a seperate Php statement for it, but didnt work. Quote Link to comment https://forums.phpfreaks.com/topic/171701-delete-member-from-databse/#findComment-905752 Share on other sites More sharing options...
Russia Posted August 25, 2009 Author Share Posted August 25, 2009 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); ?> Quote Link to comment https://forums.phpfreaks.com/topic/171701-delete-member-from-databse/#findComment-905877 Share on other sites More sharing options...
merck_delmoro Posted August 25, 2009 Share Posted August 25, 2009 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"; } Quote Link to comment https://forums.phpfreaks.com/topic/171701-delete-member-from-databse/#findComment-906212 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.