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>";
	}
}	
?>

Link to comment
Share on other sites

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'];
}

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.