Failed to attempt to delete record in database

Asked

Viewed 60 times

0

I am trying to delete a record from my database after deleting physical file from a certain folder, physical file deletion occurs as expected but the one from the database fails, what I did was this:

if ($_POST["Operacao"] == 'ExcluirUpload') {

$sql = "SELECT * FROM gerDoctoContrato WHERE IdDocumento = ? AND IdContrato = ?";

if($stmt = $conn->prepare($sql) ){
    $stmt->bind_param(
        "ii",
        $_POST["IdDocumento"],
        $_POST["IdContrato"]            
    ); 


    if ($stmt->execute()) {
        $data = array();    
        $result = $stmt->get_result();
        while ($row = $result->fetch_assoc()) {

            $CaminhoAnexo = $row['CaminhoAnexo'];
            $NomeArquivo = $row['NomeArquivo'];     

            unlink($CaminhoAnexo.$NomeArquivo);

        }

        $sqlUp = "DELETE FROM gerDoctoContrato WHERE IdDocumento = ? AND IdContrato = ?";
        if($stmts = $conn->prepare($sqlUp) ){           
            $stmst->bind_param(
                "ii",
                $_POST["IdDocumento"],
                $_POST["IdContrato"]            
            );  
        }

        $aretorno["msg"] = "Registro excluído com sucesso.";
        $aretorno["par"] = $_POST["IdContrato"];

    } else {
        $aretorno["msg"] = "Ocorreu um erro na exclusão dos dados: " . $stmt->error . ". Verifique.";
        $aretorno["status"] = "ERRO";
    }

} else {
    $aretorno["msg"] = "Ocorreu um erro na preparação dos dados: "  . $stmt->error . ". Verifique.";
    $aretorno["status"] = "ERRO";
}

}

On my console appears this:

Uncaught TypeError: e.unbind is not a function
  • what error appears?

  • It is used to be a referential integrity error, that is, this document is being linked in another table , its code is inside another table. Check if it is. If this is to solve you need to delete everything that has relation or null arrow in place to enable deletion.

  • e.unbind is not a function this looks like javascript error and not php error.

  • I made a test commented the excerpt that should make the deletion and the script works, of course, not deleting the BD record.

  • Which error appears? as it appears, the javascript event failed and does not call the php file to delete the record.

1 answer

1


I managed to solve, I changed the deletion script for one that I have working in another script and is now deleting, at the end, right after attaching the variables with bind_param() the commands $stmts->execute(); and $stmts->close(); were executed, so:

if ($_POST["Operacao"] == 'ExcluirUpload') {

$sql = "SELECT * FROM gerDoctoContrato WHERE IdDocumento = ? AND IdContrato = ?";

if($stmt = $conn->prepare($sql) ){
    $stmt->bind_param(
        "ii",
        $_POST["IdDocumento"],
        $_POST["IdContrato"]            
    ); 


    if ($stmt->execute()) {
        $data = array();    
        $result = $stmt->get_result();
        while ($row = $result->fetch_assoc()) {

            $CaminhoAnexo = $row['CaminhoAnexo'];
            $NomeArquivo = $row['NomeArquivo'];     

            unlink($CaminhoAnexo.$NomeArquivo);

        }

        $sqls = "DELETE FROM gerDoctoContrato WHERE IdDocumento = ? AND IdContrato = ?";
        $stmts = $conn->prepare($sqls);
        $stmts->bind_param(
        'ii', 
        $_POST['IdDocumento'],
        $_POST['IdContrato']);
        $stmts->execute(); 
        $stmts->close();

        $aretorno["msg"] = "Registro excluído com sucesso.";
        $aretorno["par"] = $_POST["IdContrato"];

    } else {
        $aretorno["msg"] = "Ocorreu um erro na exclusão dos dados: " . $stmt->error . ". Verifique.";
        $aretorno["status"] = "ERRO";
    }

} else {
    $aretorno["msg"] = "Ocorreu um erro na preparação dos dados: "  . $stmt->error . ". Verifique.";
    $aretorno["status"] = "ERRO";
}

}

  • What is the main difference between them?

Browser other questions tagged

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