Best way to store exam data in a database [closed] - php

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 7 years ago.
Improve this question
I am looking to implement a new database and store students exam data and marks in the database. There are an arbitrary number of students and each student has an arbitrary number of modules with corresponding marks. I need the database to be created so that I am able to query the database and return the students name followed by each of their modules and corresponding marks and then to move onto the next student. This is because I want to display it in PHP with a list of students names in bold and then a table of their modules and their marks.
I initially considered to organise the table like this:
| StudentName | Module | Result |
With a new entry for each module that the student takes and just store multiple students but then I do not know how I would then query the database and retrieve each StudentName individually and then be able to loop through their corresponding modules and results to store it in an HTML table.
Any ideas would be appreciated

Give each table an id field that auto increments
Where you need to link results to modules, for example, you'd add a
moduleID field to the result table to tie the result to the module.
You'd then do queries with joins to bring in both sets of data.
To tie students to modules, you'd need another table that has a
studentID field and a ModuleID field. You'd then query this table
and join on StudentName and Module to get the respective data for
each.

First, you need several tables. One table cannot do it. Create a table for each type of objects. In this case, you need a table for students and one for modules.
Both tables need an id column to identify the rows. Create a primary key on them.
Students table: Id, StudentName
Module table: Id, ModuleName
There is a many to many relationship between students and modules (each student can have more than one module and each module can be chosen by many students). A many to many relationship requires a separate table.
You were not clear if there is one grade per student and module. If there is only one grade, the best solution is to include the grade in this relationship table.
Grade table: Id, StudentId, ModuleId, Grade

Related

Is it possible to insert a column in a database table automatically using a variable in PHP? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 5 years ago.
Improve this question
I am trying to insert a column in a database table automatically using a variable.
I have a table named tbl_category, I want to let the user choose the sub category field by their own choice. So, from the dashboard, the user will insert 3/4 (or how many fields they would like to create) sub category fields.
I will take this value as $insert_sub_cat_count. So, when this info will hit the function named function save_category_info($data), the function will receive the value $insert_sub_cat_count as $data.
After that, this info have to implement in the table tbl_category and add 3/4 fields automatically according to the value $data.
If the user inputs 2, this will insert two columns automatically:
If the user inputs 3, this will insert 3 columns:
Is this possible? I don't know how to extend columns automatically or if there's any other way to do so.
This is a bad idea from the very start. As Raymond Nijland said above ("the same column names with increment are a SQL anti pattern.. you should check table normalization instead"), you shouldn't allow users to create columns with their desired name in your database. You should have the following tables:
user - id, name
categories - id, id_user, name
subcategories - id, id_category, id_user, name
So you will be able to link the category and subcategory to the user that created it. You don't need to create a separate column for each user.
If you're worried about speed you should add index for the subcategories table, for the column: id_user. In this way the search will work fast enough.
Insert is used to insert variables into an already existing column, you need to first create your column before filling it:
ALTER TABLE `tbl_category` ADD `sub_category_one` VARCHAR(100) NOT NULL;

How do i set up tables in an mysql database based on member positions? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
I am trying to set up tables in a mysql database so that a member can be assigned a position based on their section, group and role.
So if i had a form with three drop down boxes in html like so:
<form>
<select name="section"></select>
<select name="group"></select>
<select name="role"></select>
<button name="setPosition">Set Position</button>
</form>
How do I set up the sql tables so that each drop down can show whats available from there respective table based on the previous selection, and how would the member be assigned to that position in the database?
Thanks,
Edit: Sorry from not being clear, I know how to use ajax to get query's from a database and populate the selects with that data. What I need is the sql tables to be set up so that the selects can be context based, so for example, If I select a value from 'section' the 'group' shows all the groups in that section, which it gets from said table.
The standard way to set this up would be like this, with a members table that holds the id's for the other related tables, it's called "normalizing" the data.
table name columns
--------------------------------------------------
members section_id, group_id, role_id
sections id, section_name
groups id, group_name
roles id, role_name

