wesley220 Posted December 13, 2006 Share Posted December 13, 2006 Hi everyoneI'm fairly new to php and having some difficulties figuring out how to delete checkboxes from Mysql. Here's my code below can some one help please.[code]<?php include("config.php"); include("contentdb.php"); $id= $_POST["id"]; $question= $_POST["question"]; $opt1= $_POST["opt1"]; $opt2= $_POST["opt2"]; $opt3= $_POST["opt3"]; $answer= $_POST["answer"]; $Query="INSERT into qtable (id,question,opt1,opt2,opt3,answer) values ('$id','$question', '$opt1' , '$opt2', '$opt3' , '$answer')"; $result = mysql_query($Query) ; if (!$result) { print ("Your information has been passed into current database!<BR>\n"); } else { print ("The query could not be executed for inserting your data!<BR>"); } $query = "SELECT * FROM qtable ORDER BY id"; $result = mysql_query($query) or die ("Couldn't execute query for collecting your data."); /* Display results in a table */ print ("<TABLE BORDER=1 WIDTH=\"90%\" CELLSPACING=2 CELLPADDING=2 ALIGN=CENTER>\n"); print ("<TR ALIGN=CENTER onmouseover=blue>\n"); print ("<TD ALIGN=CENTER ><b><font color=red>No</font></TD>\n"); print ("<TD ALIGN=CENTER ><b><font color=red>Question</font></TD>\n"); print ("<TD ALIGN=CENTER ><b><font color=red>Option 1</font></TD>\n"); print ("<TD ALIGN=CENTER ><b><font color=red>Option 2</font></TD>\n"); print ("<TD ALIGN=CENTER ><b><font color=red>Option 3</font></TD>\n"); print ("<TD ALIGN=CENTER ><b><font color=red>Answer</font></TD>\n"); print ("<TD ALIGN=CENTER ><font color=blue size=2>Del/Edit</font></TD>\n"); print ("</TR>\n"); if ($result = mysql_query($query)) { // see if any rows were returned if (mysql_num_rows($result) > 0) { while ($row = mysql_fetch_array($result)) { extract($row); print ("<TR ALIGN=CENTER >\n"); print( "<TD ALIGN=CENTER >$row[id]</TD>\n"); print ("<TD ALIGN=CENTER >$row[question]</TD>\n"); print ("<TD ALIGN=CENTER >$row[opt1]</TD>\n"); print ("<TD ALIGN=CENTER >$row[opt2]</TD>\n"); print ("<TD ALIGN=CENTER >$row[opt3]</TD>\n"); print ("<TD ALIGN=CENTER >$row[answer]</TD>\n"); print ("<TD ALIGN=CENTER ><input type=checkbox name=delete value=\"$id\"></font></TD>\n"); print ("</tr>"); } mysql_free_result($result); } else { echo "Error in query: $query. ".mysql_error(); // print error message } } print ("</TABLE>\n"); echo "<input type=submit value=Delete > "; $id= $_POST["id"]; if (isset($Delete)) { $toDelete = implode(', ', $_POST['id']); $query = "DELETE FROM `qtable` WHERE ID IN ($toDelete)"; if(mysql_query($query)) { echo 'Hooray, the filthy rows were deleted'; } else echo 'Bad luck schmuck<br>' . mysql_error(); } // close connection mysql_close($db); ?> [/code] Quote Link to comment https://forums.phpfreaks.com/topic/30450-solved-how-to-delete-checkboxes/ Share on other sites More sharing options...
simcoweb Posted December 13, 2006 Share Posted December 13, 2006 I'm not sure if I understand the question. You can't insert a checkbox into a MySQL table. Only the value of the checkbox. Can you clarify please? Quote Link to comment https://forums.phpfreaks.com/topic/30450-solved-how-to-delete-checkboxes/#findComment-140257 Share on other sites More sharing options...
OOP Posted December 13, 2006 Share Posted December 13, 2006 I agree with simcoweb...could you please clarify so that we can help you Quote Link to comment https://forums.phpfreaks.com/topic/30450-solved-how-to-delete-checkboxes/#findComment-140282 Share on other sites More sharing options...
wesley220 Posted December 13, 2006 Author Share Posted December 13, 2006 I'm really sorry guys I didn't make much sense there, the problem is checkboxes are showing but when I press [b]Delete [/b] button (after checking the checkboxes) nothing happens. I'm trying all sorts of code from net nothing seems to work. Quote Link to comment https://forums.phpfreaks.com/topic/30450-solved-how-to-delete-checkboxes/#findComment-140353 Share on other sites More sharing options...
Cagecrawler Posted December 13, 2006 Share Posted December 13, 2006 I've never seen 'IN' used in this query:[code]$query = "DELETE FROM `qtable` WHERE ID IN ($toDelete)"; [/code]Try:[code]$query = "DELETE FROM qtable WHERE id = '$toDelete'";[/code] Quote Link to comment https://forums.phpfreaks.com/topic/30450-solved-how-to-delete-checkboxes/#findComment-140356 Share on other sites More sharing options...
simcoweb Posted December 13, 2006 Share Posted December 13, 2006 You might try changing this line in your WHILE loop:[code]<?phpprint ("<TD ALIGN=CENTER ><input type=checkbox name=delete value=\"$id\"></font></TD>\n");?>[/code]To this:[code]<?phpprint ("<TD ALIGN=CENTER ><input type='checkbox' name='delete' value='" . $row['id'] . "'></font></TD>\n");?>[/code]That should populate your checkbox with the right value. Then your delete query should point to that in the WHERE clause. Quote Link to comment https://forums.phpfreaks.com/topic/30450-solved-how-to-delete-checkboxes/#findComment-140407 Share on other sites More sharing options...
wesley220 Posted December 13, 2006 Author Share Posted December 13, 2006 I really appreciate your patients’ fells but unfortunately I’m not having much luck. There are no errors it just not deleting anything. Quote Link to comment https://forums.phpfreaks.com/topic/30450-solved-how-to-delete-checkboxes/#findComment-140451 Share on other sites More sharing options...
simcoweb Posted December 13, 2006 Share Posted December 13, 2006 That's probably because you don't have the checkbox and query results inside a <form></form> tag. :) Quote Link to comment https://forums.phpfreaks.com/topic/30450-solved-how-to-delete-checkboxes/#findComment-140456 Share on other sites More sharing options...
wesley220 Posted December 13, 2006 Author Share Posted December 13, 2006 Sorry mate still confused ??? Quote Link to comment https://forums.phpfreaks.com/topic/30450-solved-how-to-delete-checkboxes/#findComment-140537 Share on other sites More sharing options...
simcoweb Posted December 13, 2006 Share Posted December 13, 2006 Well, a checkbox is a form element. It can't do anything on its own. In other words, the checkbox and submit button both need to be part of a form. Right now they aren't. Therefore the button doesn't do anything. This section here:[code]<?php /* Display results in a table */ print ("<TABLE BORDER=1 WIDTH=\"90%\" CELLSPACING=2 CELLPADDING=2 ALIGN=CENTER>\n"); print ("<TR ALIGN=CENTER onmouseover=blue>\n"); print ("<TD ALIGN=CENTER ><b><font color=red>No</font></TD>\n"); print ("<TD ALIGN=CENTER ><b><font color=red>Question</font></TD>\n"); print ("<TD ALIGN=CENTER ><b><font color=red>Option 1</font></TD>\n"); print ("<TD ALIGN=CENTER ><b><font color=red>Option 2</font></TD>\n"); print ("<TD ALIGN=CENTER ><b><font color=red>Option 3</font></TD>\n"); print ("<TD ALIGN=CENTER ><b><font color=red>Answer</font></TD>\n"); print ("<TD ALIGN=CENTER ><font color=blue size=2>Del/Edit</font></TD>\n"); print ("</TR>\n"); if ($result = mysql_query($query)) { // see if any rows were returned if (mysql_num_rows($result) > 0) { while ($row = mysql_fetch_array($result)) { extract($row); print ("<TR ALIGN=CENTER >\n"); print( "<TD ALIGN=CENTER >$row[id]</TD>\n"); print ("<TD ALIGN=CENTER >$row[question]</TD>\n"); print ("<TD ALIGN=CENTER >$row[opt1]</TD>\n"); print ("<TD ALIGN=CENTER >$row[opt2]</TD>\n"); print ("<TD ALIGN=CENTER >$row[opt3]</TD>\n"); print ("<TD ALIGN=CENTER >$row[answer]</TD>\n"); print ("<TD ALIGN=CENTER ><input type=checkbox name=delete value=\"$id\"></font></TD>\n"); print ("</tr>"); } mysql_free_result($result); } else { echo "Error in query: $query. ".mysql_error(); // print error message } } print ("</TABLE>\n"); echo "<input type=submit value=Delete > "; ?>[/code]needs to be inside a form tag. So, you'd echo the form tags ahead and at the end:[code]<?phpecho "<form action='youractioninfohere.php' method='post'>"; /* Display results in a table */ print ("<TABLE BORDER=1 WIDTH=\"90%\" CELLSPACING=2 CELLPADDING=2 ALIGN=CENTER>\n"); print ("<TR ALIGN=CENTER onmouseover=blue>\n"); print ("<TD ALIGN=CENTER ><b><font color=red>No</font></TD>\n"); print ("<TD ALIGN=CENTER ><b><font color=red>Question</font></TD>\n"); print ("<TD ALIGN=CENTER ><b><font color=red>Option 1</font></TD>\n"); print ("<TD ALIGN=CENTER ><b><font color=red>Option 2</font></TD>\n"); print ("<TD ALIGN=CENTER ><b><font color=red>Option 3</font></TD>\n"); print ("<TD ALIGN=CENTER ><b><font color=red>Answer</font></TD>\n"); print ("<TD ALIGN=CENTER ><font color=blue size=2>Del/Edit</font></TD>\n"); print ("</TR>\n"); if ($result = mysql_query($query)) { // see if any rows were returned if (mysql_num_rows($result) > 0) { while ($row = mysql_fetch_array($result)) { extract($row); print ("<TR ALIGN=CENTER >\n"); print( "<TD ALIGN=CENTER >$row[id]</TD>\n"); print ("<TD ALIGN=CENTER >$row[question]</TD>\n"); print ("<TD ALIGN=CENTER >$row[opt1]</TD>\n"); print ("<TD ALIGN=CENTER >$row[opt2]</TD>\n"); print ("<TD ALIGN=CENTER >$row[opt3]</TD>\n"); print ("<TD ALIGN=CENTER >$row[answer]</TD>\n"); print ("<TD ALIGN=CENTER ><input type=checkbox name=delete value=\"$id\"></font></TD>\n"); print ("</tr>"); } mysql_free_result($result); } else { echo "Error in query: $query. ".mysql_error(); // print error message } } print ("</TABLE>\n"); echo "<input type=submit value=Delete > "; echo "</form><br>\n";[/code]By doing this the button will work. Note however that you need to have an 'action' statement that tells the form what to do. Quote Link to comment https://forums.phpfreaks.com/topic/30450-solved-how-to-delete-checkboxes/#findComment-140546 Share on other sites More sharing options...
wesley220 Posted December 15, 2006 Author Share Posted December 15, 2006 You were very right simcoweb I did need the form + my delete code wasn’t quite right, thanks to everyone who tried to help. Here’s my final code what worked for me.[quote]<?phpinclude("config.php");include("contentdb.php");if($_POST['delete']) {echo "<form action=quiz.php method=POST>"; foreach($_POST as $id) { // This will loop through the checked checkboxesmysql_query("DELETE FROM qtable WHERE id='$id'"); // This deletes the record from the database }}$errorString = ""; $id= $_POST["id"];$question= $_POST["question"];if (empty($question)) $errorString =$errorString."<br>Question Field cannot be blank."; $opt1= $_POST["opt1"];if (empty($opt1)) $errorString =$errorString."<br>Option 1 Field cannot be blank."; $opt2= $_POST["opt2"];if (empty($opt2)) $errorString =$errorString."<br>Option 2 Field cannot be blank."; $opt3= $_POST["opt3"];if (empty($opt3)) $errorString =$errorString."<br>Option 3 Field cannot be blank."; $answer= $_POST["answer"];if (empty($answer)) $errorString =$errorString."<br>Answer Field cannot be blank."; if (!empty($errorString)) { echo "There were some errors :<br>". $errorString; } else //Otherwise, insert the data into database { $Query="INSERT into $table (id,question,opt1,opt2,opt3,answer)values ('$id','$question', '$opt1' , '$opt2', '$opt3' , '$answer')";$result = mysql_query($Query) ; if (!$result) { print ("Your information has been passed into current database!<BR>\n"); } else { print ("The query could not be executed for inserting your data!<BR>"); }} $query = "SELECT * FROM qtable ORDER BY id"; $result = mysql_query($query) or die ("Couldn't execute query for collecting your data.");?><form name="action" id="action" method="post" action="<?=$_SERVER['PHP_SELF']?>"><? /* Display results in a table */print ("<TABLE BORDER=1 WIDTH=\"90%\" CELLSPACING=2 CELLPADDING=2 ALIGN=CENTER>\n");print ("<TR ALIGN=CENTER onmouseover=blue>\n");print ("<TD ALIGN=CENTER ><b><font color=red>No</font></TD>\n");print ("<TD ALIGN=CENTER ><b><font color=red>Question</font></TD>\n");print ("<TD ALIGN=CENTER ><b><font color=red>Option 1</font></TD>\n");print ("<TD ALIGN=CENTER ><b><font color=red>Option 2</font></TD>\n");print ("<TD ALIGN=CENTER ><b><font color=red>Option 3</font></TD>\n");print ("<TD ALIGN=CENTER ><b><font color=red>Answer</font></TD>\n");print ("<TD ALIGN=CENTER ><font color=blue size=2>Del/Edit</font></TD>\n");print ("</TR>\n");if ($result = mysql_query($query)) { // see if any rows were returned if (mysql_num_rows($result) > 0) { //while($row = mysql_fetch_assoc($result)) { while ($row = mysql_fetch_array($result)) { extract($row);print ("<TR ALIGN=CENTER >\n");print( "<TD ALIGN=CENTER >$row[id]</TD>\n");print ("<TD ALIGN=CENTER >$row[question]</TD>\n");print ("<TD ALIGN=CENTER >$row[opt1]</TD>\n");print ("<TD ALIGN=CENTER >$row[opt2]</TD>\n");print ("<TD ALIGN=CENTER >$row[opt3]</TD>\n");print ("<TD ALIGN=CENTER >$row[answer]</TD>\n");//echo "<td><font size=2><a href=delete.php?id=".$row[id].">Del/ // <a href=edit.php?id=".$row[id].">Edit</a></font></td>"; ?><TD ALIGN=CENTER ><input type="checkbox" name="<?=$row[id]?>" id="<?=$row[id]?>" value="<?=$row[id]?>"/></TD><?phpprint ("</tr>"); } mysql_free_result($result); } else { echo "Error in query: $query. ".mysql_error(); // print error message }}print ("</TABLE>\n"); ?><center><input type="submit" name="delete" id="delete" value="Delete Selected" onClick='alert("Are you sure ?")'/></center><?echo "</form><br>\n";echo "<p><a href='quiz.html'>Would you like to create another question</a>";// close connection mysql_close($db); ?>[/quote] Quote Link to comment https://forums.phpfreaks.com/topic/30450-solved-how-to-delete-checkboxes/#findComment-141596 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.