Answer the question
In order to leave comments, you need to log in
How to edit a database in a loop?
Good day, there was such a problem. The connection to the database is made using RedBeanPHP
. I have a database called bugs , it has fields: id, login, date, text, done, and so on.
By default , done is set to false
.
The output occurs using the foreach loop
. The output is shown in the picture:
What is the actual problem, I want that when I click on the "Problem solved" button (name = bug_done) , in the database for this particular form done changes to true .
I know how to change, but when I do that when I click on the "Problem solved" button, in the database done changes to true, then it changes only for the last ID that was displayed. For example:
Here is the given image. We have 3 forms in which information is displayed. If I click on the "Problem solved" button in any of the forms, then it changes only for ID:3 , since it is the last one in the database.
Here is the code to execute the replacement:
$bugs = R::findAll('bugs');
$data = $_POST;
if (isset($data['bug_done'])) {
$bugs->done = 'true';
R::store($bugs);
}
Answer the question
In order to leave comments, you need to log in
option 1:
Enclose each of the blocks in a tag , inside create the corresponding hidden fields. When submitting, you will receive the entire data set, according to which you can already write something to the database. At you apparently one form on all list. Therefore, all the data gets into the handler, but since the fields are duplicated by name, the last received data is taken into account, that is, the lowest fields. Which is what happens to you, the last set changes the state.
Option 2: the principle is the same as in option 1, only through javascript to catch the pressed button and from this already submit the necessary fields, for example, using Ajax. The option is more difficult for a beginner, and generally not very popular.<form></form>
Option 3: a mixture of 1 and 2 - when submitting the form, catch the event with javascript and send the data to the server in the form of serialized data via Ajax. Based on the result of the response from the server, change the state of the button with javascript to "ready", or whatever you have, and disable the button.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question