hey all! i've got a CMS i'm working on and i have a couple of HTML tables that are populated via php loops communicating with the SQL database. got it? good.

at the end of each row in the HTML table there is a checkbox. i need to be able to click as many of them as i want, and then press "delete" and have them all be deleted, and then immediately returned to the exact same page.


each HTML table row is exactly like the SQL row. each row has an ID, so that is probably the way i'd identify each row (it's worked for me so far =P).

so can i get any advice, or direction. i'm almost positive i'm gonna have to store the value of the checkboxes in an array of somesort, somehow but i have NO idea where to even start. thanks!

Recommended Answers

All 7 Replies

declare the checkboxes as array

e.g.

<html>
<body>
<form>
<input type = 'checkbox' name = 'mychk[]'>
<input type = 'checkbox' name = 'mychk[]'>
<input type = 'checkbox' name = 'mychk[]'>
<input type ='submit'>
</form>
</body>
</html>

now you have three check boxes with the same name

post it to a page

and then use

foreach ( $_POST['mychk'] as $k=> $c)
{
       if ($c == 'On')
       {   pSQL = "DELETE FROM YOUR TABLE WHERE NAME = $k"
         //execute the sql 
       }
}

Where k is the key here, or the index,

you can set the primary key as the key. or you can even use hidden text boxes to hide the corrosponding ID of the checkbox. however this is the main idea.


hope it will be of use.

that IS very useful, thank you =) except the page will just reload and display the new results (after deletion of old results).

does NOT work. what is wrong with my code before i shoot somebody!!!!!

/*****DEBUG INFO*******
        echo "<pre>";
        print_r($_POST['delete']);
        echo implode($_POST['delete'], ", ");
        echo "</pre>";
**********************/
        foreach($_POST['delete'] as $k => $c)
        {
            // echo $k ." / ". $c ." || ";  <-- prints the EXACT correct information for the sql query to be properly executed.
            $sql = "DELETE FROM my_search_table WHERE uID = ". $c ."";
            mysql_query($sql);
           }

it should work. but it doesn't. WHAT THE HE77!!!!!!

actually $c is a boolean value either ON or OFF

so you need to use an id

so uID must have been somehow generated in your table

can you please show us how you generate the table (mean the php code that generates the table)

try to put the index as your id :)

like this

<html>
<body>
<form>
<input type = 'checkbox' name = 'mychk[6]'>
<input type = 'checkbox' name = 'mychk[9]'>
<input type = 'checkbox' name = 'mychk[10]'>
<input type ='submit'>
</form>
</body>
</html>

now you can use the "$k"

maybe you will get use of our internet project :)

this is the checkbox part of it


viewsection.php
this code contains a part which can accept more then one request using checkboxes same as your idea

<?php
session_start();?>

<html>
<head>
<script language = "javascript">
function sure (stid)
{
 y = confirm ("ARE YOU SURE YOU WANT TO DELETE  THIS STUDENT WITH ID " + stid + " FROM THE DATABASE?");
 
 if (y)
 {
  w = window.open ("delstudent.php?stu=" + stid);
  w.opener.location = "viewsection.php";  
   
 }
}
function checkall ()
{
 //alert("called");
 for (i = 0; i < document.forms[0].elements.length; i ++ )
  document.forms[0].elements[i].checked=1;

}
</script>

</head>

<?
$iid=$_SESSION['iid'];
$s = $_SESSION["s"];
$isroot = $_SESSION["theroot"];
$pend = $_SESSION['pend'];
$cntrec=0;
if (!isset($iid)) 
{
 header("Location:anauthorized.html");
 die("");
}

include('DB_connectscript.php');
include 'calc.php';
//check if he actually can view this section or not (if he is not root ONLY)
if (!$isroot)
{
 $ret = mysql_query ("SELECT * FROM SECTION WHERE SNUM = ".$s." AND INSID = ".$iid);
 if(!mysql_num_rows($ret))
 {
  echo "<h1 align = 'center' > Tring to view others sections? </h1>";
  exit;
 }
}
 
echo "<body background='back.jpg' >";
echo "<form name = 'frm' method = 'POST' action = 'acceptrequest.php'>";
if ($pend != "STUDENT"){
 $result = mysql_query("SELECT NAME, SID, EMAIL, MAJOR FROM ".$pend." WHERE SNUM = ".$s) or die(mysql_error());
 echo "<H2 align=center><font color='#0'>Section #".$s."<br>Pending Students</font></H2><BR><BR>"; 
}
else{
 echo "<H2 align=center><font color='#0'>Section #".$s."</font></H2><BR><BR>";
 $result = mysql_query("SELECT NAME, SID, EMAIL,  GRADE, MAJOR, COMMENT, ATTN  FROM ".$pend." WHERE SNUM = ".$s) or die(mysql_error());
}

echo "<table border=1 align ='center'>\n";
$cnt=0;
if ($pend == "STUDENT")
 echo "<tr><th>Section #</th><th> Student Name </th><th>StudentID #</th><th>Email</th><th>Grade</th><th>Major</th><th> Instructor comments </th><th> Attendance</th><th>Option</th></tr>";
