Forum Moderators: coopster

Message Too Old, No Replies

pull data from database and show it in forms

access dynamic form

         

sqlnew

8:45 pm on Sep 16, 2005 (gmt 0)

10+ Year Member



HI,
I know some PHP and SQL scripts, but I am new to PHP/MySQL.
I have created an online survey form for my boss about one of his course. He needs his students to do the evaluation for a topic when he finishes each topic. For example, when he finished topic 1, he told students to evaluate topic 1. Two weeks later, when he finished topic 2, he asked the students to evaluate topic 2, so on. I created the survey form with radio buttons and I would like each student can keep his own record for the previous topic. For example, when it is time to do the evaluation for topic 2, when he opens the survey form, he can see his reply for topic 1.
Does any of you have any idea of doing this?
I have tried the following but failed to work:
1. I created a login page for the students with username and password.
2. I created a table name logpage. If the student is first time log in, insert the user name and password into the table logpage.
3. After the students log in, the student sees the survey form and does the evaluation for topic which is just finished.
Then submit.
4. I created another table named eval_table and insert the user name, password and the reply from the user into eval_table.
My problem is that next time when the student log in again, he still can not read his reply for the previous topic(s). I don’t know how to bind each user with his own survey form.

The following is my codes.
form.htm
<form name="form1" method="post" action="details.php">
<p>
<label>User name
<input type="text" name="name">
</label>
</p>
<p>
<label>Password
<input type="password" name="passwd">
</label>
</p>
<p>
<label></label>
</p>
<p align="center">
<label>
<input type="submit" name="Submit" value="Submit">
</label>
</p>
</form>

details.php
<?php
// listing script

// connect to the server
mysql_connect( 'localhost', 'user', 'pass4' )
or die( "Error! Could not connect to database: " . mysql_error() );

// select the database
mysql_select_db( 'db' )
or die( "Error! Could not select the database: " . mysql_error() );


$user_name = $_POST["name"];
$passwd = $_POST["passwd"];
//$topic1 = $_POST["sub1"];
$query = "SELECT * FROM logpage ";// WHERE user = "$user_name" AND password = "$passwd";
$result = mysql_query($query);
$num_rows = mysql_numrows($result);
for($i =1; $i<=$num_rows; $i++){
$name = mysql_result($result, $i, "user");
$password = mysql_result($result, $i, "password");
if($user_name == $name && $passwd == $password){
$flag = "old";
break;
}
}
if($flag!= "old"){
//include("evaluation.php");

$query = "INSERT INTO logpage (id, user, password) VALUES (NULL,'$user_name', '$passwd')";

}
else{
//include("evaluation.php");
$query = "UPDATE `logpage` SET `user`='$user_name', `password`='$passwd'
WHERE `id`='$id'";
}
$query = "SELECT * FROM `logpage` WHERE `user` = '$user_name' AND `password` = '$passwd'";

$results = mysql_query($query);
if( $results && $contact = mysql_fetch_object( $results ))
{

$id = $contact -> id;
$user = $contact -> user;
$pass = $contact -> password;
//echo "this is a test.";
echo( "$user, $pass, $id<br>" );
include("evl_form.php");
?>


<?php
//}
}
else
{
die( "Trouble getting contacts from database: " . mysql_error() );
} //

?>
evl_form.php
<form name="form1" method="post" action="thanks.php">
<label> Subject 1
<input name="sub1" type="radio" value="1">
1</label>
<label><input name="sub1" type="radio" value="2">
2</label>
<p>
<label></label>
</p>
<label> Subject 2
<input name="sub2" type="radio" value="1">
1</label>
<label>
<input name="sub2" type="radio" value="2">
2</label>

<label>
<input type="hidden" name="user" value = "<?php echo $user_name?>">
<input type="hidden" name="id" value = "<?php echo $id?>">
</label>
<p align="center">
<label>
<input type="submit" name="Submit" value="Submit">
</label>
</p>
</form>
thanks.php
<?php

