aian04 Posted October 8, 2007 Share Posted October 8, 2007 ok now i change my idea... i just put checkbox and two submit button... the button is either update or delete... hirs my code <?php $host="localhost"; $username="root"; $password="tmc"; $db_name="tgp"; $tbl_name="reservation"; $id=$_COOKIE['ID']; mysql_connect("$host", "$username", "$password")or die("cannot connect"); mysql_select_db("$db_name")or die("cannot select DB"); $sql="SELECT * FROM $tbl_name join packages on reservation.pac_id = packages.pac_id where Cid ='$id'"; $result=mysql_query($sql); $count=mysql_num_rows($result); ?> <html> <head><link href="interface design/css.css" rel="stylesheet" type="text/css"> </head> <body align="center"> <form action="http://localhost:6080/aian/update.php" method="post" name="form"> <table class="ret" border="0" cellpadding="10" cellspacing="3" background="interface design/logo/member.jpg"> <tr> <td colspan="7" align="center"><strong>Reservatio History</strong> </td> </tr> <tr><td> </td> <td align="center">Reservation ID</td> <td align="center"><strong>Check in Date</strong></td> <td align="center"><strong>No. of nights</strong></td> <td align="center"><strong>Room Number</strong></td> <td align="center"><strong>Specials And Packages</strong></td> <td align="center"><strong>Customer ID</strong></td> </tr> <?php while($rows=mysql_fetch_assoc($result)){ ?> <tr><td><input type="checkbox" name="check" id="check" value=" <? echo $row['Res_id'];?>"></td> <td><? echo $rows['Res_id'];?></td> <td><? echo $rows['check_in'];?></td> <td><? echo $rows['night_per_stay'];?></td> <td><? echo $rows['R_no'];?></td> <td><? echo $rows['pac_name'];?></td> <td><? echo $rows['Cid']; ?></td> </tr> <?php }?> </table> <pre> <input type="submit" class="groovybutton2" value="Update" name="update"> <input class="groovybutton2" type="submit" value="Delete" name="delete"> </pre> </form> </body> </html> <? @mysql_close();?> and this is my code to proces the above code <html> <head><link href="interface design/css.css" rel="stylesheet" type="text/css"> </head> <body class="ret"> <?php @mysql_connect ("localhost", "root", "tmc") or die (mysql_error()); @mysql_select_db("tgp")or die(mysql_error()); $edit=$_POST['update']; $delete=$_POST['delete']; $check=$_POST['check']; if(isset($edit)){ echo "$edit"; echo "$check"; } if(isset($delete)){ echo $check; echo $delete; } ?> </body> </html> ok im just trying to echo the value of the checkbox for testing the code.. but the value of checked checkbox doesnt show anything... is this a good idea? can u help me to solve this.. Quote Link to comment https://forums.phpfreaks.com/topic/72330-using-checkbox-for-updating-or-deleting/ Share on other sites More sharing options...
kenrbnsn Posted October 8, 2007 Share Posted October 8, 2007 Change the name of the "submit" button to be the same for each button, then you can check for the value: <input type="submit" class="groovybutton2" value="Update" name="submit_button"> <input class="groovybutton2" type="submit" value="Delete" name="submit_button"> <html> <head><link href="interface design/css.css" rel="stylesheet" type="text/css"> </head> <body class="ret"> <?php @mysql_connect ("localhost", "root", "tmc") or die (mysql_error()); @mysql_select_db("tgp")or die(mysql_error()); $check=$_POST['check']; if($_POST['submit_button'] == 'Update'){ echo "Update<br>"; echo $check; } if($_POST['submit_button'] == 'Delete'){ echo "Delete<br>"; echo $check; } ?> </body> </html> Ken Quote Link to comment https://forums.phpfreaks.com/topic/72330-using-checkbox-for-updating-or-deleting/#findComment-364715 Share on other sites More sharing options...
aian04 Posted October 8, 2007 Author Share Posted October 8, 2007 its failed to show the value of the checkbox.. Quote Link to comment https://forums.phpfreaks.com/topic/72330-using-checkbox-for-updating-or-deleting/#findComment-364726 Share on other sites More sharing options...
kenrbnsn Posted October 8, 2007 Share Posted October 8, 2007 Did you check the box. If the checkbox is not checked, then the value is not returned to PHP. Ken Quote Link to comment https://forums.phpfreaks.com/topic/72330-using-checkbox-for-updating-or-deleting/#findComment-364727 Share on other sites More sharing options...
aian04 Posted October 8, 2007 Author Share Posted October 8, 2007 yap... i checked it... but stil dont have any result Quote Link to comment https://forums.phpfreaks.com/topic/72330-using-checkbox-for-updating-or-deleting/#findComment-364741 Share on other sites More sharing options...
kenrbnsn Posted October 8, 2007 Share Posted October 8, 2007 You have multiple checkboxes all with the same name, so PHP is going to return the value of the last on defined. That's probably not what you want. You need to change your code so that the name of the checkbox is an array: <?php while($rows=mysql_fetch_assoc($result)){ ?> <tr><td><input type="checkbox" name="check[<? echo $row['Res_id'];?>]" id="check" value="1"></td> <td><? echo $rows['Res_id'];?></td> <td><? echo $rows['check_in'];?></td> <td><? echo $rows['night_per_stay'];?></td> <td><? echo $rows['R_no'];?></td> <td><? echo $rows['pac_name'];?></td> <td><? echo $rows['Cid']; ?></td> </tr> <?php }?> Then in your processing script: <?php if($_POST['submit_button'] == 'Update'){ echo "Update<br>"; echo '<pre>' . print_r($_POST['check'],true) . '</pre>'; } if($_POST['submit_button'] == 'Delete'){ echo "Delete<br>"; echo '<pre>' . print_r($_POST['check'],true) . '</pre>'; } ?> Ken Quote Link to comment https://forums.phpfreaks.com/topic/72330-using-checkbox-for-updating-or-deleting/#findComment-364751 Share on other sites More sharing options...
aian04 Posted October 8, 2007 Author Share Posted October 8, 2007 ok if i do dat... how can i query my table using the id?? for e.g updating... when i check a specific column then then i just gonna get the id from the checkbox to update my table... so how to get the id from my 1st code to second code.. and also to my 3rd code.. Quote Link to comment https://forums.phpfreaks.com/topic/72330-using-checkbox-for-updating-or-deleting/#findComment-364795 Share on other sites More sharing options...
aian04 Posted October 8, 2007 Author Share Posted October 8, 2007 ok.. nevermind... i gonna use this only for deleting... how i gona do dat.. use for loop? Quote Link to comment https://forums.phpfreaks.com/topic/72330-using-checkbox-for-updating-or-deleting/#findComment-364801 Share on other sites More sharing options...
The Little Guy Posted October 8, 2007 Share Posted October 8, 2007 make your inputs like so: <input type="checkbox" name="check[]" id="check" value="1"> This will make them all an array (Each value needs to be unique). then do this: <?php if(isset($_POST['btnDelete'])){ foreach($_POST['check'] as $value){ mysql_query("DELETE FROM tableName WHERE id = '$value'"); } }else{ foreach($_POST['check'] as $value){ mysql_query("UPDATE tableName SET value = 'Some Value' WHERE id = '$value'"); } } ?> My Example: http://phpsnips.com/snippet.php?id=11 Quote Link to comment https://forums.phpfreaks.com/topic/72330-using-checkbox-for-updating-or-deleting/#findComment-364803 Share on other sites More sharing options...
kenrbnsn Posted October 8, 2007 Share Posted October 8, 2007 When you use my method, each key of the returned array is set to the value of the record id to be deleted/updated. Just loop through the returned array: <?php if (isset($_POST['check'])) foreach($_POST['check'] as $rec_id => $dmy) { // // create the mysql_statement you need using $rec_id // } ?> Ken Quote Link to comment https://forums.phpfreaks.com/topic/72330-using-checkbox-for-updating-or-deleting/#findComment-364807 Share on other sites More sharing options...
aian04 Posted October 8, 2007 Author Share Posted October 8, 2007 <input type="checkbox" name="check[]" id="check" value="1"> bro how can u delete if u didnt get the res_id.. this code wont get the res_id coz it is not specified.. i think must put value=" <? echo $row['Res_id'];?>" so it will send the res_id to server and process it..?? Quote Link to comment https://forums.phpfreaks.com/topic/72330-using-checkbox-for-updating-or-deleting/#findComment-364817 Share on other sites More sharing options...
The Little Guy Posted October 8, 2007 Share Posted October 8, 2007 That was the HTML output, but here would be your php to create it: <input type="checkbox" name="check[]" id="check" value="<?php echo $row['Res_id'];?>"> Quote Link to comment https://forums.phpfreaks.com/topic/72330-using-checkbox-for-updating-or-deleting/#findComment-364822 Share on other sites More sharing options...
aian04 Posted October 8, 2007 Author Share Posted October 8, 2007 then how bout the server script?? shud i do the server scripting in the same php page or external php?? Quote Link to comment https://forums.phpfreaks.com/topic/72330-using-checkbox-for-updating-or-deleting/#findComment-364828 Share on other sites More sharing options...
aian04 Posted October 8, 2007 Author Share Posted October 8, 2007 wats wrong wid this.. i try to get the id and delete it [code<?php @mysql_connect ("localhost", "root", "tmc") or die (mysql_error()); @mysql_select_db("tgp")or die(mysql_error()); $ch=$_POST['check']; if($_POST['button'] == 'Delete'){ for($i=0;$i<$count;$i++){ $del_id = $ch[$i]; $sql = "DELETE FROM reservation WHERE Res_id='$del_id'"; $result = mysql_query($sql); } if($result){ echo "<script> window.location ='history.php'"; } } geeezz.. have u created deleting multiple rows using checkbox? Quote Link to comment https://forums.phpfreaks.com/topic/72330-using-checkbox-for-updating-or-deleting/#findComment-364833 Share on other sites More sharing options...
The Little Guy Posted October 8, 2007 Share Posted October 8, 2007 Try something like this: index.php <?php $sql="SELECT * FROM $tbl_name join packages on reservation.pac_id = packages.pac_id where Cid ='$id'"; $result=mysql_query($sql); echo '<form action="process.php" method="post">'; while($row = mysql_fetch_array($result)){ echo '<tr> <td><input type="checkbox" name="check[]" value="'.$row['Res_id'].'"></td> <td>'.$rows['check_in'].'</td> <td>'.$rows['night_per_stay'].'</td> <td>'.$rows['R_no'].'</td> <td>'.$rows['pac_name'].'</td> <td><'.$rows['Cid'].'</td> <tr>'; } echo '<tr><td><input type="sbumit" name="delete" value="DELETE ROWS"> <input type="sbumit" name="update" value="UPDATE ROWS"></td></tr>'; echo '</form>'; ?> process.php <?php if(isset($_POST['delete'])){ foreach($_POST['check'] as $value){ mysql_query("DELETE FROM tableName WHERE id = '$value'"); } }else{ foreach($_POST['check'] as $value){ mysql_query("UPDATE tableName SET value = 'Some Value' WHERE id = '$value'"); } } header("Location: index.php"); exit; ?> Quote Link to comment https://forums.phpfreaks.com/topic/72330-using-checkbox-for-updating-or-deleting/#findComment-364842 Share on other sites More sharing options...
Barand Posted October 9, 2007 Share Posted October 9, 2007 Once you have a check box for each item listed <input type="checkbox" name="check[]" value="'.$row['Res_id'].'"> Then you can delete all the selected items with a single query instead of a loop to delete them one by one: $items = join(',' , $_POST['check']); mysql_query ("DELETE FROM tablename WHERE id IN ($items)"); Quote Link to comment https://forums.phpfreaks.com/topic/72330-using-checkbox-for-updating-or-deleting/#findComment-365032 Share on other sites More sharing options...
The Little Guy Posted October 9, 2007 Share Posted October 9, 2007 Once you have a check box for each item listed <input type="checkbox" name="check[]" value="'.$row['Res_id'].'"> Then you can delete all the selected items with a single query instead of a loop to delete them one by one: $items = join(',' , $_POST['check']); mysql_query ("DELETE FROM tablename WHERE id IN ($items)"); Nice! I have always wanted to know how to do that, but nobody ever answers me that question. Quote Link to comment https://forums.phpfreaks.com/topic/72330-using-checkbox-for-updating-or-deleting/#findComment-365046 Share on other sites More sharing options...
Barand Posted October 9, 2007 Share Posted October 9, 2007 That will only work for numeric values. If you want to delete using string values $items = join("','" , $_POST['check']); mysql_query ("DELETE FROM tablename WHERE id IN ('$items')"); Quote Link to comment https://forums.phpfreaks.com/topic/72330-using-checkbox-for-updating-or-deleting/#findComment-365051 Share on other sites More sharing options...
aian04 Posted October 9, 2007 Author Share Posted October 9, 2007 $items = join(',' , $_POST['check']); $sql="DELETE FROM reservation WHERE Res_id IN ($items)"; if(isset($_POST['delete'])){ if(!mysql_query($sql)){?> <div style=" width: 450px; background:black;filter:alpha(opacity=75); -moz-opacity:.75;opacity:.75; margin-left: 0; margin-right:0;"><?php echo "Deleting Information Failed!"; echo $items; ?> </div><?php } else {?><script> window.location="history.php"</script><?} } @mysql_close(); i try to use ur code but its stil not workin... its still doesnt get the id on the checkbox Quote Link to comment https://forums.phpfreaks.com/topic/72330-using-checkbox-for-updating-or-deleting/#findComment-365514 Share on other sites More sharing options...
aian04 Posted October 10, 2007 Author Share Posted October 10, 2007 anyone hu knws how to do deleting multiple rows using checkbox?? dis is my last day of asking bout php... pls help.. Quote Link to comment https://forums.phpfreaks.com/topic/72330-using-checkbox-for-updating-or-deleting/#findComment-365981 Share on other sites More sharing options...
Barand Posted October 10, 2007 Share Posted October 10, 2007 You have been shown how to do it. All you need to do is implement it correctly. Quote Link to comment https://forums.phpfreaks.com/topic/72330-using-checkbox-for-updating-or-deleting/#findComment-366493 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.