DELETE function with php and mysql

1

deleta.php:

<?php
// quero transformar esse script em função e chama-lo no link
// deleta um unico post
        include("config.php");
        $id = $_GET['id'];  
        $sqlInsert = "DELETE FROM conteudo WHERE id = '$id'";   
        $deletaPost = mysqli_query($conecta,$sqlInsert);

    if($deletaPost) {
            header("Location: ".$_SERVER['HTTP_REFERER']."");
            exit;
    } else {
        echo mysqli_error($conecta);
    }

 // o script abaixo deleta através de um checkbox, e elimina varios de uma vez.
// quero que esse seja o padrão, e o script acima seja uma função que só funcione quando chamada.
    if( !empty( $_POST['deletar'] ) ) {
       $groups = array_chunk( $_POST['deletar'], 50 );
       foreach ( $groups AS $group ) {
    $group = implode('\',\'', $_POST['deletar']);
            $query = 'DELETE FROM conteudo WHERE id IN (\''. $group .'\')';
           $deleta = mysqli_query($conecta, $query);
          // executa a query
       }

        if($deleta) {
           header("Location: ".$_SERVER['HTTP_REFERER']."");
           exit;
    } else {
        echo mysqli_error($conecta);
    }

    } else {
        header("Location: ".$_SERVER['HTTP_REFERER']."");
           exit;
    }

    ?> 

HTML:

<a href="deleta.php?id=<?php echo $row['id']; ?>">Excluir</a>  

How do I make the delete script into a function, and then call the link?  I want to do this because in the page deleta.php I already have another script, and the two are giving conflicts. With that, I decided to turn the script into function and call it on the link. How to act

    
asked by anonymous 19.12.2015 / 22:15

1 answer

2

Create a function it can receive two arguments, the first is the connection and the second is the id to be deleted, validate if it is sent by the user is number, if positive delete it and return true takes the function return and redirects the user on success.

In the link, you should continue to call the delete.php file and call the function.

delete.php

include 'conexao.php';
function delete($conexao, $id){
   //código a ser desenvolvido
}

$id = isset($_GET['id']) && ctype_digit($_GET['id']) ? $_GET['id'] : null;
if(delete($conexao, $id)){
   header('delteSucesso.php');
}else{
   echo 'erro ao excluír o registro';
}
    
19.12.2015 / 22:26