echo "THank you for your time.<br>";
$id= $_POST['id'];
$user = $_POST['user'];
$sub1 = $_POST["sub1"];
mysql_connect( 'localhost', 'user', pass4' )
or die( "Error! Could not connect to database: " . mysql_error() );

// select the database
mysql_select_db( 'db' )
or die( "Error! Could not select the database: " . mysql_error() );

$query = "SELECT * FROM eval_table ";// WHERE user = "$user_name" AND password = "$passwd";
$result = mysql_query($query);
$num_rows = mysql_numrows($result);
for($i =1; $i<=$num_rows; $i++){
$name = mysql_result($result, $i, "user");

if($user_name == $name ){
$flag = "old";
break;
}
}
if($flag!= "old"){
//include("evaluation.php");

$query = "INSERT INTO eval_table (id, user, topic1) VALUES (NULL,'$user', '$sub1')";

}
else{
//include("evaluation.php");
$query = "UPDATE `eval_table` SET `topic1`='$sub1'
WHERE `user`='$user'";
}

$results = mysql_query( $query );

if( $results )
{
echo( "Successfully saved the entry.<br>" );
}
else
{
die( "Trouble saving information to the database: " . mysql_error() );
}
$query = "SELECT * FROM `eval_table` WHERE `user` = '$user'";

$results = mysql_query( $query );

// print out the results
if( $results )
{

if( $contact = mysql_fetch_object( $results ) )
{
// print out the info
$id = $contact -> id;
$user = $contact -> user;
$password = $contact -> password;
$topic1 = $contact -> topic1;
echo "For user ", $user, " the reply is ",$topic1, "<br>";

}

}
else
{
die( "Trouble getting contacts from database: " . mysql_error() );
}

?>

sqlnew

7:46 pm on Sep 21, 2005 (gmt 0)

10+ Year Member



Is it undoable?
Any idea is highly appreciated.

jd01

8:56 pm on Sep 21, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Of course this can be done...

Without writing it all out for you, here are some things you will need to do (you already have some of them, but I will go through the list):

1. Store the users/passwords, other info that you will need for determining current users, etc. -- table 1
2. Store the form question -- table 2
3. Store the results of the form -- either A). table 3 stored answers with username (or id number) in an extra col for association B). create a table 'username' for each user and store the results there.
4. When someone logs in check the username and password.
5. Select the previous set of questions (If a more recent set is posted.)
6. Select the corresponding set of answers from table 3 or username table.
7. Show the results.

5 & 6 Should be easy to select if you use a 'username' table, because the id of the question table and the answers in the username table will be the same -- If someone has not completed an eval, they will not have the most recent id, so they should be prompted to complete that eval. EG if there are 3 sets of questions and only 2 sets of answers for any student you will know they should be completing set 3 of the questions and you need to show set 2 as the previous, but if there is a 3 for each, they have answered all sets and set 3 answers should be shown.

Hope this gives you some ideas.

Justin

sqlnew

9:46 pm on Sep 21, 2005 (gmt 0)

10+ Year Member



Thank you very much for your help.I have waiting for any reply for several days.

For step2, must I store the form questions in a table? When the user log in, can I just show the user the evaluation form and when he/she finishes the evaluaction for topic eg. 1 and after submit it I store the result in table3.
For step7 show results, where do you mean show the results? Pull out the record from table 3 or show the result in the evaluation form?

Thanks again. I really appreciate you help.

jd01

12:40 am on Sep 22, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I would store the questions in a table for a few of reasons:

1. It would be very easy to keep track of the evaluations and use them again once they are entered EG next semester.

2. It would be easy for the instructor to add, edit, delete questions from the evaluations with a simple web-based form.

3. It would be easy to find the next evaluation in the set that a person should complete, if there are 15 total evaluations and a person has rows 1 & 2 in their table I could automate showing eval #3 and results from eval #2 very simply.

-- Basically, it is the easiest way to accomplish what you are looking for after the initial set-up.

can I just show the user the evaluation form and when he/she finishes the evaluaction for topic eg. 1 and after submit it I store the result in table3.

Yes, there are a number of ways to do this.
I would #1 select the max id from the USERNAME table. This would tell me what the last eval they completed was -- If there are two rows, I know the eval they need to complete is number 3.

Something like:
"SELECT * FROM ".$username." ORDER BY id LIMIT 1";

Then I would display the results from the USERNAME table, so in the example above, the user would see the results of the second evaluation on the page. Then I would put a link to the next eval to be completed. (Something like "<a href=\"/form.php?eval=".($selected['id']+1)."\">Complete the Next Eval</a>";

This would add one to the row number of the results I selected from the users table, which would give me the row id of the next eval in the form table.

Then I could select the next set of eval questions with something like this:

"SELECT * FROM froms WHERE id=".$eval."";

You could also go in the opposite order and select the last row in the user table, then immediately select the next eval to be completed and display it, with a link to the previous eval...

Hope this helps.

Justin

Added: I mean pull the questions from the table, and display them into a form.

sqlnew

2:56 pm on Sep 22, 2005 (gmt 0)

10+ Year Member



Thanks. Do you mean I show the student a form with only one new question (the just finished topic)? Actually, I have created a form with all the questions in it. Each question has 5 options I created with radio button. Whenever there is topic finishes, the student go the form and do the new topic and can see his/her reply for the previous topics. I don't know when a student log in, how can I direct him/her to his own form so he can see his reply for the previous topics.
Thanks again.