Hi everyone, i have a problem in php. My Problem is that as follows: i have a checkbox and name checkbox1,checkbox2,checkbox3 etc which id dynamically generated in php. ? Hoe can i get this values from php and how can i delete multiple records using this concept in php. Any one can help me. Thanks, Rajeev
just give them an array name like input name=checkboxes[] type=checkbox and do a foreach() into the php side
Hi, This is the code <input type="checkbox" name="chk<?=$sn?>" id="chk<?=$sn?>" value="<?=$Detail->id?>" Here is: $sn is the serial number which is generated through code $Detail->id is the id of the particular checkbox. Please help me. Rajeev
If I correctly understand your explanation: $to_delete = array(); foreach ($_REQUEST as $k => $v) if (preg_match('/^chk\d+$/', $k)) $to_delete[] = intval($v); $to_delete_str = join(', ', array_unique($to_delete)); $sql = "delete from my_table where id in ("{$to_delete_str}"); Code (markup):
this seems some work but when we delete multiple records it only delete the first record not last one. please help me. Rajeev
Okay, simple. (Let me know if this is what you are looking for) First off, you should be simply creating your HTML checkbox inputs as follows: <input type="checkbox" name="checkbox[]" value="1" /> Code (markup): Now, for PHP code. If your submit button's name is submit something perhaps like this will work then: <?php if( TRUE == isset($_POST['submit']) ) { /* $check_box_ids now will contain an array of all check box values that have been checked. */ $check_box_ids = $_POST['checkbox']; /* Make sure that check_box_ids count is greater then 0, else we will run into problems when executing mysql_query */ if( 0 < count($check_box_ids) ) { /* Do your operation here .. Perhaps an SQL query */ $sql = "DELETE FROM `item_table` WHERE primary_key IN (" . implode(',', $check_box_ids) . ")"; $mysql_rid = mysql_query($sql); } } Code (markup): Now, does this help you? If not, let me know.
Mhmm. Okay, well then. Do you perhaps have a bit more detail/information about your code: What exactly is the meaning for the serial number? Is it a unique value continuously? Are the checkbox items for items all within the same Database table? It would be helpful to have a bit more of an explanation on what you are trying to do with the data that is posted.
This is the code <input type="checkbox" name="chk<?=$sn?>" id="chk<?=$sn?>" value="<?=$Detail->id?>" Here is: $sn is the serial numbe eg:1,2,3,4 etc (means: chk1,ch2,chk3,chk4) $Detail->id is the id of the particular checkbox.
$to_delete = array(); foreach ($_REQUEST as $k => $v) if (preg_match('/^chk\d+$/', $k)) $to_delete[] = intval($v); $to_delete_str = join(', ', array_unique($to_delete)); $sql = "delete from my_table where id in ("{$to_delete_str}"); This is the code send to me some. it works but when we delete the first and last record from selecting the checkbox it only deletes the first record not last one( Eg: when we celete checkbox1,checkbox2 it only delete the checkbox1 not the checkbox2) Rajeev
Okay. May I ask why you are creating checkbox1, checkbox2, checkbox3 and you can not just use an array of them? Do all the checkbox values contain a primary key or an identity value within a database? If so, are they all in the same table?
hi grunt, $to_delete = array(); foreach ($_REQUEST as $k => $v) if (preg_match('/^chk\d+$/', $k)) $to_delete[] = intval($v); $to_delete_str = join(', ', array_unique($to_delete)); $sql = "delete from my_table where id in ("{$to_delete_str}"); this code works. but you it deletes one record not lastone. actually we can not change the whole code. if it possible to only modify this code and works fine. Thanks, Rajeev
If it's not deleting all the checked items then whoever wrote that code (me) didn't understand what your form looks like. Maybe you need to provide a sample of the form's HTML, and also to explain which field you are using as your primary key in the database, and where that field appears in the form.
Hello There I come to know you about PHP Mysql-Deleting multiple records using checkbox. I have a url which can help you. Click Here : http://www.raiseitsolutions.com/forum/viewtopic.php?f=5&t=121 I hope it is enough for you. Thank You AWON