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.

9
  • For modal forms using ajax is better way. Commented Apr 26, 2015 at 14:11
  • hmmm... ajax, i'll see what i can do. For now i wanna to fix this. Thanks... Commented Apr 26, 2015 at 14:31
  • Try my solution and be sure $_GET["id"] is not null. Commented Apr 26, 2015 at 22:16
  • @Kowts Are you sure mysql is supported? What error do you get? Commented Apr 26, 2015 at 22:25
  • 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. Commented Apr 27, 2015 at 2:33

2 Answers 2

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

Sign up to request clarification or add additional context in comments.

1 Comment

Actually there is also delete method, most frameworks do it with post & and a hidden method, check it out.
1

Change this line :

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

to

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

3 Comments

How is that different? PHP will interpolate $id in the first one, so there's no need for an explicit append.
@EngineerCoder... still the same thing, nothing change but thanks.
Check the $id variable for exist or not; if exist proof that are there any matched id in your database.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.