Ok I have been working on this for a long time and I am just plain stuck. What I am developing is an online application for a school to keep track of missing assignments that students have. I have a table that lists the students first name, last name, homeroom, and last period teachers. Each student in the table has an ID. What I want to do is have teachers click on "Add Assignment" and they would enter the assignment details and the subject and then using checkboxes select the students that are missing such assignment. Then on "Submit" the student data (first name, last name, homeroom, and last period teachers) would get copied into a new table along with the description of the assignment and the subject each as their own ID. This is so the teacher only has to enter the description and the subject once, but can assign it to multiple students.

I have two tables in a database "student" and "assignments" Inside the "student" table I have 4 fields (firstName, lastName, AP, and 8th) and in the "assignments" table I have 6 fields (firstName, lastName, AP, 8th, description, and subject). If you can help please do. I have looked everywhere on the internet for some examples to work off of, but I cannot find anything. I find lots on deleting multiple rows by clicking check boxes but that is not helping me.

Here is my code:

<?php
//Database Connection
mysql_connect($_MISSING['db_host'], $_MISSING['db_username'], $_MISSING['db_password']) or die(mysql_error());
@mysql_select_db($_MISSING['db_database']) or die(mysql_error());

if (!$user_id) include("login.php");


print("<title>" . $_MISSING['organization'] . " - " . $_MISSING['html_title'] . " / Add Assignment");
print("<link rel=\"stylesheet\" href=\"../css/style.css\" title=\"style.css\" type=\"text/css\">");


$sql="SELECT * FROM student ORDER BY lastName";
$result=mysql_query($sql);

$count=mysql_num_rows($result);

?>

<form action="<?=$_SERVER['PHP_SELF']?>" method="post">
<?php


print("<p>Enter New Assignment</p>");
print("<p><b>Subject:</b> <input type=\"text\" name=\"subject\" size=\"50\" /></p>");
print("<p><b>Description:</b> <input type=\"text\" name=\"description\" size=\"100\" /></p>");


//Prints Student in a Table
print("<div class=\"student_wrapper\">");
print("<div class=\"student_checkbox\">");
print("<b>ID</b>");
print("</div>"); //Closes DIV student_checkbox
print("<div class=\"student_lastName\">");
print("<b>Last Name</b>");
print("</div>"); //Closes DIV student_lastname
print("<div class=\"student_firstName\">");
print("<b>First Name</b>");
print("</div>");//Closes DIV student_firstname
print("<div class=\"student_AP\">");
print("<b>AP</b>");
print("</div>");//Closes DIV student_AP
print("<div class=\"student_8th\">");
print("<b>8th</b>");
print("</div>");//Closes DIV student_8th
print("<br /><hr />");



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

$value = ($students['id']);


?>

<div class="student_checkbox"><input class="checkbox" name="checkbox[]" type="checkbox" id="checkbox[]" value="<? echo $value; ?>"></div>

<?PHP

print("<div class=\"student_lastName\">" . $students['lastName'] . "</div>");
print("<div class=\"student_firstName\">" . $students['firstName'] . "</div>");
print("<div class=\"student_AP\">" . $students['AP'] . "</div>");
print("<div class=\"student_8th\">" . $students['8th'] . "</div>");
print("<br>");

}
print("</div>"); //End of <DIV class="student_wrapper">

print("<input type=\"submit\" name=\"submit\" value=\"Submit\" />");
print("</form>");



// Check if add button active, start this 
if($_POST['submit']){
$checkbox=$_POST['checkbox'];
for($i=0;$i<$count;$i++){

$ID = $checkbox[$i];

$first = $students['firstName'];
$last = $students['lastName'];
$AP_teacher = $students['AP'];
$last_teacher = $students['8th'];
$description = $_POST['description'];
$subject = $_POST['subject'];


$sql = "INSERT INTO assignments SET id = '$ID', firstName = '$first', lastName = '$last', AP = '$AP_teacher', 8th = '$last_teacher', description = '$description', subject = '$subject'";

$result = mysql_query($sql) or die(mysql_error());
}

// if successful redirect back to main page.

  if ($result) {
    echo('<p>New assignments added</p>');
	echo '<META HTTP-EQUIV="Refresh" Content="5; URL=../index.php">';    
	exit;
		    
  } else {
    echo('<p>Error adding new assignment: ' . mysql_error() . '</p>');
  }
  

}


