PHP application database logic flawed - php

I have a page on my application that is supposed to keep track of students. Once a counselor accepts the student for a in real life session. doing so the students records are then put into a table called support. My issue is that once that happens for any reason if a counselor needs to cut the session in half and place them back to the que if another counselor accepts the session then I have double, triple, quadruple (etc etc) of the same data being written to the table. Example :
is there a way that I can have it so if the session already exists in the database table called support then when the counselor hits session start, rather then inserting it updates? So an example would be :
Staff member john accepts student bobby
Staff member john has a family emergency and needs to end the session putting the student back in the que.
staff member Sara sees the student in the que and accepts the student
Rather then the session start button inserting the same session id (example 60) in again it will just update what was already done?
What I have in mind is to set up a Finish student button that will just update the support table rather then just rewrite to the table.
Does this solution seem feasible? - if not what can I do? Sorry for the way I asked this question I am trying to be as specific as possible.
EDIT : New pic for further examples :

You can alter the table to add a new attribute - "Session completed", that is by default 0, 1 if the student is accepted and currently in session, and 2 if it is completed. When a student is accepted, the 0 changes to 1, and if the student is done, councilor clicks OK, and Session Complete can change to 2 (every once in a while all students with 2 will be erased to not overpopulate the table). If an emergency happens, the councilor clicks Cancel and the 1 changes back to 0...

Related

How would I apply foreign keys to this specific requirement

I'm currently making a payslip system wherein there are two tables, the account_table and the payslip_table. I'm currently having trouble with the database design, I'm torn between creating a relationship between two tables or not creating one at all. I guess I'll first explain how this system works:
Admin logs in to the site
Admin goes to the upload payslip page
Admin uploads an excel file and the back end code parses the excel file and saves it in the database
User logs in to the site
User goes to the payslip page
Payslip page shows the row where the employee_id is the session_user(since a user's username is his employee_id)
The account table has the following columns:
1. employee_id
2. password
3. first_name
4. last_name
5. user_type
The payslip table has the following columns:
1. payslip_id
2. employee_id
3. salary
4. tax
5. total_deductions
6. total_salary
The requirements for this system is that the owner wants to upload a payslip even if there "isn't an account for a user".
Q. Then why are your tables designed like that?
A. The payslip in its nature has an employee_id and its presence is the key on determining which payslip owns which. For me to effectively show a payslip, all I need to do is to compare the session user (which I have mentioned is their employee id since it's their username) with the employee_id on the payslip table and just echo the row that was hit.
Q. What happens to the payslips when a user isn't an employee anymore? There will be useless rows on the payslip_table
A. I've decided to create a column named created_on and add a triger to delete payslips that are 3 months old (since they have no use already)
Q. What happens to an account when a user isn't an employee anymore?
A. Admins have the authority to delete a user, once a user is gone from the company, the admins can terminate the account and as mentioned above, the payslips of the deleted user will be eventually deleted
Q. Why go through all this trouble?
A. The owner specifically stated that he wanted to have the payslips ready even if an account has yet to be made so if one person were to create an account, his payslips are automatically ready for viewing
I have very minimal knowledge in database designs and I'm very open to suggestions.
Or if you guys could suggest an alternative way of achieving the requirement using foreign keys then that would be the best way to go.
You should create a relationship between the two, as you can see here you will be able to add a null value into a foreign key field and fill it later with an employee_id.
This will help with finding rows, and making sure there is no useless data floating about wasting space.
The only problem I see is how the admin will link payslips already on the system to a new employee account. e.g. finding the correct payslips for the new employee number

Query and Display referrer details

I have a website built with PHP and mysqli and now I am building administration panel for this website. I am facing some difficulties for querying and displaying referral data I do not have any idea how to do it.
I have a table called user_registration and the fields include, user_name, email, password, referrer. Whenever any of the registered member is referring others the referrer username will be saved in the field "referrer".
Now what I want is to fetch only the rows of the members who has referrals (means referred by my registered members) and also want to count how many referrals a member have and echo it.
For example: I have 20 registered members and from it 5 members have some referrals so I want to query and echo those member's username who has referrals and also count how many referrals they have:
Member's Username Total Referrals
user7 8
user6 6
user1 5
user9 3
user5 2
My solution would be. Make a new column in your user table like "totalreferrals". In this way you can easily keep track of the amount of people they have referred. You could also make a new table "referral". In here you would save the referrals name, the new persons name and a timestamp or something. Now it is even possible to make queries for different periods.
I think that you need to rethink about your database setup, instead of trying to make way to complicated queries, to make your database work.
I am just giving an idea in short which may help you to complete your project -
Suppose your site URL is http://yoursite.com.
1) Whenever a user is registered, you can generate a random key for that user and save it in the database for that particular user. e.g. user1 => abc123
2) Now, you can tell your user (may be on a page after their successful registration) that his/her referrer id is http://yoursite.com/?ref_id=abc123 (by appending it as a new parameter)
or if your register page is http://yoursite.com/register then http://yoursite.com/register/?ref_id=abc123
3) Now, they can share this link to their friends, colleague etc to whomever they want.
4) Now, suppose if a person (may be his friend to whom the user1 referred) clicks on the above link. He will land on your site with the parameter 'ref_id' appended.
5) Just using $_REQUEST['ref_id'] or $_GET['ref_id'], capture its value and store it in a SESSION variable. This you can do on the main page of your site e.g. index.php
6) Now, if that user does the registration, you can make an entry in the database that he has been referred once by the user who has referrer id abc123 i.e. user1.
7) So, you can add count = 1 for user1. If more people come with the same value for the 'ref_id' parameter, you can keep on increment the count.
8) If no such parameter is exists when user lands on your site, then that means he has not referred by anyone. So, that means he is the first kind of user i.e. user in point no. 1).
9) You may also need to take care of some validation part at some places in this.
EDIT:
SELECT user_name, referrer FROM user_registration WHERE referrer > 0;
Assuming that you have a proper insert query ready which gives you the result as you shown the table in your question.