else
 {echo "<tr><th>Section #</th><th> Student Name </th><th>StudentID #</th><th>Email</th><th>Major</th><th>Enrollment</th></tr>";
 $cnt=5;
 }

while ($myrow = mysql_fetch_row ($result))
{
 echo "<tr align = 'center'>\n";
 echo "<td>".$s."</td>";
 printf ("<td>%s</td>\n", !$myrow[0]? "-" : $myrow[0]);
 echo "<td>".$myrow[1]."</td>\n";
 //echo "<td><a href = 'mailto:".$myrow[2]."'>".$myrow[2]."</a></td>\n";
 printf ("<td>%s</td>\n", !$myrow[2]? "-" : "<a href = 'mailto:".$myrow[2]."'>".$myrow[2]."</a>");
 
 if ($pend == "STUDENT")
 {
  printf ("<td>%s</td>\n", !$myrow[3]? "-" : $myrow[3]);
  printf ("<td>%s</td>\n", !$myrow[4]? "-" : $myrow[4]);
  echo "<td>".$myrow[5]."<small> <a href = 'tmpcomment.php?sid=".$myrow[1]."'> edit comment? </a></small></td>\n";
  echo "<td>".$myrow[6]."<small> <a href = 'attn.php?sid=".$myrow[1]."&at=".$myrow[6]."'>edit attendance? </a></small></td>\n";   
  echo "<td><img border = 0 src = 'delete.gif' onclick = 'sure(".$myrow[1].")'> </td>";
 }
 else
 {
  printf ("<td>%s</td>\n", !$myrow[3]? "-" : $myrow[3]);
  //echo "<td><small> <a href = 'acceptrequest.php?sid=".$myrow[1]."'> Accept Request? </a></small></td>\n"; 
  echo "<td><input type = 'checkbox' name = acc[".$myrow[1]."]></td>";
  $cntrec++;
 }
 
 echo "</tr>\n\n";
}

if ($pend == "STUDENT")
{ echo "</table><br><br>";
 echo "<p align = center><a  href = 'addstudent1.php'> Add student </a></p>";
 echo "<p align = 'center'><a href = 'showgrade.php'> Show Assignment(s) Grade </a></p>";
 echo "<p align = 'center'><a href = 'addassif.php'> Add/Hand New Assignment </a></p>";
 echo "<p align = 'center'><a href = 'showpgrade.php'> Show Project(s) Grade </a></p>";
 echo "<p align = 'center'><a href = 'addproject.php'> Add/Submit New Project</a></p>";
 echo "<p align = 'center'><a href = 'showegrade.php'> Show Exam(s) Grade </a></p>";
 echo "<p align = 'center'><a href = 'addexam.php'> Add/Submit New Exam</a></p>";
 echo "<p align = 'center'><a href = 'uploadlecture.php'> Upload Lectures </a>";
 
 echo "<br><p align='center'><a href = 'allstudents.php'><img border=0 src='backa.gif'></a></p>";
 
}
else
{
 if ($cntrec) {
     
 
 echo "<tr>";
 for ($i = 0; $i < $cnt ; $i++)
  echo "<td></td>";
 echo "<td><input type = 'submit' name = 'submit' value = 'Accept'><input type = 'button' name = 'ck' value = 'Check All' onClick = 'checkall()' ></td></tr>";
 }
 echo "</table><br><br></form>";
 
 echo "<br><p align='center'><a href = 'request.php'><img border=0 src='backa.gif'></a></p>";
 
}
?>
 
 
 
</body></html>

here's what my HTML checkbox looks like...

<TD align=center><input type=checkbox name=delete[] value=". $row['uID'] .">

here's my foreach loop...

foreach($_POST['delete'] as $k => $c){
            //echo $k ." / ". $c ." || ";  //<-- prints the EXACT correct information for the sql query to be properly executed.
            $sql = "DELETE * FROM my_search_table WHERE uID = ". $c ."";
            mysql_query($sql);
}

when i uncomment the debugging line and comment out the other two lines, and click a couple of checkboxes, then press delete, this is the output it gives me...

0 / 3ZFc7FmpYiGpyTybHKXix14teZsQlQ || 1 / FVWxsWALHwtzWjKX23NsMXwbjImaqh || 2 / pSWURh1EsraukzZwYHwiF4DHJnaB3B ||

so, what i see is that it's passing the correct information to the foreach loop ($c is the uID), but for some reason or another, my SQL query won't process the request, and i can't figure out why. i'm thinkin the problem resides in my SQL query... can anybody help?

IT WORKS IT FINALLY WORKS!!!!! it was the '*' and the single quotes in the SQL query that were causing all the problems!!!! IT WORKS!!!!

$_POST['delete'];
if($_POST['delete'] != NULL){
    foreach($_POST['delete'] as $k => $c){
        //echo $k ." / ". $c ." || ";  //<-- prints the EXACT correct information for the sql query to be properly executed.
        $sql = "DELETE FROM my_search_table WHERE uID = '". $c ."'";
        mysql_query($sql) OR die ("The query:<br>" . $sql . "<br>Caused the following error:<br>" . mysql_error());
    }
}

RIGHTEOUS!!!!!! thank you guys so much!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.