4

Can someone tell me what I'm doing wrong here? I create a table which has a delete button in each row, when clicked pops up an modal and asks if I want to delete the row. But when i click on the button nothing happens. Why is this happen?(I suspect it may be because of the INNER JOIN) Any ideas how to solve this?

list_book.php (INNER JOIN):

 <table class="table table-bordered table-hover" id="datatable">
    <thead>
        <tr> 
            <th>Número</th>
            <th>Estante</th>
            <th>Obra</th>
            <th>Autor</th>
            <th>Categoria</th>
            <th>Ano Escolaridade</th> 
            <th>Observação</th>
            <th class="text-center">Opções</th>
        </tr>
    </thead>
    <tbody> 
        <?php
        $query = "SELECT B.number, B.shelf, B.title, B.author, B.obs, C.category_name, S.scholarity_name FROM book AS B 
        INNER JOIN category AS C ON C.id_category=B.category_id INNER JOIN scholarity AS S ON S.id_scholarity=B.scholarity_id";
        $res = mysql_query($query);
        mysql_set_charset('utf-8');
        if (!$res) {
            echo "Erro ao executar a query";
        } else {
            while ($dados = mysql_fetch_array($res)) {
                ?>
                <tr>
                    <td><?php echo $dados['number']; ?></td>
                    <td><?php echo $dados['shelf']; ?></td>
                    <td><?php echo $dados['title']; ?></td>
                    <td><?php echo $dados['author']; ?></td>
                    <td><?php echo $dados['category_name']; ?></td>
                    <td><?php echo $dados['scholarity_name']; ?></td>
                    <td><?php echo $dados['obs']; ?></td>
                    <td class="text-center">
                        <a href="#" class="btn btn-warning btn-xs"><span class="fa fa-edit"></span>&nbsp;Editar</a>
                        <a class="btn btn-danger btn-xs" data-toggle="modal" data-target="#<?php echo $dados['id']; ?>" data-whatever="@mdo"><span class="fa fa-trash"></span>&nbsp;Apagar</a>
                        <?php include('modal_delete.php'); ?>
                    </td>
                </tr>
                <?php
            }
        }
        ?>
    </tbody>
</table>

MODAL(BOOTSTRAP):

<div class="modal"  id="<?php echo $dados['id_book']; ?>" tabindex="-1" role="dialog" aria-labelledby="mySmallModalLabel" aria-hidden="true">
  <div class="modal-dialog">
     <div class="modal-content">
        <div class="modal-header">
            <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
            <h4 class="modal-title text-left">Apagar</h4>
        </div>
        <div class="modal-body text-left">
            <p>Deseja apagar este  registro?</p>
        </div>
        <div class="modal-footer">
            <a href="../delete/delete_book.php?id=<?php echo $dados['id_book']; ?>" type="button" class="btn btn-primary">Apagar</a>
            <button type="button" class="btn btn-default" data-dismiss="modal">Cancelar</button>
        </div>
    </div>
</div>

delete_book.php

<?php
session_start();
require("../../conexao/conexao.php");

$id = $_GET["id"];
$query= "DELETE FROM book WHERE id_book = $id";
$delete= mysql_query($query);
    if(!$delete){
        echo "Erro!! Não foi possivel apagar dado.";    
    }
    else{
        echo"Dado removido com sucesso!";
            header("Location: ../list/list_book.php");
    }?>

I'm new on this so if something's wrong... please help me.

Kowts
  • 77
  • 3
  • 12
  • For modal forms using ajax is better way. – mcklayin Apr 26 '15 at 14:11
  • hmmm... ajax, i'll see what i can do. For now i wanna to fix this. Thanks... – Kowts Apr 26 '15 at 14:31
  • Try my solution and be sure $_GET["id"] is not null. – EngineerCoder Apr 26 '15 at 22:16
  • @Kowts Are you sure mysql is supported? What error do you get? – tika Apr 26 '15 at 22:25
  • so your last section called **PHP DELETE** is the `delete_book.php` content? – Alex Apr 26 '15 at 23:00
  • @tica... that's the problem, i'm not getting any error, nothing is working. The problem is the INNER JOIN(i think) but I can't fix it. – Kowts Apr 27 '15 at 02:10
  • @Alex... ya, that's right! – Kowts Apr 27 '15 at 02:11
  • 1
    try to remove everything from that file and leave just one line `echo 'This is delete_book.php page'`. and could you show your `list-book.php` content? because if your query is succesful you redirect user there. – Alex Apr 27 '15 at 02:33
  • @Alex... i have done what you said... i remove some piece of code in some pages and i found the problem. I replaced `SELECT B.number, B.shelf, B.title, B.author, B.obs, C.category_name, S.scholarity_name` for `SELECT *`... and everything works fine now. But i don´t know why? :/ – Kowts Apr 27 '15 at 11:22

2 Answers2

2

You must not do your delete request with GET , Create a form instead of anchor , and place a button inside it. make your request with post method

When you should use get & post request

Community
  • 1
  • 1
Mete Kabak
  • 332
  • 3
  • 19
1

Change this line :

$query= "DELETE FROM book WHERE id_book = $id";

to

$query= "DELETE FROM book WHERE id_book = ".$id;
EngineerCoder
  • 1,445
  • 15
  • 30