How to save data from a grid that has changed?

6
  

My system has a grid with order items coming from the database.   data.

Well, I need to change the order items, add one more item, or even remove. Get the information in json and put it on the grid. I can interact in JsonStore , which is an array of items, I remove and add normally, but this locally.

Well, once the budget items have been edited, I need to send the edits to the database.

At this point I need help.

  

Is there a modeling standard in sql to perform this procedure?

     

What is the best way? Deleto everything in the bank related to the items of the   budget and re-enter? Do I update the items?

If you need more clarity in the question please let me know.

    
asked by anonymous 06.02.2016 / 15:03

5 answers

2

The solution that I propose, is not based on your language (php) but on your problem.

For your printscreen, I understood that 1 Order has N Product Order Item (containing product code, quantity ...) containing 1 reference to the Product. We are talking here about 1 - > N - > 1

Every time you send the data to the server, you must submit and retrieve the Order id, and from there, retrieve all the ProductData Item .

Product Order Item Update

You should use the id of them that will come from a hidden field to make the comparison with what is already registered in the bank.

Removing a Product Order Item

Compare the Product Order Item that came from the screen with the database, when a particular record does not come from the screen, it is why it was removed, so just make a delete.

Adding a Product Order Item

If a ProductData item comes from the screen without the id, it means that it is a new record, so it will only be necessary to do the insertion.

Changes to the Request must be atomic (must be carried out in the same transaction) as I believe that by example, you will have to validate if a certain quantity of products are in stock.

    
11.02.2016 / 14:20
1

Using an auxiliary variable in your favor

Create a list that defines the behavior that should be done in JSON itself in each record.

Any record previously sent would have, say, 0, saying it is a pre-existing data not changed.

Have you entered a new record? Assigns another value, such as 1. Here the server knows that it has to execute a INSERT INTO

Have you deleted a record? Oops, but the guy may have deleted something that is not even on the server yet ... so basically

se(action = 0)entao
  definir action deste registro 2, para executar DELETE do lado do servidor
senao
  só apaga do JSON, não envia pro servidor

The same thing goes for an edit. If the person edit a record that has not yet been inserted into the server you will keep the 1 but will update the data inside the JSON. If the registry existed previously there you set a 3, so that a UPDATE is executed in the database.

This will prevent you from having to make 1 SELECT for each record, almost nothing increases the client-side effort, greatly reduces the problem on the server side. (You'll just have to add a switch on the server side if it's already ready)

If you want to further reduce the server-side problem, you can try to filter JSON and send everything that is not 0, so that the server is only in charge of changing the database, not having to keep checking what records were or unaffected.

    
11.02.2016 / 21:03
0

So, I've done a budget system, and to change individually, you have to make a form for each line, interesting also make an individual submit button for each one also, both delete and change. Ajax will be able to do what you want with the data of the post, you can try a little more and do with AJAX, the application gets much better, without reload, if it no longer uses. Ai, from POST, you can change in the database. Answering your questions, to change in the bank only with same UPDATE, method simpler and faster. You do not have to delete all the information, just change the line you want, as I said before.

    
09.02.2016 / 05:17
0

With UPDATE , it is faster since data is being rewritten on existing rows. With DELETE you would have to erase and then re-insert and this takes more time, lines of code and performance. In addition, you can use the MULTI_QUERY PHP statement ( link ) with the UPDATE to update all data with a request and further improve performance.

    
10.02.2016 / 23:37
0

You will need to have little knowledge of jquery / js.

1 ° Create 2 array variables.

var deleteID = new Array();
var updateID = new Array();

You will create an onchange event in all fields of the grid, which when changed will insert the following key into the array, field = value,

3 ° When you make a change in the product, the onchange will send to the array updateID with product ID and the field that is supposed to change example Quantity: 20.

4 ° To delete you will create a button to delete X in each row of the table and insert a button with action onclick = 'deleteProduct (iddproduct)' or can do checkbox; within the function you add so

function deletarProduto(iddoproduto) {
    deleteID.push(iddoproduto);
}

At the end you will have the Save / Update button in this action you send the data of the 2 Arrays to the backend. Then the logic will be more or less like this.

<?php
  $acao_deletar = $_POST['deletar'];
  $acao_update  = $_POST['update'];

  // Se existir alguma ação de deletar 
  if(!isset($acao_deletar) {
     $deletar_ids = implode(",",$acao_deletar);
     mysql_query("DELETE FROM tb_produto WHERE ID IN (".$deletar_ids.")");
  }

  // Se existir alguma ação de update 
  if(!isset($acao_update) {
     foreach($acao_update as $update) {
        mysql_query("UPDATE FROM tb_produto SET $update[campo] = $update[valor] WHERE ID = $update[id]");
     }
  }


?>

This code above is just an example to open your mind a little hope I have helped.

    
15.02.2016 / 18:45