samedi 13 août 2016

Delete rows with ajax and php

Is this a good way, to delete all the selected rows from sql table?

The code works fine, but maybe there other or better ways to do this. The real_ escape_string function is in the good place, or i should put in in the foreach loop, before the sql query?

$('.deleteRows').click(function(e)
{
    e.preventDefault();
    var val = [];
    if(confirm("Biztos, hogy törölni szeretné a kijelölt sorokat?"))
    {
        $(':checkbox:checked').each(function(i){
          val[i] = $(this).val();
        });
        $.ajax({
            data: { val:val },
            type: 'POST',
            url: 'files/delete_all_uzenet.php',
            success: function(data) 
            {
                var result = $.trim(data);
                $('#newsletterResult').html(data);
                $('#newsletterModal').modal('show');
            },
            complete: function()
            {
                setTimeout(function() 
                {
                    location.reload();  
                }, 4000 );  
            }
        });
    }
    return false;
});

Php file:

<?php
include_once("../../files/connect.php");
if(isset($_POST['val']))
{
    foreach($_POST['val'] as $v)
    {
        mysqli_query($kapcs, $sql = "DELETE FROM kapcsolatfelvetel WHERE kapcsolat_id = '".mysqli_real_escape_string($kapcs, $v)."'") or die(mysqli_error($kapcs));
    }
    echo 'Rows deleted';
}
else
{
    exit("No rows selected.");
}
?>




Aucun commentaire:

Enregistrer un commentaire