Jump to content

Update query using Checkboxes for a value HELP!


Guber-X

Recommended Posts

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

post-60710-13482403427352_thumb.png

Link to comment
Share on other sites

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>

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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>

Link to comment
Share on other sites

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 />';
 }
?>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.