Ken2k7 Posted December 24, 2007 Share Posted December 24, 2007 Say I have: <form method='post' name='whatever'> <input type='checkbox' name='1' /> Something 1 <input type='checkbox' name='2' /> Something 2 . . . <input type='submit' name='submit' value='submit' /> </form> Is there a way of using PHP to first check all the input and see if its checked and then if it is, remove that entry from the database? - Note that the input list is generated with PHP and I will handle the remove line. I just need to know how to check with PHP if the input value is checked. I was thinking of something like: <?php if ($_POST['submit']){ foreach ($p as $_POST){ // check if it is checked // if so, remove it (again, I will write this line so you don't have to) }} ?> Thanks in advance, Ken Quote Link to comment Share on other sites More sharing options...
Barand Posted December 24, 2007 Share Posted December 24, 2007 like this http://www.phpfreaks.com/forums/index.php/topic,173955.msg769731.html#msg769731 Quote Link to comment Share on other sites More sharing options...
Ken2k7 Posted December 24, 2007 Author Share Posted December 24, 2007 Thanks Barand. Just one question about that. How does this line work? mysql_query ("DELETE FROM tablename WHERE id IN ('$deleteList')"); Quote Link to comment Share on other sites More sharing options...
Barand Posted December 24, 2007 Share Posted December 24, 2007 The query looks like DELETE FROM tablename WHERE id IN ('1', '29', '67') which is an easier way of doing DELETE FROM tablename WHERE (id = '1') OR (id = '29') OR (id = '67') Quote Link to comment Share on other sites More sharing options...
Ken2k7 Posted December 24, 2007 Author Share Posted December 24, 2007 The query looks like DELETE FROM tablename WHERE id IN ('1', '29', '67') which is an easier way of doing DELETE FROM tablename WHERE (id = '1') OR (id = '29') OR (id = '67') AWESOME! Thank you. I did not know that. Thank you and happy holidays, Ken P.S.: I'm leaving this one open for now. I will close it once I get this working. Quote Link to comment Share on other sites More sharing options...
Ken2k7 Posted December 31, 2007 Author Share Posted December 31, 2007 Uh this doesn't work for me. Am I doing something wrong. Here's the code: <?php include_once "test_db.php"; $query = "SELECT number FROM numbers"; $result = mysql_query($query, $database->db_connect) or die(mysql_error()); echo "<form method='post'>"; while ($row = mysql_fetch_assoc($result)){ echo "<input type='checkbox' name='del[]' />".$row['number']."<br />"; } echo "<input type='submit' name='submit' value='submit' /></form>"; if (isset($_POST['submit'])){ $delete = join(",", $_POST['del']); mysql_query("DELETE FROM numbers WHERE number IN ('$delete')"); echo "success"; } ?> In case you're wondering about how the SQL table is set up, here: Table: numbers Column: number int( 8 ) That's it. Just testing to see if this works and it doesn't. It doesn't delete the rows I checked off. Please help. Thanks in advance, Ken Quote Link to comment Share on other sites More sharing options...
Barand Posted December 31, 2007 Share Posted December 31, 2007 You have to give a value of $row['number'] to the c/boxes echo "<input type='checkbox' name='del[]' value='{$row['number']}'/>".$row['number']."<br />"; Quote Link to comment Share on other sites More sharing options...
Ken2k7 Posted January 1, 2008 Author Share Posted January 1, 2008 Thanks Barand. Only problem now is that it only deletes the first checkbox, but I want it to delete multiple ones. Code as of now: <?php include_once "test_db.php"; $query = "SELECT number FROM numbers"; $result = mysql_query($query, $database->db_connect) or die(mysql_error()); echo "<form method='post'>"; while ($row = mysql_fetch_assoc($result)){ echo "<input type='checkbox' value='{$row['number']}' name='del[]' />".$row['number']."<br />"; } echo "<input type='submit' name='submit' value='submit' /></form>"; if (isset($_POST['submit'])){ $delete = join(",", $_POST['del']); mysql_query("DELETE FROM numbers WHERE number IN ('$delete')"); echo "success"; } ?> Please help. Thanks in advance, Ken Quote Link to comment Share on other sites More sharing options...
teng84 Posted January 1, 2008 Share Posted January 1, 2008 i think the code is fine but i also think you should remove the ' ' in your in clause so this line mysql_query("DELETE FROM numbers WHERE number IN ('$delete')"); should be mysql_query("DELETE FROM numbers WHERE number IN ($delete)"); Quote Link to comment Share on other sites More sharing options...
Ken2k7 Posted January 1, 2008 Author Share Posted January 1, 2008 Thanks a million teng84. Happy new year to ya and to all as well. 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.