Answer the question
In order to leave comments, you need to log in
Generic method for updating data in a database?
Good day, dear Toaster users. (If, of course, you solve my question, you will be respected)
I am writing a class, which is an admin panel through which you can add, delete and edit information in the database. I have two schemas and I want to make a universal way to update data in this schema.
The columns are different everywhere.
The method itself, which is the generated request.
This is the first diagram.
And here is the second one, there are more columns here.
And here I was thinking... I don't know which column the user wants to change. Help me to correctly generate query for SQL.
Answer the question
In order to leave comments, you need to log in
Who prevents you from collecting sql on the fly? You accept a query as a field-value array and generate the corresponding query.
The approach is normal, but the names of the table and all fields must be hardcoded in the class description .
Names of fields in the elementary case a case stupidly to thrust in an array.
After that, you can use them to form dynamic queries.
Like this: https://phpdelusions.net/pdo_examples/dynamical_update
$allowed is your list of fields.
It is better not to use $_POST directly, but to pass a copy to the method, but the principle is the same - we take only filled fields from the post and add them to the request.
The Update method will be on the ancestor class, from which classes will be inherited to work with individual entities, such as article. Thus, in the Article class, you will not have the Update method at all, but at the same time you will write
and everything will be fine.
I already see that your SQL query is 100% vulnerable to SQL Injection.
Screw just RedBeanPHP as a daughter-son and that's it.
Locking:
1. Exclusive (prohibition of any transactions):
2. Shared (leave others to read): https://dev.mysql.com/doc/refman/8.0/en/innodb-loc...SELECT ...... FOR UPDATE
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question