Forum Moderators: coopster

Message Too Old, No Replies

Populate fields on form if data already exists

Populate fields on form if data already exists

         

djs1971

2:18 pm on Sep 26, 2010 (gmt 0)

10+ Year Member



Hi - newbie to PHP/mysql and the forum. Starting to get to grips but found first issue i haven't been able to resolve. Any help would be appreciated.

I have created a form that displays a list of students (based on the teacher selecting a class/lesson/date) with drop downs to enter a score, code and comment for the lesson.

There might be occasions when another member of staff may have already entered a score/code/comment for some of the students for that class/lesson/date so I would like them to show up in the form. Try as i might I can't get that to happen.

The code I have so far is below - any thoughts - probably an obvious one for those used to php/mysql!

Thanks in advance.
<form action="savescores.php" method="get" name="sampleform">

<input type="hidden" name="tutor" value="<?php echo $_GET["tutor"]; ?>" />
<input type="hidden" name="date" value="<?php echo $_GET["date"]; ?>" />
<input type="hidden" name="lesson" value="<?php echo $_GET["lesson"]; ?>" />


<?php

error_reporting(-1);

$con = mysql_connect("localhost","root","");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}

mysql_select_db("lakeside", $con);

$result = mysql_query("SELECT students.admin, students.fname, students.surname, students.tutor
FROM students
WHERE tutor='$_GET[tutor]'
ORDER BY students.tutor, students.surname");


echo "<table class='scores'>
<tr>
<th>Admin</th>
<th>Firstname</th>
<th>Surname</th>
<th>Tutor</th>
<th>Score</th>
<th>Code</th>
<th>Comment</th>
</tr>";

while($row = mysql_fetch_array($result))
{

$sqlstatement = "SELECT * FROM scores where admin = '" . $row['admin'] . "' and lesson = '" . $_GET[lesson] . "' and date = '" . $_GET[date] . "' ";


$studentsvalue = mysql_query($sqlstatement);


while($rowvalue = mysql_fetch_array($studentsvalue))
{
$score = $rowvalue['score'];
$comment = $rowvalue['comment'];
$code = $rowvalue['code'];

}



echo "<tr>";
echo "<td>" . $row['admin'] . "</td>";
echo "<td>" . $row['fname'] . "</td>";
echo "<td>" . $row['surname'] . "</td>";
echo "<td>" . $row['tutor'] . "</td>";

echo "<td><select name='score" . $row['admin'] . "' />
<option>" .score. "</option>
<option value=0>0</option>
<option value=1>1</option>
<option value=2>2</option>
<option value=2.5>2.5</option>
<option value=3>3</option>
<option value=3.5>3.5</option>
<option>4</option>
</select>
</td>";

echo "<td><select name='code" . $row['admin'] . "' />
<option>" . $code . "</option>
<option>PP</option>
<option>PS</option>
<option>F</option>
<option>INT</option>
<option>SP</option>
<option>VP</option>
<option>PF</option>
<option>OB</option>
<option>A</option>
<option>L</option>
<option>LC</option>
<option>S</option>
<option>OS</option>
<option>D</option>
<option>WR</option>
<option>RM</option>
<option>TO</option>
<option>DM</option>
<option>SW</option>
<option>AS</option>
<option>I</option>
<option>SL</option>
<option>TM</option>
<option>SB</option>
<option>IE</option>
<option>LT</option>
<option>DT</option>
<option>NFI</option>
</select>
</td>";



echo"<td><textarea name='comment" . $row['admin'] . "' />" . $comment . "</textarea>
</td>";
echo "</tr>";
}
echo "</table>";



mysql_close($con);
?>
<br />
<input type="submit" />

</form>

rainborick

4:04 pm on Sep 26, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



You may have transcribed your code incorrectly here, but this:

<option>" .score. "</option>

should probably be:

<option>" .$score. "</option>