Solved

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

Posted on 2008-10-13
1
647 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
simplest php form 3 59
WordPress Themes 10 34
highcharts wont fix inside a bootstrap modal window 6 27
WordPress website error - Unable to access website. 5 31
Deprecated and Headed for the Dustbin By now, you have probably heard that some PHP features, while convenient, can also cause PHP security problems.  This article discusses one of those, called register_globals.  It is a thing you do not want.  …
This article discusses how to create an extensible mechanism for linked drop downs.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

911 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now