I am quietly going insane on this project. I've never worked on a project like this one before. All my previous sticky forms were for data entry, not editing. I don't know how to display the form with data from the database, allow the user to update it, and then display the form again with POST data. I can get the data out of the database and get the user updates back into the database, but how do I get the filled-out form back to the user again! The form displays again but all the input fields are blank.
Somehow I have to name the sticky fields with variable names that are identical for the data coming out of the database and for the POST data, and I can't figure out how to do that. Quiz scores come from mysql. Activities come from mysql and are updated by the user. `activities` and `quizzes` are MySQL tables.
I cut this back to just the relevant parts of the code. I omitted insert & update queries because they're working but left fetch because there's probably something in the way I'm naming variables. I'm hazy on how all these foreach loops work. For instance, I wasn't sure how to grab them later as post variables. Because I think that if I could rename the post variables as simple variables, the first time around I could rename the fetch results as simple variables, and then put the simple variables in the form fields....It sounds so simple but nothing I've tried works! Or if I could put the posts into the $act array that was holding the fetch results....Any advice on the simplest way to straighten out this mess and get it working would be most appreciated!
I'm limited to php 4.3 and mysql 4.0 right now but will soon be upgrading this app to php 5 and mysql 5, so it has to run on both versions of both programs.
[PHP]<?php
if (array_key_exists('_submit_check', $_POST)) //ON SUBMIT
{
include ('./includes/init_session.php');
//Process Activities Post data
$one_1_1 = $_POST['one_1_1'];
// update and insert records
}
else //ON INITIAL PAGE LOAD
{
//prepare query
$quiz_query = "
SELECT quiz1, quiz2, quiz3, quiz4, quiz5, quiz6,
quiz7, quiz8_1, quiz8_3, quiz9, quiz10
FROM quiz_scores
WHERE userid='$userid'
";
//query quiz_scores table in database
$quiz_results = mysql_query($quiz_query);
//fetch quiz scores from results
$quiz_results_array = mysql_fetch_assoc($quiz_results);
//initialize variables
$quiz['key'] = 'value'; //initialize array
$quiz_total = 0;
$quiz_count = 0;
foreach($quiz_results_array as $quiz_nbr => $score)
{
$quiz[$quiz_nbr] = $score;
}
$act_select_query = "SELECT * FROM activities
WHERE userid = '$userid'";
$act_select_results = mysql_query($act_select_query);
$act_results_array = mysql_fetch_assoc($act_select_results);
$act['key'] = 'value'; //initialize array
foreach($act_results_array as $field => $value)
{
$act[$field] = $value;
}
}
?>
<form name="form1" method="post"
action="<?php echo $_SERVER['PHP_SELF']; ?>">
<input name="one_1_1" type="checkbox" value="checked"
<?php if ($act['one_1_1'] == "checked") echo 'checked="$checked"'; ?>
/>
<?php if ($quiz[quiz1]) echo $quiz[quiz1]; ?>
</form>
[/PHP]