A
A
Alan2019-09-22 21:30:02
PHP
Alan, 2019-09-22 21:30:02

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.
5d87bd40330e7073893580.png
The method itself, which is the generated request.
5d87bd58aff2c574385258.png
This is the first diagram.
5d87bd737cfc9280088903.png
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

5 answer(s)
M
McBernar, 2019-09-22
Bogov @AVAVION

Who prevents you from collecting sql on the fly? You accept a query as a field-value array and generate the corresponding query.

F
FanatPHP, 2019-09-22
@FanatPHP

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.

R
Randewoo, 2019-09-22
@Randewoo

I already see that your SQL query is 100% vulnerable to SQL Injection.
Screw just RedBeanPHP as a daughter-son and that's it.

X
xmoonlight, 2019-09-22
@xmoonlight

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

N
Northern Lights, 2019-09-23
@php666

google ORM, ActiveRecord

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question