Gilly79 Posted February 28, 2010 Share Posted February 28, 2010 Hello I have a table of returned items from a SQL back end database an i would like to be able to mark them for delete... I have put a checkbox in and it works on a submit button.. However when it goes to the php action page i get the error message DELETE FROM MESSAGES WHERE MessageNo = '' i think its not passing the message_no from the delete step but im not sure... Can anyone help me please??? Ive attatched the main file but putting below the peices of code i think are relevant: messages.php while ($row = @ mysqli_fetch_array($result)) { //while($row = mysql_fetch_row($result)) { echo "<tr>"; echo "<td >".$row["MessageNo"]."</td>"; echo "<td>".$row["MessageFrom"]."</td>"; echo "<td>".$row["MessageTo"]."</td>"; echo "<td>".$row["Subject"]."</td>"; echo "<td>".$row["Message"]."</td>"; echo "<td><input type=\"checkbox\" name=\"reply[]\" value=".$row["MessageNo"]." /></td>"; echo '<td><input type="checkbox" name="delete[]" value="'. $row['MessageNo']. '" />'; echo "</tr>"; delete.php script is <?php // Start the session require_once('startsession.php'); // Page requires admin rights //require_once('admin.php'); require_once('appvars.php'); require_once('connectvars.php'); // if id provided, then delete that record $delete = $POST['delete']; // create query to delete record $query = "DELETE FROM MESSAGES WHERE MessageNo = '$delete' "; $result = mysqli_query($dbc,$query); //$result = mysqli_query($query) or die ("Error in query: $query. ".mysqli_error()); // see if any rows were affected if (mysqli_affected_rows($dbc) > 0) { //If so , return to calling page using header function and HTTP_REFERER header("Location: {$_SERVER['HTTP_REFERER']}"); } else { // print error message echo "Error in query: $query".mysqli_error(); mysqli_close($dbc); exit; } ?> [attachment deleted by admin] Link to comment https://forums.phpfreaks.com/topic/193676-checkbox-delete-row-command/ Share on other sites More sharing options...
teamatomic Posted February 28, 2010 Share Posted February 28, 2010 name="delete[],$delete is an array and you treat it as a var,MessageNo = '$delete'. Also, have you completed the form in messages.php? All I see is the checkboxes and no <form></form> or submit. HTH Teamatomic Link to comment https://forums.phpfreaks.com/topic/193676-checkbox-delete-row-command/#findComment-1019441 Share on other sites More sharing options...
Gilly79 Posted February 28, 2010 Author Share Posted February 28, 2010 hiya There is issue with the form - I just copied over a snipit of the code on messages I have the following on my deletemessages.php action page $delete = $GET['delete']; Because the name of the delete checkbox is delete I thought that this would be what I needed to set the VAR as?? Link to comment https://forums.phpfreaks.com/topic/193676-checkbox-delete-row-command/#findComment-1019455 Share on other sites More sharing options...
teamatomic Posted February 28, 2010 Share Posted February 28, 2010 In the form you use name=delete[], which is an array, it makes sense it would be an array as how would you post multiple checkbox values without an array? so you need to treat $delete as an array. If you only have one checkbox then use name=delete, without the square brackets, and it will be a var instead of an array. HTH Teamatomic Link to comment https://forums.phpfreaks.com/topic/193676-checkbox-delete-row-command/#findComment-1019459 Share on other sites More sharing options...
Gilly79 Posted February 28, 2010 Author Share Posted February 28, 2010 I think I get what you are saying... However Im a bit slow So the var that I will be passing to the deletemessage action page, do I need to address that any differently? Or do I put the var in the deletemessage page '$delete' = $GET['MessageNo']; Really sorry - its just the penny isn't dropping! Link to comment https://forums.phpfreaks.com/topic/193676-checkbox-delete-row-command/#findComment-1019463 Share on other sites More sharing options...
Spikerok Posted February 28, 2010 Share Posted February 28, 2010 Should be some thing like that <?php class myclass { private $user_id; public function __construct() { $this->user_id = $_SESSION['user_id']; if(isset($_POST['button'])) { myclass::deleteEntry(); } else { myclass::viewform(); } } private function viewform() { myclass::includes(); print "<form enctype=\"multipart/form-data\" method=\"post\" action=\"\">" $query = "SELECT * FROM MESSAGES WHERE MessageTo='". $this->user_id . "' "; $result = mysqli_query($dbc,$query) or die ("Error in query: $query. ".mysql_error()); if (mysqli_num_rows($result)>0) { print "<table border=1>\n"; print "<tr>\n"; print "<td>MessageNo</td>\n"; print "<td>MessageFrom</td>\n"; print "<td>MessageTo</td>\n"; print "<td>Subject</td>\n"; print "<td>Message</td>\n"; print "<td>Reply</td>\n"; print "<td>Delete</td>\n"; print "</tr>\n"; while ($row = @ mysqli_fetch_array($result)) { print "<tr>\n"; print "<td> . $row[\'MessageNo\'] . </td> \n"; print "<td> . $row[\'MessageFrom\'] . </td>\n"; print "<td> . $row[\'MessageTo\'] . </td>\n"; print "<td> . $row[\'Subject\'] . </td>\n"; print "<td> . $row[\'Message\'] . </td> \n"; print "<td><input type=\"checkbox\" name=\"reply[]\" value=\" . $row['MessageNo'] . \" /></td>\n"; print "<td><input type=\"checkbox\" name=\"delete[]\" value=\" . $row['MessageNo']. \" />\n"; print "</tr> \n"; } print "</table>"; print "<label>\n"; print "<p>\n"; print "<input type=\"submit\" name=\"button\" id=\"button\" value=\"Submit\" />\n"; print "</label></p>\n"; print "</form>\n"; } else { print "No rows found!"; } } private function deleteEntry() { myclass::includes(); $delete = $_POST['delete']; $max = count($delete); for($i = 0; $i < $max; $i++) { $query = "DELETE FROM MESSAGES WHERE MessageNo = '" . $delete[$i] . "'"; $result = mysqli_query($dbc,$query); } if(mysqli_affected_rows($dbc) > 0) { header("Location: {$_SERVER['HTTP_REFERER']}"); } else { echo "Error in query: $query" . mysqli_error(); mysqli_close($dbc); exit; } } private function includes() { require_once('startsession.php'); require_once('connectvars.php'); } } new myclass(); ?> Link to comment https://forums.phpfreaks.com/topic/193676-checkbox-delete-row-command/#findComment-1019464 Share on other sites More sharing options...
Dennis1986 Posted February 28, 2010 Share Posted February 28, 2010 First of all you should make sure you're using the correct form method (GET vs POST), and since I don't know which one you're using I'll simply use $_REQUEST in this example. Setup your form as you already have: <input type="checkbox" name="delete[]" value="..." /> <input type="checkbox" name="delete[]" value="..." /> <input type="checkbox" name="delete[]" value="..." /> <input type="checkbox" name="delete[]" value="..." /> PHP part you need: $array = implode(',', $_REQUEST["delete"]); $sql = "DELETE FROM messages WHERE MessageNo IN ($array)"; Link to comment https://forums.phpfreaks.com/topic/193676-checkbox-delete-row-command/#findComment-1019478 Share on other sites More sharing options...
Gilly79 Posted February 28, 2010 Author Share Posted February 28, 2010 hi dennis - Ive included the : $array = implode(',', $_REQUEST["delete"]); $sql = "DELETE FROM messages WHERE MessageNo IN ($array)"; within my deletemessage page HOWEVER i have amended it to a POST as that is what my form is... But im now getting Error in query: DELETE FROM messages WHERE MessageNo IN () I musn't be getting the vars through... Ive attatched the full messagesv2 page incase ive got something wrong - it could be around the vars within my table?? Really sorry - ive been tearing my hair out nearly all week with this one! [attachment deleted by admin] Link to comment https://forums.phpfreaks.com/topic/193676-checkbox-delete-row-command/#findComment-1019496 Share on other sites More sharing options...
Spikerok Posted February 28, 2010 Share Posted February 28, 2010 check print_r($array); before $sql Link to comment https://forums.phpfreaks.com/topic/193676-checkbox-delete-row-command/#findComment-1019497 Share on other sites More sharing options...
Gilly79 Posted February 28, 2010 Author Share Posted February 28, 2010 yeah nothing is getting through - I think it must be the naming convention in the form?? Link to comment https://forums.phpfreaks.com/topic/193676-checkbox-delete-row-command/#findComment-1019506 Share on other sites More sharing options...
jl5501 Posted February 28, 2010 Share Posted February 28, 2010 can you show us what that print_r($array) shows you. And what a print_r($_REQUEST) shows you Link to comment https://forums.phpfreaks.com/topic/193676-checkbox-delete-row-command/#findComment-1019508 Share on other sites More sharing options...
Spikerok Posted February 28, 2010 Share Posted February 28, 2010 well you seem to use post method in script, try print_r($_POST['delete']); Link to comment https://forums.phpfreaks.com/topic/193676-checkbox-delete-row-command/#findComment-1019511 Share on other sites More sharing options...
Gilly79 Posted February 28, 2010 Author Share Posted February 28, 2010 Array ( [delete] => Array ( [0] => 8 ) [button] => Submit ) Error in query: DELETE FROM messages WHERE MessageNo IN () thats the message... Ive changed it to a post as that what my form is too $array = implode(',', $POST["delete"]); print_r($array); print_r($_POST); Link to comment https://forums.phpfreaks.com/topic/193676-checkbox-delete-row-command/#findComment-1019512 Share on other sites More sharing options...
Spikerok Posted February 28, 2010 Share Posted February 28, 2010 check my other code above, it should work. here is example on what your script should do <?php class myclass { public function __construct() { if(isset($_POST['submit'])) { myclass::result($_POST['editor']); } myclass::show(); } private function show() { print "Which text editor do you use?<br /> \n"; print "<form method=\"post\" action=\"\"> \n"; print "<input type=\"checkbox\" name=\"editor[]\" value=\"Notepad\" /> \n"; print "Notepad<br /> \n"; print "<input type=\"checkbox\" name=\"editor[]\" value=\"Scite\" /> \n"; print "Scite<br /> \n"; print "<input type=\"checkbox\" name=\"editor[]\" value=\"Crimson Editor\" /> \n"; print "Crimson Editor<br /> \n"; print "<input type=\"checkbox\" name=\"editor[]\" value=\"Dreamweaver\" /> \n"; print "Dreamweaver<br /> \n"; print "<input type=\"checkbox\" name=\"editor[]\" value=\"vim\" /> \n"; print " vim<br /> \n"; print "<input type=\"submit\" name=\"submit\" id=\"submit\" value=\"submit\" /> \n"; print "</form>\n"; } private function result($editor) { $max = count($editor); for($i = 0; $i < $max; $i++) { print $editor[$i]; print "<br>\n"; } } } new myclass(); ?> I get $_POST['editor'] and then counting them and then using loop output all variables that array editor contains Link to comment https://forums.phpfreaks.com/topic/193676-checkbox-delete-row-command/#findComment-1019517 Share on other sites More sharing options...
Gilly79 Posted February 28, 2010 Author Share Posted February 28, 2010 i see what you have done with the Class etc but it didn't work for me - the page didn't even load with your previous example - im just checking a couple of bits though that i may need to include that are specific to the website Link to comment https://forums.phpfreaks.com/topic/193676-checkbox-delete-row-command/#findComment-1019524 Share on other sites More sharing options...
Gilly79 Posted February 28, 2010 Author Share Posted February 28, 2010 hi just thinking about it - is it my sql command $query = "DELETE FROM messages WHERE MessageNo IN ($array)"; as the array is working... the error message shows the message number [delete] => Array ( [0] => 7 [1] => 8 ) (i selected message 7 & 8 to be deleted...) shouldn't the sql have an equals in? Link to comment https://forums.phpfreaks.com/topic/193676-checkbox-delete-row-command/#findComment-1019537 Share on other sites More sharing options...
Dennis1986 Posted February 28, 2010 Share Posted February 28, 2010 You wrote: $array = implode(',', $POST["delete"]); Which is wrong. It should be: $array = implode(',', $_POST["delete"]); Link to comment https://forums.phpfreaks.com/topic/193676-checkbox-delete-row-command/#findComment-1019540 Share on other sites More sharing options...
Gilly79 Posted February 28, 2010 Author Share Posted February 28, 2010 legend!!! its working!! Link to comment https://forums.phpfreaks.com/topic/193676-checkbox-delete-row-command/#findComment-1019544 Share on other sites More sharing options...
Dennis1986 Posted February 28, 2010 Share Posted February 28, 2010 I never had a doubt about that Link to comment https://forums.phpfreaks.com/topic/193676-checkbox-delete-row-command/#findComment-1019559 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.