new_born191285 Posted August 17, 2008 Share Posted August 17, 2008 Hi all, I am writing some PHP for a website where an administrator may view applications and then choose to either delete or approve them by means of a checkbox on the right. I have a problem when I try and delete an application- where I if there were 4 applications for example, if I choose the checkbox for the 3rd application then the 2nd one would be deleted instead. I have investigated this, and it is always the element before the one I wish to delete that is actually deleted which has left me and a friend stuck on how to solve it. page.php - where the applications are shown, with checkboxes process.php - where the applications are deleted page.php <?php session_start(); //always start sessions here, or in an include /************** keep headers above html and other output *************/ if (!(isset($_SESSION['login']) && $_SESSION['login'] != '')) { header ("Location: login.php"); exit(); // exit to stop the rest of the page from being processed after the user is redirected } else { $user_name = "REMOVED"; $password = "REMOVED"; $database = "Agent"; $server = "127.0.0.1"; $db_handle = mysql_connect($server, $user_name, $password); $db_found = mysql_select_db($database, $db_handle); $id_array=array(); $name_array=array(); $dob_array=array(); $gender_array=array(); $status_array=array(); $email_array=array(); $home_array=array(); $mobile_array=array(); $work_array=array(); $ed_type=array(); $ed_subject=array(); $high_exp_title=array(); $exp_sector=array(); $years_worked=array(); $checked=array(); if (!$db_found) // the exclamation mark means NOT, as in, not found or not set { echo "Database NOT Found "; //use echo instead of print lol, suppose it doesnt really matter tho mysql_close($db_handle); } else // put the processing after the error report, just good practice { ?> <html> <head> <title>Administation</title> </head> <body> <?php $SQL = "SELECT * FROM tblcandidate WHERE checked=0"; $result = mysql_query($SQL); $a = 0; /********* WHEN UVE GOT ARRAYS, TO STORE STUFF U GOTTA INCREMENT THEM! OR ITLL JUST OVERWRITE OR FAIL *******/ while ($db_field = mysql_fetch_assoc($result)) { $id_array[$a] = $db_field['id']; // make sure ur variables match ($id, doesnt match $id_array! $name_array[$a] = $db_field['name']; $dob_array[$a] =$db_field['dob']; $gender_array[$a] = $db_field['gender']; $email_array[$a] = $db_field['email']; $home_array[$a] = $db_field['home']; $mobile_array[$a] = $db_field['mobile']; $work_array[$a] = $db_field['work']; $ed_type[$a] = $db_field['edtype']; $ed_subject[$a] = $db_field['coursename']; $high_exp_title[$a] = $db_field['hipostitle']; $exp_sector[$a] = $db_field['ExSector']; $years_worked[$a] = $db_field['yearsworked']; $checked[$a] = $db_field['checked']; $a++; } mysql_close($db_handle); $_SESSION['id_array']=$id_array; ?> <form action="process.php" method="post"> <table border="1" font-size=2 width="600"> <tr align="left" valign="top"> <td width="100" height="10"><font size=3px>Name</font></td> <td width="40" height="10"><font size=3px>Date of birth</font></td> <td width="40" height="10"><font size=3px>Gender</font></td> <td width="100" height="10"><font size=3px>Email</font></td> <td width="50" height="10"><font size=3px>Home Tel</font></td> <td width="50" height="10"><font size=3px>Mobile Tel</font></td> <td width="50" height="10"><font size=3px>Work Tel</font></td> <td width="150" height="10"><font size=3px>Highest Qualifcation</font></td> <td width="150" height="10"><font size=3px>Highest job title</font></td> <td width="30" height="10"><font size=3px>Sector</font></td> <td width="20" height="10"><font size=3px>Years worked</font></td> <td width="20"></td> </tr> <?php //keep php in lower case $b= count($name_array); $a=0; //declare $a as 0 again, itll not be 0 atm cos of the first loop while ($a<$b) { echo "<tr>"; echo "<td><font size=2px>".$name_array[$a]."</font></td>"; echo "<td><font size=2px>".$dob_array[$a]."</font></td>"; echo "<td><font size=2px>".$gender_array[$a]."</font></td>"; echo "<td><font size=2px>".$email_array[$a]."</font></td>"; echo "<td><font size=2px>".$home_array[$a]."</font></td>"; echo "<td><font size=2px>".$mobile_array[$a]."</font></td>"; echo "<td><font size=2px>".$work_array[$a]."</font></td>"; echo "<td><font size=2px>".$ed_type[$a]." ".$ed_subject[$a]."</font></td>"; echo "<td><font size=2px>".$high_exp_title[$a]."</font></td>"; echo "<td><font size=2px>".$exp_sector[$a]."</font></td>"; echo "<td><font size=2px>".$years_worked[$a]."</font></td>"; echo "<td><input type='checkbox' name='checked[".$a."]' value ='id_array[".$a."]' /><td>"; //dont get lost with your quotes and php tags! echo "</tr>"; //the dots on either side of the $a are concatanating the echo line $a++; } ?> </table> <input type="submit" name="Delete" value="Delete"> <input type="submit" name="Approve" value="Approve"> </form> <?php print mysql_fetch_assoc($result); ?> <a href="logout.php">Log out</a> </body> </html> <?php } // end of else process page } // end else for successful login ?> process.php <?php session_start(); if ($_POST['Delete']) { $user_name = "REMOVED"; $password = "REMOVED"; $database = "Agent"; $server = "127.0.0.1"; $db_handle = mysql_connect($server, $user_name, $password); $db_found = mysql_select_db($database, $db_handle); if (!$db_found) // the exclamation mark means NOT, as in, not found or not set { echo "Database NOT Found "; //use echo instead of print lol, suppose it doesnt really matter tho mysql_close($db_handle); } $inid_array=$_SESSION["id_array"]; foreach ($_POST['checked'] as $value) { $sql="DELETE FROM tblcandidate WHERE id=$value"; $result=mysql_query($sql); } } else if ($_POST['Approve']) { $user_name = "REMOVED"; $password = "REMOVED"; $database = "Agent"; $server = "127.0.0.1"; $db_handle = mysql_connect($server, $user_name, $password); $db_found = mysql_select_db($database, $db_handle); if (!$db_found) // the exclamation mark means NOT, as in, not found or not set { echo "Database NOT Found "; //use echo instead of print lol, suppose it doesnt really matter tho mysql_close($db_handle); } $inid_array=$_SESSION["id_array"]; $countCheck= count($inid_array); for($i=0;$i<$countCheck;$i++) { $del_id = $inid_array[$i]; $sql = "UPDATE tblcandidate SET checked=true WHERE id = $del_id"; $result = mysql_query($sql, $dbconn); } $check=$countCheck-1; if($i==$check) { echo "Candidate approved"; } else { echo "Error: ".mysql_error(); } } ?> Can anyone offer any suggustions on how to fix the loop problem? Cheers, Link to comment https://forums.phpfreaks.com/topic/120014-loop-problem-checkboxes-deleting-wrong-records/ Share on other sites More sharing options...
chronister Posted August 17, 2008 Share Posted August 17, 2008 If I could read the code I would be happy to help, but all that space is difficult to read. Re-format and re-post and you may get better responses. Cheers Link to comment https://forums.phpfreaks.com/topic/120014-loop-problem-checkboxes-deleting-wrong-records/#findComment-618308 Share on other sites More sharing options...
toplay Posted August 17, 2008 Share Posted August 17, 2008 Change this: echo "<td><input type='checkbox' name='checked[".$a."]' value ='id_array[".$a."]' /><td>"; to this: echo "<td><input type='checkbox' name='checked[]' value ='", $id_array[$a], "' /><td>"; Link to comment https://forums.phpfreaks.com/topic/120014-loop-problem-checkboxes-deleting-wrong-records/#findComment-618315 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.