DELETE function with php and mysql

Asked

Viewed 4,496 times

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 should I turn the delete script into a function, and then call on the link?
I want to do this because on the page deleta.php I already have another script, and the two are giving conflicts. With this, I decided to turn the script into a function and call it on the link. How to act

  • you can delete more than one record per link?

  • No, with a checkbox in another part of the code.

1 answer

2


Create a function it can take two arguments, the first is the connection and the second the id to be deleted, make the validation if input sent by the user is number, if positive effect delete and return true, takes the return of the function and redirects the user in case of success.

In the link, you should continue to call the file delete.php and in it 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';
}
  • And how would my link call the function? Would the function structure look like this?: Function deleta($connects, $Row['id']) {...} and the link: href="deleta.php? id=<? php include('config.php'); Function deleta($connects, $Row['id']); ?>

  • @Thiagobarros looks the same

  • How do I turn it into function then? It is that in the page deleta.php has the script to delete by id, and just below has a script that works with a checkbox, which I use to delete several records at once..

  • I left without turning into function and tried to delete, even if there are both scripts. When I tried to delete, it worked, but instead of redirecting, I got the message: Erreur de syntaxe pr s de '' la ligne 1. When trying to use the script below, it didn’t work. Is it better to create a page for each script?

  • It seems to be bank error, the ideal that delete.php only have php code and no html.

  • There is no HTML. I will update the post with my full deleta.php. What I want is to turn the first script that deletes by ID into a function and call it in the link.

Show 1 more comment

Browser other questions tagged

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