Today we continued learning about implementing a competition database with our website.
Class notes:
To continue the work we were doing last week to create a competition database here is the sql for making the database:
CREATE TABLE Competition (CompID INT AUTO_INCREMENT PRIMARY KEY,CompName VARCHAR(255), compdesc VARCHAR(255) StartDate DATETIME, EndDate DATETIME);
CREATE TABLE Question (QuestionID INT PK AI, CompID INT FK, QuestionText TEXT);
CREATE TABLE Proposed Answer (AnswerIDF INT PK AI, QuestionID INT FK NOT NULL, AnswerText TEXT, IsCorrect INT(1) NOT NULL DEFAULT(0));
CREATE TABLE Competitor(CompetitorID INT PK AI, Classroom VARCHAR(255) NOT NULL. Email VARCHAR (255), SchoolID INT FK);
CREATE TABLE School (SchoolID INT PK AI, SchoolName VARCHAR(255) NOT NULL, SchoolPhone VARCHAR(25) NOT NULL);
CREATE TABLE Competition_Responce (CompID INT FK PK, AnswerID INT FK PK, COmpetitorID INT FK, WHEN DATETIME DEFAULT non());
The phpmyadmin GUI database editor was not the easiest to use, particularly in the process of setting foreign key constraints. I tried setting the foreign keys with the relation view after making the tables and it was unreliable if it actually worked.
So in class I wrote up SQL code so I can set up a file that recreates the database quickly if I need to drop and then recreated the database in the future, rather than using a GUI editor.
Our competition form needs to have the following:
- Classroom
- School
- SchoolNumber
- Competition Question
- Proposed answers
There will be INSERT queries into the following table fields:
Competition
- CompID- From form
Competitor
- Classroom
School
- Name
- Number
Response
- CompID
- CompetitorID
- AnswerID-From form
- When (when submitted this information is from the form)
Question
- QuestionID- From form
On the form will be hidden the AnswerID for each of the proposed answers and the QuestionID for the displayed question, this is so the questions and answers can be identified
CompetitionID hard coded (piece of code containing CompetitionID in the php code) into the view because we don’t know what the competition is.
When form submitted the form is submitted to the controller, and this is sent to the database object to be inserted into the database.
The process for inserting into the database after submitting the form is:
- INSERT School
INSERT INTO School(SchoolName, SchoolPhone) VALUES ($_REQUEST[‘school’],$_REQUEST[‘SchoolNumber’]);
This will create a string, inside SQL we have to use single quote for a schoolname which is a string. ‘”. this concatenates the school. You start a new string with a single quote character.
Model runs the INSERT INTO School. Create a database object and run this query against it.
$SQL=”INSERT INTO School (SchoolName, SchoolPhone) VALUES (‘”.$_REQUEST[‘school’].”‘, ‘”.$_REQUEST[‘SchoolNumber’].”‘)”
Todd recommended making a model class named Insert for School.
Making a form: Below is a basic form
<form method=”post” action=”form_controller.php” onsubmit=”Return true”> //Onsubmit will contain JavaScript for making validating the form before submitting it
Classroom <input type=”text” value=” ” name=”classroom”>
School <input type=”text” value=” ” name=”SchoolName”>
<input type=”submit” value=”win!” name=”Command”> //Controller will look for a command named win!
</form>
NOTE: In order to submit information to server you must give input type a name, the value of the name is what you put in the request.So in model you would write $name=$_REQUEST[‘classroom’]; The classroom name will contain the value that the user wrote into the input field, and this line will store this inputted value into the $name variable.
Use the POST method for forms as it hides the contents of the form from the user.
In your website you will have the following folders:
/views
/controllers
/models
With new model class (which can be a new model file) for the form model. Similarly you can still use fat controller method where you have one controller for the whole website (i.e. the controller deals with the website and the form handling) or you can make a new controller file which just does the handling for the form and decides what query in the model to run).
The SQL query is stored in the model.
Project Notes: I need to start building the form for my website, and will start creating a controller for the form.