Structuring user table in mySQL [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 8 years ago.
Improve this question
I'm trying to decide the best possible way to structure my user table. Users can have access to multiple "brands" and each brand will have multiple tables. That is, if user X wants to see data for brand Y, the database contains the information to say which tables I need to make calls to.
For example, user X can access Brand1 and Brand2. Brand1 has its data in table1, table2, and table3. Brand2 has its data in table4, table5, table6. User selects Brand2 and the application makes a call to find out that table4, table5, table6 should be used until user selects a different brand.
What's the best way to structure this knowing that a single brand might have multiple users that can access the data?
Do I need more than just a user table and, if so, what else and how would that connect to the user table?
Thanks.
Like Mark Baker pointed, you can have one user table, one brand table and one user_brand table.
user table - stores user_id (and other user data)
brand table - stores brand_id (and other brand data)
You've already defined relationships between users and brands. It's M:N (many to many), which means that:
one user can have access to multiple brands.
one brand can be accessed by many users.
Table user_brand solves the access problem.
user_brand table - stores user_id and brand_id (and optional data which better describes this relationship).
Here is an example about sql syntax (enforcing foreign key constraints).
You can use GRANT query so the user can access just 2 tables in a database, then in the application, you can code it just to select 1 table, until the user changes the brand. The brand itself is the table, isn't it?
In PHP code, the code and query should be like this:
<?php
$db = new mysqli('hostname', 'db_username', 'db_password', 'db_name');
$brand = $_SESSION['brandName']; // use this if you use sessions to cache the data
$db->query("SELECT * from `$brand`");

Database performance from php calls [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
I'm wondering about a potential problem I might get. My problem is let's say I have 2 tables one called speakers and clients. In the client table the client has the speaker IDs of 1,5,8(SAVED in a STRING field) - so I'm using explode() to get the values.
So now I have to call the speaker table 3 times to get the values of each speaker. This introduces the problem that it will get very expensive if there is alot of users online wouldn't it?
Is there an alternative to calling a table from an array of items or something?
I'm not too clued up about all the php approaches to this so any help will be appreciated!
I agree with #RiggsFolly that it may not be the best way to store the data, you could do something like this:
SELECT (whatever fields you want or speakers.* for all) FROM speakers JOIN clients ON clients.id=(clients.id) WHERE speakers.id IN (clients.field_with_speakers_list_string);
I believe that will get you going. It should return what you want. You would just need to replace (clients.id) with the client ID that you already have in your script and change speakers.id to whatever the ID field in your speakers table is called, and change field_with_speakers_list_string to whatever the field in your clients table with the string of speakers is called, and of course change the part of the SELECT to the fields you want to limit it by.
Based on the description of your database, each Client can have multiple Speakers. This is a one-to-many (1-N) relationship, and is typically expressed in two tables. One is a main table, "clients", which stores information about the client, but nothing about the speakers. A subordinate table, "speakers", stores information about each speaker AND the ID of the Client it is associated with.
For example, Speaker 1, Speaker 5, and Speaker 8 all have the same Client ID in the speakers table. The client_id field in the speakers table is called a foreign key.
Now you can get all the speaker information for Client 1 in one query:
select * from clients, speakers where clients.clientid=speakers.clientid and clients.clientid=1
Your current two-table design is flawed because you have the speakeridS column in the clients table. The foreign key column that expresses the ONE in a 1-N relationship should always be in the "many" table, i.e. "speakers".
Since your current speakers table doesn't have client_id information, you'll have to write a data transformer to migrate the string, comma-delimited speakers field in the clients table, to a numeric foreign key field "clientid" in the speakers table. This is a one-time transformation so you can do it in a PHP script:
$query="select * from clients";
$rs=mysql_query($query,$db);
while ($myrow=mysql_fetch_array($rs)){
$clientid=$myrow['clientid'];
$speakerids=explode(',',$myrow['speakers']);
foreach ($speakerids as $speakerid){
if (!is_numeric($speakerid)) continue;
$query="update speakers set clientid=$clientid where speakerid=$speakerid";
mysql_query($query,$db);
}
}
Your application displays the speakers in a checklist. The interface, however, doesn't have to dictate your storage structure. Write a loop and store the client ID in each speaker record.
IF the same speaker works with multiple clients, then you'll need a many-to-many (N-N) table. This is done via a bridging table. In this case, neither clients or speakers table need to know about each other. Create another table called "clientspeakers" which includes at least two foreign keys: clientid and speakerid.

Many to many relationship with Mysql & PHP [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
Let' say I Have 2 tables, webpage table, and a keywords table. It's a many to many relationshiop, right? One webpage can contain more than one keyword, and one keyword can be part of more than one webpage.
Webpage table contain id field as PK, and few other fields. Keywords table contain id as a PK, and also a few other fields. Third table, a child table, should contain id fields from both parent tables? Is it posible to track many to many relationship, with no foreign keys, just declaring this 2 id fields in child table as UNIQUE?
With or without FK's, when inserting new keywords for example through PHP, how should I refer, to which webpage this new keyword belongs, webpage id in a webpage table, or a id in a child table?
I would do something like this...
Table1
Table_WebPage
PageID, PageName, Url,...........
Table2
Table_KeyWords
WordID, Word, .........
Table3
Table_PageKeyWords
ID, PageID, WordID
Dont know why you want to do it without FK, Having FKs will enforce the data integrity and stop garbage data coming into your tables.

Categories