Marcos01 Posted September 17, 2008 Share Posted September 17, 2008 Hi all, I want to select a record from a list. I use a checkbox for that. I am having trouble deleting the record. What am I doing wrong? Here is the code: $result = mysql_query("SELECT id, datum, cursus, plaats FROM trainingsdata ") or die(mysql_error()); echo '<form method="post" action="'.$_SERVER['PHP_SELF'].'">'; echo '<table cellpadding="5" cellspacing="5"><tr>'; while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) { echo '<td><label><input type="checkbox" name="checkbox" id="checkbox" value="'.$row['id'].'"/></label></td>'; echo "<td>".$row['id']."</td><td>".$row['datum']."</td><td>".$row['cursus']."</td><td> ".$row['plaats']."</td><tr>"; } echo '</table><label>'; echo '<input type="submit" name="doaction" id='.$row['id'].' value="delete" />'; echo '</label></form>'; if ($_POST['doaction']=='delete') { $sql = "DELETE FROM trainingsdata WHERE id=".$row['id']."" ; echo 'data deleted'; } Quote Link to comment Share on other sites More sharing options...
ratcateme Posted September 17, 2008 Share Posted September 17, 2008 try this at the bottom if ($_POST['doaction']=='delete') { $sql = "DELETE FROM trainingsdata WHERE id=".$_POST['checkbox']."" ; echo 'data deleted'; } Quote Link to comment Share on other sites More sharing options...
Marcos01 Posted September 17, 2008 Author Share Posted September 17, 2008 Hi ratcateme, Thanks for your help. Unfortunately nothing happens when I try that. Any ideas? Quote Link to comment Share on other sites More sharing options...
budimir Posted September 17, 2008 Share Posted September 17, 2008 Try this: if ($_POST['doaction']=='delete') { $sql = "DELETE FROM trainingsdata WHERE id = ' ".$_POST['checkbox']." ' " ; echo 'data deleted'; } You need to put single quotations around variable so id could properly parsed. In first query it wasn't parsed properly especially if it's a number. I hope you can see the difference (don't just copy/paste the code, you need to take out the spaces!!!) Quote Link to comment Share on other sites More sharing options...
ranjuvs Posted September 17, 2008 Share Posted September 17, 2008 Two things I would like to tell First: As you have more than many records instead of line <input type="checkbox" name="checkbox" id="checkbox" value="'.$row['id'].'"/> use <input type="checkbox" name="checkbox[]" id="checkbox" value="'.$row['id'].'"/> Second: On posting the checkbox values will be available as an array. Check whether which all checkbox you have checked in by looping through the array you can access the check box value using index like $_POST[checkbox][0] let me know whether this works for you Quote Link to comment Share on other sites More sharing options...
Marcos01 Posted September 17, 2008 Author Share Posted September 17, 2008 Thanks budimir and ranjuvs for your help. Sorry to say but still noting happens after I check a box and hit delete. Here the code so far: $result = mysql_query("SELECT id, datum, cursus, plaats FROM trainingsdata ") or die(mysql_error()); echo '<form method="post" action="'.$_SERVER['PHP_SELF'].'">'; echo '<table cellpadding="5" cellspacing="5"><tr>'; while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) { echo '<td><label><input type="checkbox" name="checkbox[]" id="checkbox" value="'.$row['id'].'"/></label></td>'; echo "<td>".$row['id']."</td><td>".$row['datum']."</td><td>".$row['cursus']."</td><td> ".$row['plaats']."</td><tr>"; } echo '</table><label>'; echo '<input type="submit" name="doaction" id='.$row['id'].' value="delete" />'; echo '</label></form>'; if ($_POST['doaction']=='delete') { $sql = "DELETE FROM trainingsdata WHERE id='".$_POST['checkbox']."'" ; echo 'data deleted'; } Quote Link to comment Share on other sites More sharing options...
ranjuvs Posted September 17, 2008 Share Posted September 17, 2008 In my last post I mentioned that the value of $_POST['checkbox'] this will be array. You need to use index to get the value. Try to loop through the array and find the value. The array variable which have 1 has its value is the one you checked. $_POST['checkbox'][index] where index - 0,1,2.... Quote Link to comment Share on other sites More sharing options...
Marcos01 Posted September 17, 2008 Author Share Posted September 17, 2008 I just started with PHP so I don't know how to do that. Quote Link to comment Share on other sites More sharing options...
Marcos01 Posted September 17, 2008 Author Share Posted September 17, 2008 Anyone? Quote Link to comment Share on other sites More sharing options...
ratcateme Posted September 17, 2008 Share Posted September 17, 2008 $_POST['checkbox'] becomes an array so you need to loop through it like this if ($_POST['doaction'] == 'delete') { foreach ($_POST['checkbox'] as $id) { $sql = "DELETE FROM trainingsdata WHERE id='{$id}'"; mysql_query($sql) or die(mysql_error()); echo "{$id} data deleted"; } } Scott. Quote Link to comment Share on other sites More sharing options...
Marcos01 Posted September 18, 2008 Author Share Posted September 18, 2008 Thanks ratcateme, I tried that but nothing happens after I select and hit delete. Code so far: $result = mysql_query("SELECT id, datum, cursus, plaats FROM trainingsdata ") or die(mysql_error()); echo '<form method="post" action="'.$_SERVER['PHP_SELF'].'">'; echo '<table cellpadding="5" cellspacing="5"><tr>'; while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) { echo '<td><label><input type="checkbox" name="checkbox" id="checkbox" value="'.$row['id'].'"/></label></td>'; echo "<td>".$row['id']."</td><td>".$row['datum']."</td><td>".$row['cursus']."</td><td> ".$row['plaats']."</td><tr>"; } echo '</table><label>'; echo '<input type="submit" name="doaction" id='.$row['id'].' value="delete" />'; echo '</label></form>'; if ($_POST['doaction'] == 'delete') { foreach ($_POST['checkbox'] as $id) { $sql = "DELETE FROM trainingsdata WHERE id='{$id}'"; mysql_query($sql) or die(mysql_error()); echo "{$id} data deleted"; } } Quote Link to comment Share on other sites More sharing options...
ratcateme Posted September 18, 2008 Share Posted September 18, 2008 name="checkbox" needs to be name="checkbox[]" so it will be a array Scott. Quote Link to comment Share on other sites More sharing options...
Marcos01 Posted September 18, 2008 Author Share Posted September 18, 2008 I tried that. Still no result. Quote Link to comment Share on other sites More sharing options...
ratcateme Posted September 18, 2008 Share Posted September 18, 2008 add print_r($_POST) to the top somewhere and post the output Scott. Quote Link to comment Share on other sites More sharing options...
Marcos01 Posted September 18, 2008 Author Share Posted September 18, 2008 This is what I get Array ( [checkbox] => Array ( [0] => 1 ) [doaction] => Query send ) And every time a different here: Array ( [checkbox] => Array ( [0] => 1 ) [doaction] => Query send ) Quote Link to comment Share on other sites More sharing options...
Marcos01 Posted September 18, 2008 Author Share Posted September 18, 2008 Anyone? Quote Link to comment Share on other sites More sharing options...
ratcateme Posted September 18, 2008 Share Posted September 18, 2008 if ($_POST['doaction'] == 'delete') { needs to be if ($_POST['doaction'] == 'Query send') { Scott. Quote Link to comment Share on other sites More sharing options...
Marcos01 Posted September 19, 2008 Author Share Posted September 19, 2008 Thanks ratcateme, Now it works! But now the button text says: Query send Is there a way to change that into delete? Quote Link to comment Share on other sites More sharing options...
Marcos01 Posted September 19, 2008 Author Share Posted September 19, 2008 I have fixed the button. Quote Link to comment Share on other sites More sharing options...
ranjuvs Posted September 19, 2008 Share Posted September 19, 2008 Whats the value you have given for doaction submit button. Is it "delete" or "Query Send"? Quote Link to comment Share on other sites More sharing options...
ranjuvs Posted September 19, 2008 Share Posted September 19, 2008 Fixed it. Good. Sorry for the above post. Quote Link to comment Share on other sites More sharing options...
Marcos01 Posted September 19, 2008 Author Share Posted September 19, 2008 ratcateme, ranjuvs and budimir, Thanks for your help! Here the final code: $result = mysql_query("SELECT id, date, course, place FROM data ") or die(mysql_error()); echo '<form method="post" action="'.$_SERVER['PHP_SELF'].'">'; echo '<table cellpadding="5" cellspacing="5"><tr>'; while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) { echo '<td><label><input type="checkbox" name="checkbox[]" id="checkbox" value="'.$row['id'].'"/></label></td>'; echo "<td>".$row['date']."</td><td>".$row['course']."</td><td> ".$row['place']."</td><tr>"; } echo '</table><label>'; echo '<input type="submit" name="doaction" id={"delete"'.$row['id'].'} value="delete" /></label></form>'; if ($_POST['doaction'] == 'delete') { foreach ($_POST['checkbox'] as $id) { $sql = "DELETE FROM data WHERE id='{$id}'"; mysql_query($sql) or die(mysql_error()); echo "date gewist"; echo '<meta http-equiv="refresh" content="2;URL='.$_SERVER['PHP_SELF'].'">'; } } Quote Link to comment 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.