Ajax Update only on the selected line

0

I have a DataTable here and I'm using a plugin so the user can edit the table data, I got it in parts (haha), the problem is that my call is not identifying what the user is entering, and when I define manually the cell value it changes on all rows of that column in db, I guess I need to identify the line ID that the user is editing but I do not know how to do it.

Below my Ajax Call:

    $.ajax({
        url: "include/edita.php",
        type: "POST",
        data: { 'coletadora': 'Definição Manual'},                   
        success: function(data){
            alert(data);                                    
        }
    });
It is also relevant the content of my edita.php (ignore the php lagged, was a long time away from php and I need this system running before transitioning to PDO), I know I need a WHERE to limit the update to this determined ID we are going to find out:

    <?php
$host= 'localhost';
$bd= 'minhadb';
$userbd = 'meuusr';
$senhabd= 'meupw';



//Conectando com banco MySQL
$conexao = mysql_connect($host,$userbd,$senhabd);
if (!$conexao)
    die ("Erro de conexão com localhost, o seguinte erro ocorreu -> ".mysql_error());
//Conectando com a tabela do banco de dados
$banco = mysql_select_db($bd,$conexao);
if (!$banco)
    die ("Erro de conexão com banco de dados, o seguinte erro ocorreu -> ".mysql_error());
$coletadora   = $_POST["coletadora"];
$sql = "UPDATE minhadb SET coletadora = '$coletadora'";

mysql_query($sql,$conexao);
?>

Thank you in advance for the help!

    
asked by anonymous 10.05.2017 / 14:51

1 answer

0

That's why you did not tell us, through the primary key or another unique value, which line should be changed in the database.

An exit to this may be the inclusion of the 'id' of the changed line in the request.

For example: (taking into consideration that you use jQuery)

<td data-id="1" data-coluna="coletadora">VALOR DA CÉLULA</td>

When your user submits the changes, you must include both the column and the new value as well as the "identifier" of that row, which in this example would be the id.

Ex:

$.ajax({
        url: "include/edita.php",
        type: "POST",
        data: { 
                $('tdQueOUserAlterou').data('coluna') : 
                $('tdQueOUserAlterou').text(),
                'id' : $('tdQueOUserAlterou').data('id')
        },                   
        success: function(data){
            alert(data);                                    
        }
    });

Then the SQL query could be changed to:

$id = $_POST['id'];
$sql = "UPDATE minhadb SET coletadora = '$coletadora' WHERE 'id'='$id'";

This WHERE tag is what defines which line to change. You want to set the collector equal to that value ONLY where the id is equal to that reported. Remember that there can be any single field in your table, not just id.

    
10.05.2017 / 19:16