I am only beginner in this. I don't understand all of code, most of it was given to me to work on.

I am trying to do checks on MySQL on workshift table. There is dayshift, eveningshift and nightshift (Other two fields are shiftid, name and date). They are drop down box with y for yes and n for no. They are allowed to work ONE workshift a time. They are NOT allowed to do more than 1 workshift. When inserting data using form from PHP, if I picked y for dayshift and eveningshift, it shouldn't allow me do that maybe pop up box say its not allowed or something.

I don't know how to do that sort of checks.

//$query = "SELECT name,owner,species,sex,birth FROM pet WHERE name!='fluffy' ORDER BY name";
$header = "WORK SHIFTS";
$table = "workshift";
$query = "SELECT * FROM ".$table." ORDER BY DATE ASC";
$query = "SELECT * FROM ".$table." WHERE 'n' NOT IN (DAYSHIFT, EVENINGSHIFT, NIGHTSHIFT)";

Can you help me soon please?

Dani AI

Generated

A reliable solution splits the work into three layers: a database rule that cannot be bypassed, server-side checks that enforce that rule and give clear errors, and optional client-side UX to prevent obvious mistakes. That follows 's suggestion to think the logic through first and builds on 's remark about storing the chosen shift as a single value rather than multiple independent flags.

A compact schema-level enforcement is a composite unique index on the person (or user) plus the date so the database will reject any second assignment for the same person/date. One way to model it:

CREATE TABLE schedule (
  id INT AUTO_INCREMENT PRIMARY KEY,
  user_id INT NOT NULL,
  shift_date DATE NOT NULL,
  shift ENUM('day','evening','night') NOT NULL,
  UNIQUE KEY uq_user_date (user_id, shift_date)
);

See the MySQL guidance on creating tables and the ENUM type for options: MySQL CREATE TABLE docs and MySQL ENUM.

Server-side code should use prepared statements and transactions and must handle the duplicate-key error from the database as a normal, expected outcome (reporting a friendly message). A common pattern with PDO is to attempt the INSERT inside a transaction and catch a PDOException; MySQL duplicate-entry errors surface as SQL error 1062. See PDO prepared statements and PDO transactions.

Client-side controls (a single-select control or radios) improve usability but are not a security measure; rely on the DB+server rules for correctness. Quick checks during testing: try inserting the same user/date twice, submit malformed dates, and test time-zone/format edge cases to confirm the server-side logic rejects invalid inputs cleanly. For front-end hints and pattern examples, see Form validation.

Recommended Answers

All 4 Replies

I think you really have to at least attempt to do your homework not just ask people how to start. First of all try to figure out the logic of it and write it out in pseudocode(words) before even starting. Then give us what you've got cause up there you ain't got anything, even worse you have pointless/confusing comments:

//$query = "SELECT name,owner,species,sex,birth FROM pet WHERE name!='fluffy' ORDER BY name";

I know it doesn't belong but it looks like honestly you don't care about the coding at all, you just want it done.

Member Avatar for Member #120589

Have to agree. Get a book and work your way through it. You'll need to know the basics in case you need to change the code at a later date.

However, you'll probably want client-side and server-side validation on your form. The javascript should disable the option of too much night shifts automatically (if user has to login - then data can be loaded into the form beforehand). BUT do not rely on client-side validation, this can be circumvented quite easily. Your server-side validation should query the database for a nightshift entry, either prior to the insert query or as a clause in the actual insert query itself.

I think you really have to at least attempt to do your homework not just ask people how to start. First of all try to figure out the logic of it and write it out in pseudocode(words) before even starting. Then give us what you've got cause up there you ain't got anything, even worse you have pointless/confusing comments:

//$query = "SELECT name,owner,species,sex,birth FROM pet WHERE name!='fluffy' ORDER BY name";

I know it doesn't belong but it looks like honestly you don't care about the coding at all, you just want it done.

That code above you put in was given to me in assignment. That bit is on every page for each table. It there for example.

I been at this for hours and can't find anything on internet to do this.

I can't buy a book cos you need order it on internet and get it delievered at home, there not enough time to do that. I had 2 weeks to do this (Other bits was was fine, it only this is problem). Now got last couple of days.

I have done some basic. I learnt all basics from w3schools.com website. It doesn't have any sort of checks entering data into table if it is allowed or not. BTW, I don't need to change code later date. When this is handed in then thats it.

I have been looking for validation on internet but can't find right one to do with this. Can you tell me what sort of validation function for MySQL or PHP then I will try work out the code for it.

Member Avatar for Member #120589

That code above you put in was given to me in assignment

.

Giving you answers to your assignment questions would be cheating!

You don't have to buy a book on the internet, most good bookshops will sell you a book on pHp/MySQL. In addition, almost everything you need to know will be in electronic format somewhere on the internet in the shape of tutorials.

You just need to make the effort.

I will however give you a clue:

The table needs to have one field for SHIFT. The datatype can vary, but you could choose 'char' with valid entries = 'n' or 'e' or 'd'. You then specify a date and user_id in the same table. If a record exists for a person, you cannot insert only update. If no record, only insert not update.

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.