How to post to multiple database tables with 1 to many relationships

I have a semi-complex database with multiple tables (3) that I'd like to post data to.
Rules of database
Each experiment (experiment table) can have multiple varibles associated with it (1 to many)
Each variable (variable table) can have multiple values (value table) (1 to many).
Sample DB Structure
experiment table
exprid (pk - auto_increment)
pagetype
pagename
expresult
variable table
varid (pk - auto_increment)
image
varresult
exprid (fk)
value table
valueid (pk - auto_increment)
image
valueresult
varid (fk)
I'm setting up a form that accepts inputs that will post to the appropriate tables, however I am having a hard time understanding how I can get this to work properly.
Here's essentially what I'd like to do:
User enters experiment details
User enters details for variable 1 (that info is associated with the experiment)
User enters details for value 1 (that info is associated with variable 1)
user submits value 1 to value table
User enters details for value 2 (that info is associated with variable 1)
user submits value 2 to value table
user is finished entering values for variable 1
User creates new variable in order to enter new values
Repeat steps 2-6
Ultimately, I am unsure how to:
a) post to the appropriate tables
b) ensure the visitor does not have to enter the experiment details once a value or variable is submitted
The ask:
It would be great if someone can help me understand the logic that goes into something like this. I really don't even know where to start because all of the forms I've built so far require me to only post to 1 table. So written logic would be helpful.
Any links to posts about this topic or articles online, any instructions on how to tackle or even sample code would be helpful. At this point, I think anything would be helpful if I can just get an idea of where to start.
So after each submit (via ajax request) you have to extend your list of fields.
If you're using jQuery I would suggest to have a look at it's ajax methods: http://api.jquery.com/jQuery.ajax/
When the data is submitted dynamically add your fields, there are dozens of tutorials, like: http://www.infotuts.com/dynamically-add-input-fields-to-form-jquery/
That's all, so in the end you'll have multiple requests which always store your new added data and skip the reload of the pages, as ajax is done in the background.

HTML/PHP Storing values in table, saving to database and showing it later

I am new and trying to make a simple website for a project. I have to pages, add and view hobbies. User needs to be able to add 1 or more hobbies and store it in a database and to retrieve it in another page, i have listed what i plan to do below.
How do i make it such that I am able to store multiple hobbies for users in the database?
Using MySQL, HTML, PHP, Dreamweaver
Assume: User has been created, user has been logged in and all user related database coding has been done. More than 1 user exists, all users are to have a few hobbies each.
Database
User table: UserId, Username, HobbyId
Hobby table: HobbyId, HobbyName, HobbyDaysAWeek
1 user can have 0 or more Hobbies.
Add hobby page
dropdownlist (binds list of hobbies from hobby table)
textbox (for user to enter how many days a week he spends engaging in that hobby)
add button (displays value in dropdownlist and textbox in a table)
table (Hobby and daysAWeek will be shown here, with a delete icon to delete it)
submit button(saves hobby & days a week & username into database)
View hobby page
Displays user's hobby & days a week in a table.
Thanks for helping! And please provide simple and bare minimum coding as i am new!
Well, nobody posted an answer but my question was answered in the comments so..
Start by adding a 3rd table with UserId and HobbyId – Dagon
You're storing the relationship between, not a relationship. So think of it like you have two things and you want to remember that they relate. Thus, a table with two columns, one with an identifier of one part, the other with identifier (ID) of the other part. Pull them together with the data about each part, you have information. Hence, you've stored the relationship. It's like metadata, really. It's usually pretty efficient. - Jared Farrish
Thanks guys!

How to prevent race condition in online hotel booking

I am writing a hotel booking software using PHP and MySQL. I am pretty much done with it but I ran into a race condition problem. For example there is only one standard room left and when 2 guests both select it it shows available to both of them. I tried fixing it by checking the room again when the guest clicks confirm before payment but that still has issues. I also tried making the room status to pending when whoever clicks the confirm first but I can't figure out how to change it back to available if the guest decides not to pay or just closes the browser. I searched SO for answers but I didn't really find a definitive answer. Thanks in advance
One solution is to add two columns to a table in the database. One column is the session ID or user ID or whatever of the user that is being offered the room. The second column is a timestamp indicating when that offer will expire.
Then, in your app, only show rooms that have an expired timestamp in the hold column. (Set the initial timestamp to 0 so that it starts out expired.) When a room is selected, check the column again. If there's an unexpired timestamp there, the user gets a "sorry, you were too slow" message. Otherwise, put a timestamp there for 15 minutes into the future or whatever, and proceed.
You see this on travel sites and ticket-purchasing sites a lot where it says something like "We're holding these seats for you for another 14 minutes. Please complete the transaction by then or it will be released blah blah blah."
I would also go with the pending state. You could save the state together with the session id of this user and have a cronjob that deletes all pending states that have expired session ids associated to them.

Categories