Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Getting check box values through query string and saving them into database.

Posted on 2008-10-13
1
Medium Priority
?
660 Views
Last Modified: 2013-12-13
I am looking ofr some code revision please, I am trying to save/update data from some check boxes ( based on groups and sub groups. Everything is working perfect just have one little glitch that I can't figure out. when I chance selection (EDIT check boxes) how do I minipulate the change and save it. Origionally how I did was, I deleted everything with the Contact ID and re-saved fresh data but that messes things up.

Question would be how do I clear check boxes data when user checks un-checks data. The old data is saved in database. ( i have three tables. groups, sub-groups and a look up table group-subgrups with contact ids)
Any ideas/suggesions?

I have attached the screenshot just to get an idea how groups/sub-groups are being displayed.

I have attached the code for save ( that works perfectly )
and update ( that's where I have some issues )
 Thanks.
Save groups/subgroups
--------------------
//pass to record set
$rs = mysql_query($sql, $cn);
while($row = mysql_fetch_array($rs))
{
print "<p>";
$group_id= $row["group_id"] ;
	
//name='maingroup[]' value="new_group_is_here___3"
//the part before the "___" is the actual group name;
//the part after the "___" is the group id
$group_name = $row["group_name"];
$group_value = preg_replace('/\s/','_',$group_name) . "___" . $group_id;
//$group_name_array[] = $group_name;
 
// if had check box for the Main Groups
Print " <input type='hidden' name='maingroup[]' value='" . $group_value ."'/>" . "  " . "<strong>". $group_name . "</strong>: "."</ br>";
	
//sub group part - start
$sql2 = "Select * from tbl_sub_groups where group_id = '$group_id'";
 
//pass to record set
$rs2 = mysql_query($sql2, $cn);
 
// check the number of records and print the main group name or skip it if not children.
	while($row2 = mysql_fetch_array($rs2))
	{
	$id= $row2["sub_group_id"] ;
	$sgroup_name = $row2["sgroup_name"];
 
	print "</ br>";
	Print " <input type='checkbox' name='{$group_value}[]' 
               value='{$id}'/>" . "  " . $sgroup_name . "</ br>";
	}
	// sub group part ends.
 	print "</p>";                                   
}
 
	
$maingroup=$_REQUEST['maingroup'];
foreach($maingroup as $v){
 
	list($group_name,$group_id)=explode("___",$v);
	$group_id = preg_replace('/_/',' ', $group_id);
 
	$subgroup=$_REQUEST[$v];
 
	if($subgroup){
		foreach($subgroup as $v){
		
	   	 $sql3 = "INSERT INTO `tbl_people_groups` (`people_id`, `group_id`, `sub_group_id`) 
                                         VALUES ('$people_id', '$group_id', '$v')";
	   	 $rs3 = mysql_query($sql3, $cn);
		 }	
		}
		else{
			    $sql3 = "INSERT INTO `tbl_people_groups` (`people_id`, `group_id`, `sub_group_id`) 
                                         VALUES ('$people_id', '$group_id', '')";
				$rs3 = mysql_query($sql3, $cn);
		}								 
}
 
	
$maingroup=$_REQUEST['maingroup'];
foreach($maingroup as $v){
 
	list($group_name,$group_id)=explode("___",$v);
	$group_id = preg_replace('/_/',' ', $group_id);
 
	$subgroup=$_REQUEST[$v];
 
	if($subgroup){
		foreach($subgroup as $v){
		
	   	 $sql3 = "INSERT INTO `tbl_people_groups` (`people_id`, `group_id`, `sub_group_id`) 
                                         VALUES ('$people_id', '$group_id', '$v')";
	   	 $rs3 = mysql_query($sql3, $cn);
		 }	
		}
		else{
			    $sql3 = "INSERT INTO `tbl_people_groups` (`people_id`, `group_id`, `sub_group_id`) 
                                         VALUES ('$people_id', '$group_id', '')";
				$rs3 = mysql_query($sql3, $cn);
		}								 
}
 
 
Edit:
---------------
//pass to record set
$rs = mysql_query($sql, $cn);
while($row = mysql_fetch_array($rs))
{
	print "<p>";
  
	$group_id= $row["group_id"] ;
	
	//name='maingroup[]' value="new_group_is_here___3"
	//the part before the "___" is the actual group name;
	//the part after the "___" is the group id
	$group_name = $row["group_name"];
	$group_value = preg_replace('/\s/','_',$group_name) . "___" . $group_id;
	//$group_name_array[] = $group_name;
	
    if (in_array(trim($group_id), $selected_groups))
{
   $checkedString = "checked=\"checked\"";
}
else
{
   $checkedString = "";
}
 
 
print "<input type='hidden' name='maingroup[]' value='" . $group_value ."' $checkedString />" . "  " . "<strong>". $group_name . "</strong>: "."</ br>";
 
	//sub group part - start
	$sql2 = "Select * from tbl_sub_groups where group_id = '$group_id'";
 
	//pass to record set
	$rs2 = mysql_query($sql2, $cn);
	while($row2 = mysql_fetch_array($rs2))
	{
		$sgroup_id= $row2["sub_group_id"] ;
		$sgroup_name = $row2["sgroup_name"];
		$group_id = $row2["group_id"];
		 
		$sql4 =  "Select * from tbl_people_groups where people_id = '$id' and group_id = '$group_id'";
        $rs4 = mysql_query($sql4, $cn);
	//echo $sql4;
	    while($row4 = mysql_fetch_array($rs4))
	    {
		 $selected_sub_groups[]= $row4["sub_group_id"];
	    }
 
	
	 	   if (in_array(trim($sgroup_id), $selected_sub_groups))
			{
			//echo "test 1";
 			  $checkedString2 = "checked=\"checked\"";		
			  print "</ br>";
		      Print " <input type='checkbox' name='{$group_value}[]' value='{$sgroup_id}' checked='$checkedString2'/>" . "  " . $sgroup_name . "</ br>";
			}
			else
			{ 
			//echo "test 2";
  			 $checkedString2 = "";
	
			  print "</ br>";
		      Print " <input type='checkbox' name='{$group_value}[]' value='{$sgroup_id}' />" . "  " . $sgroup_name . "</ br>";
			  
			}
 
	}
	// sub group part ends.
	print "</p>";                                   
}
 
Save:
-------
foreach($_REQUEST['maingroup'] as $grp){
// echo "Group:" . $grp . "<br />";
$pizza  = "Media___12";
$pieces = explode("___", $pizza);
// $pieces[0]. "<br />"; // piece1
$group = $pieces[1];
 
 $sql3 = "DELETE  from tbl_people_groups where people_id = '$id'";
  $rs = mysql_query($sql3,$cn);
  
 
 //explode("___", $grp)
  foreach( $_REQUEST[$grp] as $subgrp )
  {
  
 $sql2 = "insert into tbl_people_groups(people_id, group_id, sub_group_id) 
	                            values('$id','$group','$subgrp')";
		
  $rs = mysql_query($sql2,$cn);
		
// echo "Subgroup:" . $subgrp;
  }
}

Open in new window

0
Comment
Question by:martyje
1 Comment
 

Accepted Solution

by:
martyje earned 0 total points
ID: 22705457
I got it... ty guys...
Just added a counter to track the ids and delete and refresh the data whatever.

$count = 0;
      
foreach($_REQUEST['maingroup'] as $grp){
// echo "Group:" . $grp . "<br />";
$pizza  = "Media___12";
$pieces = explode("___", $pizza);
// $pieces[0]. "<br />"; // piece1
$group = $pieces[1];

// $sub = $_REQUEST[$grp];

      if ($count == 0){
     echo $sql3 = "DELETE  from tbl_people_groups where people_id = '$id'";
     $rs = mysql_query($sql3,$cn);
     $count = $count + 1;
     }
   //explode("___", $grp)
  foreach( $_REQUEST[$grp] as $subgrp )
  {
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
This article discusses how to create an extensible mechanism for linked drop downs.
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
Suggested Courses

927 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question