I am populating a table with information from a database, one of the values is a rating 1, 2, 3, or 4. I made it so that depending on the value in the database the radio button with the corresponding value is selected. But I am having problems figuring out how I can update the database when the radio button is changed to a new value. It doesn't have to be changed on click, it can be changed with a submit button after they have made all of the changes they would like to make.

I am fairly new to PHP so I appoligize in advanced if the code is a bit messy. But below is the code I used to populate the table with all of the appropriate values.

while($row2=mssql_fetch_array($SQLresult)) 
{
	//find the competency level
	$complevel = $row2[CompetencyLevel];
	if($complevel=="1") $a="checked";
	if($complevel=="2") $b="checked";
	if($complevel=="3") $c="checked";
	if($complevel=="4") $d="checked";
	//build the table
	echo "<tr class=\"\"><td>".$row2[CompetencyID]."</td><td>".$row2[CompetencyName]."</td>";
	echo "<td class=\"lft\"><input type=\"radio\" name=\"".$groupnum."\" value=\"1\"".$a."> 1";
	echo "<input type=\"radio\" name=\"".$groupnum."\" value=\"2\"".$b."> 2";
	echo "<input type=\"radio\" name=\"".$groupnum."\" value=\"3\"".$c."> 3";
	echo "<input type=\"radio\" name=\"".$groupnum."\" value=\"4\"".$d."> 4</td></tr>";
	//increment the group number
	$groupnum++;
	//clear the variables
	$a="";
	$b="";
	$c="";
	$d="";
}

Same answer that I gave to someone yesterday. You need to create a proper form and then you need to process the results from the form. Have a look at the two links below and try to apply what they are telling you.

Learn about HTML forms

Click here to see more about how to retrieve the data from the form in your php program.

I guess I can't wrap my head around how I would pass the value to the database when the form is submitted. The query returns a different number of rows for each person it is run on, each of those rows has to be rated then 1 - 4. I guess what I am having problems grasping is how I would pass the value of the radio button, the ID of the person and the id of the field that is being updated with 1 - 4 when submit is pressed.

Thinking about it I could probably use a couple of hidden inputs to hold the ID values I would need to update the database. I'll give it a try and see what I can come up with.

I tried and I seem to be missing something. Here is what I've added to my original above.

while($row2=mssql_fetch_array($SQLresult)) 
{

	//find the competency level
	$complevel = $row2[CompetencyLevel];
	if($complevel=="1") $a="checked";
	if($complevel=="2") $b="checked";
	if($complevel=="3") $c="checked";
	if($complevel=="4") $d="checked";
	//build the table
	echo "<tr class=\"\"><td>".$row2[CompetencyID]."</td><td>".$row2[Category]."</td>";
	echo "<td>".$row2[CompetencyName]."</td>";
	echo "<td class=\"lft\"><input type=\"radio\" name=\"".$row2[CompetencyID]."\" value=\"1\"".$a."> 1";
	echo "<input type=\"radio\" name=\"".$row2[CompetencyID]."\" value=\"2\"".$b."> 2";
	echo "<input type=\"radio\" name=\"".$row2[CompetencyID]."\" value=\"3\"".$c."> 3";
	echo "<input type=\"radio\" name=\"".$row2[CompetencyID]."\" value=\"4\"".$d."> 4";
	echo "<input type=\"hidden\" name=\"".$groupnum."\" value=\"".$row2[CompetencyGUID]."\">";
	echo "</td></tr>";
	$groupnum++;
	//clear the variables
	$a="";
	$b="";
	$c="";
	$d="";
}

Then below that I have this code to update sql

if($_POST['Submit']){
	for($i=0;$i<$count;$i++){
		$sqlUpdate="UPDATE data SET CompetencyLevel = '".$_POST[$row2[CompetencyID]]."' WHERE StudentGUID = '".$guid."' AND CompetencyGUID = '".$_POST[$groupnum]."'";
		mssql_query($sqlUpdate);
	}
}

The page refreshes when you press submit but the database doesn't update. I'm just not sure where to go from here, a little help would be appreciated.

I too have this problem.
y did nt u passe values in qutoes i mean here like this.
$row2

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.