Remove with php line in mysql

Asked

Viewed 24 times

1

I have this function to send the array to the php file:

function deletar(){
    var ids = []; //arraypara armazenar os id's a serem deletados
    $(".colorir").each(function(){ //percorre todos os tr que possui a classe colorir
        ids.push($(this).find(".apagar").attr("Id")); //adiciona o id da linha ao array
        $(this).remove();
    })

    $.ajax({
        url: './deleteRAD',
        type: 'POST',
        cache: false,
        data: {ids:ids},
        error: function(){

        },
        success: function(result)
        { 
        }
    });
    //só fazer a chamada para remoção das linhas no php
    console.log(ids);
}

The value of ids sent by ajax is ["33,Rute"].

Then in the file of php do the delete:

$partes = explode(',',$_POST["ids"]);

$partes1 = $partes[0];
$partes2 = $partes[1];

$delete = "DELETE FROM centrodb.Alertas WHERE Id='$partes1'";
$result = mysqli_query( $conn, $delete);
if (false === $result) { echo mysqli_error(); }

But do not remove in the database and in the log file of the web server I get the following message:

inserir a descrição da imagem aqui

1 answer

1


Your $_POST['ids'] is an array where every item of it looks like the string '33,Rute', by what it seems you want to make a explode in an item of his or is the correct code would be:

$partes = explode(',', $_POST['ids'][0])
//                                   ^
//                                   índice do item

Imagining that you will pass more data in this array maybe you will need iterate between them, for this you can use for,foreach,... That is something like in the example below.

if( isset($_POST['ids']) && is_array($_POST['ids']) )
{

    foreach( $_POST['ids'] as $valor )
    {

        $partes = explode(',',$valor);

        print_r($partes);   /// para você testar
        #$partes1 = $partes[0]; 
        #$partes2 = $partes[1];

        #$delete = "DELETE FROM centrodb.Alertas WHERE Id='$partes1'";
        #$result = mysqli_query( $conn, $delete);
        #if (false === $result) { echo mysqli_error(); }

    }
}

The above code has not been tested.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.