The purpose of the database
I’m using a mysql database with an Apache server to store homeschool information. I’m trying to track each assignment similar to a grade book where I can query by subject and month to track scores.
My database design
assignment is the main table and has: id(primary auto inc), student_id, subject_id, points_id, date, Notes_id, and type.
notes contains optional notes on the assignment and has id(primary auto inc), note, and date.
points has the score and total available for the assignment and contains id(primary auto inc), received, and total.
student is a list of students(my children) and contains id(primary auto inc), first_name, and last_name(yes I know this column is pointless since its my kids but I did it for practice).
subject is a list of current subjects and contains id(primary auto inc), name, description. My logic was I can add more subjects pragmatically as needed each year.
type is contains the types of assignments and contains id(primary auto inc), name, description.
Now how do I join to print it out?
I need to join assignment, student, subject, points, notes into one table that I can iterate through with a while loop to echo out recent activity (data entry) into an html table.
I’ve tried many versions similar to:
$result = mysql_query("SELECT * FROM assignment, student, subject, points, notes
WHERE assignment.student_id = student.id
AND assignment.subject_id = subject.id
AND assignment.points_id = points.id
");
Doing so has yielded only empty queries. Can someone nudge me in the right direction? The end result I’m looking for is a combined table that fills in the stored _id’s in assignment with the values I want to display such as: 9/12/2012 Jane Doe Math Quiz 25/28 in an html table.
Additional info if needed… PHP function storing the data
function submit_entry($con){
$student = $_POST['student'];
$subject = $_POST['subject'];
$type = $_POST['type'];
$notes = mysql_real_escape_string($_POST['notes'],$con);
$received = mysql_real_escape_string($_POST['received'],$con);
$total = mysql_real_escape_string($_POST['total'],$con);
//Retreive student data
$token = strtok($student, " ");
$student_firstname = $token;
$token = strtok(" ");
$student_lastname = $token;
$result = mysql_query("SELECT * FROM student WHERE first_name='$student_firstname'");
$row = mysql_fetch_array($result);
$student_id = $row['id'];
//Retrieve subject data
$result = mysql_query("SELECT * FROM subject WHERE name='$subject'");
$row = mysql_fetch_array($result);
$subject_id = $row['id'];
//Retrieve type
$result = mysql_query("SELECT * FROM type WHERE name='$type'");
$row = mysql_fetch_array($result);
$type_id = $row['id'];
//obtain last inserted ID (assignment)
if(!mysql_query("INSERT INTO points ( received, total)
VALUES ('$received', '$total')"))
{
die('Error: ' . mysql_error() . ' in points INSERT!');
}
$points_id = mysql_insert_id();
if(!mysql_query("INSERT INTO assignment (student_id, subject_id, type, date, points_id)
VALUES ('$student_id', '$subject_id', '$type_id', NOW(), '$points_id')"))
{
die('Error: ' . mysql_error() . ' in assignment INSERT!');
}
}
I’ve simi gone with Remm’s suggestion. I’m polling for the values in each table (student, subject, and type) and storing them in an associative array.
It’s still a touch buggy. I want the tables to be separated by dates with a css created date block inserted infront of each one. Currently I am getting inaccurately created dates which is the result of an unforeseen problem with the statement
I can see how this project could be misconstrued as homework from the thoroughness of my details. Being a professional medical machinist programmer/operator has made me appreciate the importance of clearly communicating information.