Changes not picked up by script
| From: | Miles Thompson | Date: | Tue, 12 Dec 2000 01:28:10 +0000 |
| Subject: | Changes not picked up by script | ||
| Groups: | php.general | ||
| Request: | Send a blank email to php-general+get-29790@lists.php.net to get a copy of this message | ||
I discovered that where data is fetched from a database is important in
a PHP script.
I have a simple form for user registration. If the user is registered,
the user's registration key is passed to the script, data is fetched and
the fields of the form are automatically filled in. The user can then
make any changes and submit them.
The problem was that the changes were not being picked up by the script,
but they showed up in HTTP_POST_VARS. Could this be a sequencing
(program flow) problem? Here's how the page flows, a mix of pseudocode
and "straight from the file" PHP.
The problem was that the data was fetched at the very head of the
script. When the data fetch was to the beginning of the display block
averything worked fine.
Hope this helps someone - Miles Thompson
------- start of script -------
connect to database, etc.
if nUserKey passed to form
fetch values and assign to variables
e.g. $cUsername, $cFirstName, $cCity etc.
Note - This is the original location for the data fetch.
Set cChkOK = 'yes' - control variable for whether update executes
Function definitions to check for empty fields and to set flag values,
validity of email address etc. If any fail they set cChkOK = "no"
Action Block
************
If SUBMIT or UPDATE value present
Switch statement
Case Add
Check that cUsername not already taken.
Display error message if it is,
Call functions which check fields
Create and store SQL statement to variable $sql
Case Update
Call functions which check fields, etc.
If any check fails, cChkOK = 'no'
Create and store SQL statement to variable $sql
The code:
$sql = "update bidder set
cUsername = '$cUsername',
cFirstName = '$cFirstName', etc., etc.,
cCity = 'ScCity'
where nUserKey = $nUserKey"
End of switch statement
If cChkOK = 'yes' and hidden variable on form is set
Echo $sql - for debugging
Execute $sql
If executes OK, display success message and return
endif SUBMIT or UPDATE
else
When the fetching of the data was moved here, both Add and Update
routines worked.
DISPLAY form
echo "<table>";
echo "<form method=\"post\" action=\"$PHP_SELF\">";
echo "<tr> <td>" . $arr_lbl[ lbl_cUsername ] . "</td>
<td> <input type=\"text\" name=\"cUsername\"
value = \"$cUsername\"$cDisabled size=10>
</td></tr>";
echo "<tr> <td>" . $arr_lbl[ lbl_cEmail ] . "</td>
<td> <input type=\"text\" name=\"cCity\"
value=\"$cCity\" size=30 maxlength=30>
</td> </tr>";
a couple of hidden fields,
submit buttons, etc.
close the form
close the table
end of the else.
-------------- end of script -------