Hello
I'll explain to you with an example.
If an admin updates the admin table, then some data is automatically added to the stock table. And if a client submits a form to the sales table, then also some parts of the data update automatically in the stock table.
I mean I want some contents of the admin and sales tables in the stock table. I want to do the process with PHP.
You want to use either triggers, as Cybernate mentioned, or stored procedures to combine the two operations.
Triggers - http://dev.mysql.com/doc/refman/5.0/en/triggers.html
Stored Procedures - http://dev.mysql.com/tech-resources/articles/mysql-storedproc.html
If you want to do it in PHP only, simply add the update to the other table within whatever functions you are using.
Related
I am fairly new to web development and currently working on a website using an MVC framework that can capture maintenance work conducted. I have managed to make the forms and it correctly displays any errors in filling the form and if there aren't any errors successfully inserts it into the database. What I would like to achieve is having the main table with the general details of the maintenance such as (date, time, technician, department, location, recommendations) and another table for which records what tasks were done during the maintenance such as sweeping, mopping, wiping the windows, cutting grass, etc. I have a single form that requires all the details required in both the tables to be filled. both tables will have primary keys that will be auto-increment. I would then like to simultaneously insert the data into the relevant tables only while inserting data into the tasks table I would like to have a foreign key to the main table for that particular record so it corresponds accordingly. How can I achieve this without manual input by the user if the primary key of the main table is an auto increment?
This isn't a big problem. It can't be done as a single query, but using transactions you can achieve an all-or-nothing result.
In pseudocode:
Validate data
Start a transaction
Insert data into main record
Get the last inserted ID
Insert one or more records into the child table, using the ID retrieved above
Commit the transaction (or roll back if some error occurred)
The exact mechanics vary between MySQLi and PDO, but the principle is the same.
I have a table called users and a table called pages. Users of the system can subscribe to a page and receive updates about the page. My problem is that users and pages will be updated dynamically (ie. no manual intervention to the tables) and I don't want to keep adding another column everytime someone subscribes to the page.
How can I achieve updating both the users table and the pages table dynamically to reflect that they have subscribed to that page?
My idea would be to add an comma separated array of usernames into the pages table and update them as users subscribe/unsubscribe.
Just making it an official answer:
While the initial hunch may be to use comma separated values to represent the link between those 2 tables (or any other way of saving the data in one column like saving a json string), it is actually bad practice because it does not conform to the First Normal Form (and definitely not 2nd and 3rd).
First Normal Form - Wikipedia
First Normal Form says you should never store more than 1 value in 1 table cell.
The problem, in short, starts when you'll need to use that data, which will actually take you at least 2 actions - 1 is reading the data from the database and 2nd is to parse it in your languaging script. Imagine what happens when you need then to use that data to read some other data from the database - you are making more sql queries than you need and taking at least twice the time (+resources). It becomes even more complicated when you need to use JOIN queries or have other one-to-many data relationships.
The solution then is simple - you need to create a 3rd table that serves as an intermediate table.
You can call it users_pages or user2pages and that represents the 1 to many relationship between 1 user and many pages.
The structure of the table is as simple as:
users_pages
-----------
-- id // a unique id for the relationship, can be auto generated
-- user_id // the user id
-- page_id // the page id
-----------
This allows you to build a more robust application as well as run advanced queries and calculations without the need to parse the data in your script (i.e count amount of pages each user is subscribed to, or amount of users subscribed to 1 page).
Unsubscribing can be also much easier this way since you don't need to read the users or pages table at all. You simply delete the relation from the users_pages table.
Without it, you will need to (a) first read the users table (b) get the pages data comma separated (c) parse the data and remove the specific page from it (d) save the new data again to the database. That's 4 actions and 2 SQL queries...
I hope this helps!
I have created few tables such as date table which records the date and time of the attendance recorded, student table which records details of students and status which records present, absent or late.
I want to create a interface where a teacher can record the attendance easily and where MYSQL PHP language can be used.
I have being able to echo out all the list of the students from a certain class taught by specific teacher. Now I want to let the teacher to use checkbox system to select or deselect and submit and directly update into the database.
Can you please help me how can I start this process?
If you want more information then please let me know.
I want to create a interface such as attendance recording interface where I have create a table which involves student_id as foreign key from student table. Then a status table that records present, late or absent status and a date & time record that records the date and time the record was added.
When a user selects the checkboxes of three different types and clicks Submit then the PHP will send this information to the MYSQL saying Tim is present but Ashley is late recognized with their student_id so it means it will update the student’s record however I was think of using insert query command rather than update because new records might be added everyday.
So the formula or the query could be SELECT the checkboxes and insert status of the student WHERE student_id = the chosen column.
I am struggling on the right query to work out and do I have to use JavaScript to make it work or use a PHP function?
You need to understand how POST/GET resquests work (for interfacing between HTML and PHP side), and then how to call a MySQL request from PHP. Here you have some usefull examples:
GET/POST requests:
http://www.w3schools.com/tags/ref_httpmethods.asp
Interfacing MySQL with PHP (I recommend read all sections, intro, connection to database etc)
http://www.w3schools.com/php/php_mysql_intro.asp
MySQL insertion to database (this is the 'record' part you are looking for):
http://www.w3schools.com/php/php_mysql_insert.asp
Basically what you're asking for - "How can I make a nice slick HTML/CSS interface that uses PHP/MySQL on the backend to perform these operations - CREATE, RETRIEVE, UPDATE, DELETE"
Things you can learn in sequence -
Acccessing MySQL DB from PHP and populate into HTML table, you can generate the checkboxes too.
Make the table look cool with some CSS
And communicate between the server and client side using form submissions, using GET/POST
This is just a general guidance to get you started. Do not land up using frameworks or libraries immediately if you wan't to be in this field in the long run.
Welcome to webdev. :)
I'm sorry if this a stupid question, but I am new to this. I want to store all the information from all my customers' different contact forms in a database so they can retrieve the information later. Each contact form has a different number of fields. Ideally I'd store all of this info in one MYSQL table, but as I said each customer's contact form has a different number of fields.
I was thinking of creating a database with the following fields
ID, customerID, dateTime, data
The data would be the html of the contact form. However I am sure that's not best practice. Also it wouldn't allow me to offer a download option to the customer of all their contact forms as a csv or similar.
I don't want to have a different table for each customer either, and I am sure having a table with 'field1', 'field2', 'field3' etc isn't that good either.
Any ideas?
If you don't need it to be relational, that would be fine. You wouldn't be able to query it very easily.
You could store the answers in another table using keys to link back to the form and field.
Or use a KeyValue store type DB like MongDB, and you could store all your data without worrying about the schema.
Make another table called user_data. In it, have:
id, user_id, data
Then, if they fill out a form with 10 fields, you can enter 10 entries into this table. When you pull data for the user, just also pull any data from the user_data table that has user_id = their id.
If you need more than that, you could also add a "field_name" field or something:
id, user_data, field_name, data
You can use the MySQL database to have a schema for your desired information that relates many tables to one another.
Here is another discussion about database design:
Database Design Best Practices
If you must have only one table, the other choice I would mention is to have the ID created automatically, and then make the fields be not required. This way when a customer fills out one form before the other it won't mess you up. You can then store the ID as a Session Variable and reuse it for different INSERT statements to your database.
You can make a download function for the customer that will output in the desired format that queries your table and then generates the file.
I have a MySQL database where I am storing information that is entered from a PHP web page. I have a page that allows the user to view an existing row, and make changes and save them to the database. I want to know the best way to keep the original entries, as well as the new update and any subsequent updates.
My thought is to make a new table with the same columns as the first, with an additional timestamp field. When a user submits an update, the script would take the contents of the main table's row, and enter them into the archive table with a timestamp when it was done, and then enter in the new values to the main table. I'd also add a new field to the main table to specify whether or not the row has ever been edited.
This way, I can do a query of the main table and get the most current data, and I can also query the archive table to see the change history. Is this the best way to accomplish this, or is there a better way?
You can use triggers on update, delete, or insert to keep track of all changes, who made them and at what time.
Lookup database audit tables. There are several methods, I like the active column which gets set to 0 when you 'delete' or 'update' and the new record gets inserted. It does make a headache for unique key checking. The alternative I've used is the one you have mentioned, a separate table.
As buckbova mentions you can use a trigger to do the secondary insert on 'delete' or 'update'. Otherwise manage it in your PHP code if you don't have that ability.
You don't need a second table. Just have a start and end date on each row. The row without an end date is the active record. I've built entire systems using this method, and just so long as you index the date fields, it's very fast.
When retrieving the current record, AND end_date IS NULL gets added to the WHERE clause.
In this situation, I would recommend you to consider all properties in one table after adding it few columns:
active/ not active
ID of the person who kept these parameters
timestamp of adding