Jump to content

[SOLVED] Update statement within a loop - haviing to submit twice to see changes


plastik77

Recommended Posts

Hi, I've got a frustrating problem which i can't seem to resolve. Basically, I have tabulated data pulled from a database, however, one of the fields I have made an editable text field so that this field can be changed by the user. Therefore, the user can edit the text fields, click the Submit Changes button and the page should refresh showing the table of data with the changes having taken effect. One problem - the submit button has to be clicked twice before u see the changes on the page. This is because of the position of the update statement in the loop below which is definitely wrong, however, I can't how work out how else to resolve this as i need access to the variables and the text field values which must be generated before i can do my update statement. New to PHP so any help would be great.

 

<form action="<? $_SERVER['PHP_SELF'] ?>" method ="post" >
<?
$query = mysql_query("select* from TestQuestion t,Question q, Section s WHERE t.testID = '$_SESSION[test]' 
and q.questID = t.questID and s.sectionID = t.sectionID  and s.testId = '$_SESSION[test]' order by questNumber asc");
$numrows = mysql_num_rows($query);

for ($i=0;$i<$numrows;$i++)
	{	
		$row = mysql_fetch_array($query);
		$qID = $row['questID'];
                        /process update if form submitted
                if(isset($_POST['Submit']))
		{
			//update with the new quest number
			$update = mysql_query("update TestQuestion set questNumber ='$_POST[$qID]' where questID 
                                ='$qID'");
		}
		$questNum = $row['questNumber'];
		$sectName = $row['sectionName'];
		$qtext = $row['quesText'];
		$cat = $row['category'];
		$weight = $row['weight'];
		echo "<tr>";			
		echo "<th>Q: <input type='text' name='$qID' value='".$questNum."' size='2'></th>";
		echo "<th>".$qtext."</th></tr>";
	}
}	
?>

Thanks for the replies. For the moment, I will avoid using AJAX as this is a simple prototype. I realise I need to have the

script at the top of the page, but I'm not sure how best to access the values in the text field, when I try to use access the posted textfield value, I get the undefined index error for $qID. Any idea how i can access the array of values?

 

f(isset($_POST['submit'])) {
  //do the updating....
echo $_POST['$qID'];
}

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.