Guber-X Posted April 15, 2012 Share Posted April 15, 2012 okay, what i have is a page part of my ACP, and what i am trying to do is list all images from database, the list of images will have a "Checkbox" beside it. the checkbox is for setting where the image will be displayed. reason for the checkbox is cuz the images are only being displayed on 2 separate pages. basicly the whole list of images are in 1 form so when submit is activated its suppose to update the image list where checkboxs have been changed. hope i explained this clearly since im sick haha. but heres what i got for code <form method="post"> <input type="submit" name="submit" value="Submit" /><br /> <?php $result = mysql_query("SELECT * FROM items") or die("Query Failed: ".mysql_error()); while($row = mysql_fetch_array($result)){ list($id, $item_info, $item_img, $price, $sale, $location) = $row; if($location == 'home'){ $set_checked = 'checked="checked"'; }else{ $set_checked = ''; } if(isset($_POST['location'])){ foreach($_POST['location'] as $value){ $id = $_POST['id']; $insert = mysql_query("UPDATE items SET location='$value' WHERE id='$id'") or die('Insert Error: '.mysql_error()); } } echo '<img src="../images/items/'.$item_img.'" width="100" /> Home Page Slide Show: <input type="checkbox" id='.$id.' value="home" name="location[]" '.$set_checked.' /><br />'; } ?> <input type="hidden" name="hdnLine" value="<? echo $count ?>" /> <input type="submit" name="submit" value="Submit" /> </form> i also added a pic of how it looks like lol Quote Link to comment https://forums.phpfreaks.com/topic/260994-update-query-using-checkboxes-for-a-value-help/ Share on other sites More sharing options...
Drummin Posted April 16, 2012 Share Posted April 16, 2012 Without testing, I would say what you have looks close. Add the hidden id input line and move processing above query. <form method="post"> <input type="submit" name="submit" value="Submit" /><br /> <?php if(isset($_POST['location'])){ foreach($_POST['location'] as $value){ $id = $_POST['id']; $insert = mysql_query("UPDATE items SET location='$value' WHERE id='$id'") or die('Insert Error: '.mysql_error()); } } $result = mysql_query("SELECT * FROM items") or die("Query Failed: ".mysql_error()); while($row = mysql_fetch_array($result)){ list($id, $item_info, $item_img, $price, $sale, $location) = $row; if($location == 'home'){ $set_checked = 'checked="checked"'; }else{ $set_checked = ''; } echo '<img src="../images/items/'.$item_img.'" width="100" /> Home Page Slide Show: <input type="checkbox" id='.$id.' value="home" name="location[]" '.$set_checked.' /><input type="hidden" name="id" value='.$id.' /><br />'; } ?> <input type="hidden" name="hdnLine" value="<? echo $count ?>" /> <input type="submit" name="submit" value="Submit" /> </form> Quote Link to comment https://forums.phpfreaks.com/topic/260994-update-query-using-checkboxes-for-a-value-help/#findComment-1337681 Share on other sites More sharing options...
Guber-X Posted April 16, 2012 Author Share Posted April 16, 2012 yeah something is still not right. like its not sending the value into the DB. ill keep muckin around with it. im also trying a different method where each image has its own form but its not the way i want it and its weird haha. like when checked and submitted i have to reload the page just to have the results show haha Quote Link to comment https://forums.phpfreaks.com/topic/260994-update-query-using-checkboxes-for-a-value-help/#findComment-1337683 Share on other sites More sharing options...
Drummin Posted April 16, 2012 Share Posted April 16, 2012 Ya, sorry about that last one. What you need to do is not only change the selected items to "home" but also those not selected to say blank. I think you'd want to post all id's then if the "location" id is posted, update the value to "home". Otherwise, update the value to "empty". Something like this. Again not tested. <form method="post"> <input type="submit" name="submit" value="Submit" /><br /> <?php if(isset($_POST['submit'])){ foreach($_POST['id'] as $id){ $value = (isset($_POST['location'][$id]) && $_POST['location'][$id]=="home" ? 'home' : ''); $insert = mysql_query("UPDATE items SET location='$value' WHERE id='$id'") or die('Insert Error: '.mysql_error()); } } $result = mysql_query("SELECT * FROM items") or die("Query Failed: ".mysql_error()); while($row = mysql_fetch_array($result)){ list($id, $item_info, $item_img, $price, $sale, $location) = $row; if($location == 'home'){ $set_checked = 'checked="checked"'; }else{ $set_checked = ''; } echo '<img src="../images/items/'.$item_img.'" width="100" /> Home Page Slide Show: <input type="checkbox" id='.$id.' value="home" name="location['.$id.']" '.$set_checked.' /><input type="hidden" name="id[]" value='.$id.' /><br />'; } ?> <input type="hidden" name="hdnLine" value="<? echo $count ?>" /> <input type="submit" name="submit" value="Submit" /> </form> Quote Link to comment https://forums.phpfreaks.com/topic/260994-update-query-using-checkboxes-for-a-value-help/#findComment-1337729 Share on other sites More sharing options...
Guber-X Posted April 17, 2012 Author Share Posted April 17, 2012 ah, there we go, its working now. i just made a couple adjustments and its working great. Thanks Drummin i adjusted my DB where the location is, and made it used 0's and 1's... so i just made sure i changed all the "home" tags to "0" and added the "1" where the isset had a blank single quote. heres my final code for it. <?php if(isset($_POST['submit'])){ foreach($_POST['id'] as $id){ $value = (isset($_POST['location'][$id]) && $_POST['location'][$id]=="0" ? '0' : '1'); $insert = mysql_query("UPDATE items SET location='$value' WHERE id='$id'") or die('Insert Error: '.mysql_error()); } } $result = mysql_query("SELECT * FROM items") or die("Query Failed: ".mysql_error()); while($row = mysql_fetch_array($result)){ list($id, $item_info, $item_img, $price, $sale, $location) = $row; if($location == '0'){ $set_checked = 'checked="checked"'; }else{ $set_checked = ''; } echo '<img src="../images/items/'.$item_img.'" width="100" /> Home Page Slide Show: <input type="checkbox" id='.$id.' value="0" name="location['.$id.']" '.$set_checked.' /><input type="hidden" name="id[]" value='.$id.' /><br />'; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/260994-update-query-using-checkboxes-for-a-value-help/#findComment-1337990 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.