mysql_close();


?>
Member Avatar for diafol

I've developed a few of these over the years!

Student table
=============
student_id (PK)
firstname
surname
class (homeroom)
teacher_id (FK; last period teacher) - same every day?

Assignments table
=================
assign_id (PK)
subject_id (FK)
title
description
set (date)
deadline (date)
teacher_id (FK)

Subject table
=============
subject_id (PK)
subject (e.g. English, Math etc)

Teacher table
=============
teacher_id (PK)
teachername

Assignees (or results)
=========
result_id (PK)
student_id (FK)
assign_id (FK)
presented (date)
grade (or however you're going to mark/grade/tick)

My solution:

1) Assign assignments to students (your checkbox page) - this will add students and assignments to the assignees table.
2) When work is presented to the teacher, he/she can then, with another checkbox page check off all who've presented the assignment (with a grade if req'd).
3) At a set time, say the the deadline day, enter a page for late work. You can then print off a list of kids with their missing work.

This takes a bit of fiddling with MYSQL - the JOIN clause is very important here. As you're aware, the system must be robust if you are going to use this in your school. Ensuring that you have a robust normalized MySQL DB is essential.

Good luck.

Yeah that is what I am doing but it is not assigning the assignments to the individual students selected and putting them in a new table, instead it is just placing in the "description" and the "subject" into the new table but not the student info. I know that I am doing something wrong in my code, but I can't seem to see what it is. I come from an ObjC world and this is very different. It is hard to debug this when I don't even get a MySQL error. It says that it is successful, but when I check the database it has the description and subject in there but not the student information.

Any ideas of where I am going wrong with my code?

Ok I have figured out why my data was not being transfered to the new table, but now I am having issues with my loop. If I select more than a single student, the last student selected is the one inserted into the database and not any of the others. Here is my code of my loop. Anyone see something that I am missing?

$i = 0;

while($students=mysql_fetch_array($result)){
?>
<tr>
<td align="center" bgcolor="#FFFFFF"><input name="checkbox[]" type="checkbox" id="checkbox[<? echo $i;?>]" value="<? echo $i++; ?>" /></td>
<td align="center"><input name="lastName[]" type="text" id="lastName" value="<? echo $students['lastName']; ?>"></td>
<td align="center"><input name="firstName[]" type="text" id="firstName" value="<? echo $students['firstName']; ?>"></td>
<td align="center"><input name="AP[]" type="text" id="AP" value="<? echo $students['AP']; ?>"></td>
<td align="center"><input name="last_teacher[]" type="text" id="last_teacher" value="<? echo $students['last_teacher']; ?>"></td>
<br />

</tr>
<?php
}
?>
<tr>
<!-- <td colspan="4" align="center"><input type="submit" name="Submit" value="Submit"></td> -->
<?php

// Get values from form 
$lastName=$_POST['lastName'];
$firstName=$_POST['firstName'];
$AP = $_POST['AP'];
$last_teacher = $_POST['last_teacher'];
$description = $_POST['description'];
$subject = $_POST['subject'];

$checkbox=$_POST['checkbox'];


// Check if button name "Submit" is active, do this 
if($_POST['Submit']){
foreach($checkbox as $i){
	

	$sql2 = "INSERT INTO assignments SET  firstName = '$firstName[$i]', lastName = '$lastName[$i]', AP = '$AP[$i]', last_teacher = '$last_teacher[$i]', description = '$description', subject = '$subject'";

	
}

$result2=mysql_query($sql2) or die(mysql_error());


if($result2){
header("location:../index.php");
}
mysql_close();
}

?>
Member Avatar for diafol

Ok, haven't the time to go through the whole thing:

name=checkbox[<?php echo $students['id'];?>] ... name=lastName[<?php echo $students['id'];?>] ... name=firstName[<?php echo $students['id'];?>]

Doing that in your php loop, will give your the 'student id' handle of all checkboxes selected.
In your form handling routine, loop through the $_POST array to extract the keys. You don't need the firstname and lastname for adding an assignment to a student. The assignees table, will just require a student is and an assignment id when assigning at the start.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.