About updating database with PDO

Hello,

<form>

</form>

Editing and updating a table within a form is no problem
But editing two tables in one form is ok but how do we update the database table;

 if ($stmt->rowCount() > 0) {
   $messages[] = "Successfully Updated";
 } else {
   $errors[] = "Update Failed Because No Changes Made to Data";
 }

The first table is the main table, where a lot of data is organized.
The other second table is editing only one data 0/1 yes/no

If there is a change in the data of both tables, there is no update problem.
However, only the first main table may need to be edited or just the other second table may need to be edited
What to do in such a situation?

We really need more code to really understand the problem, but you would join the tables together to update using a query statement. It would also be nice to know the structure of your database table.

Adem, when you show your form and it is from a previously inserted or updated database table,
you should set all of the checkmarks check boxes, to their correct values. Then, you can just
update all of the data as they will contain the old data if the user has not altered them.

Strider, his structure is HUGE and hard to follow. I think his structure and code will be confusing at first.
This project of Adem’s has been going on for months and is very complex. But, if you want to look at it,
you can review his previous posts. He is using a ton of complex displays and tables and JS.
Just FYI…

1 Like

This is sample first table code (base table)

$data = [
    'column' => $column,
    'column2' => $column2,
    'column3' => $column3,
    'id' => $id,
];
$sql = "UPDATE table1 SET column=:column, column2=:column2, column3=:column3 WHERE id=:id";
$stmt= $pdo->prepare($sql);
$stmt->execute($data);

This is the sample second table (for the red framed area in the image)

$sql = "UPDATE table2 SET kdv = ? WHERE id = ?";
$pdo->prepare($sql)->execute([$kadv, $id]);

I am using the following code on every table update

I want it to treat the user as if it is a single table
Changes Updated Successfully
OR
Update Failed Due To An Error
OR
Update Failed Because No Changes In Data

 if ($stmt->rowCount() > 0) {
   $messages[] = "Successfully Updated";
 } else {
   $errors[] = "Update Failed Because No Changes Made to Data";
 }

Edit screenshot
The area framed in red belongs only to the second table

Well, start with the bottom one first. You would need to read the database for this item. Compare it to all of the inputs from the user. If all are the same display the notice that no changes were made.

Middle one next. You would need to read all of the inputs from the user and validate each one, one-by-one to see if they fit the correct data. If any inputs do not pass the validation, then display a notice that there was an error and display the error.

Then, if these two are not displayed, then the data was changed and all inputs were validated and therefore you can update the data to the database.

With all of your inputs, it will take some time to process this list. You can make a table of all of the fields involved, an array of field names. This would make it easier, but, the field names in the forms would need to match the field names in the database. Then, you could just loop thru the array of field names and check if each has changed or is not valid data. If all field names pass thru with changes and are all valid, then update them. Or you could build a query that updates just the changed field names.

Hope those ideas help…

I am very very sorry
This affects the setting I want to make, the global settings, I never thought of that
If I do this I’ll be doing it wrong
I should set this setting to be valid only for the relevant quote.
If I had done the above, x dealers would have been authorized to set the global setting.That would be a huge mistake I never thought

Again, I’m sorry I kept you busy.

No problem at all. I am here to help.

1 Like
Sponsor our Newsletter | Privacy Policy | Terms of Service