Search code examples
phpmysqlinner-join

Delete * from two tables inner join


hello to all I have created in my project two tables that have the same id, the structure of the tables is this:

campagne:

id_campagna

data_inizio

data_fine

and table campagne_cliente

id_campagna_cliente

cliente_id_campagna

impianto_id_campagna

my will is to delete all the data contained in the two tables when the id

id_campagna

id_campagna_cliente

are the same.

also place the elimination procedure, from the table where they are shown all the data I have to select: id_campagna_cliente

in this way:

<a href="#elimina<?php echo $row['id_campagna_cliente']; ?>" data-toggle="modal" class="btn-floating btn-sm btn-pin "><span class="glyphicon glyphicon-trash"></span> <i class='fa fa-remove' aria-hidden='true'></i></a>

this is the modal:

<div class="modal fade" id="elimina<?php echo $row['id_campagna_cliente']; ?>" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
    <div class="modal-dialog" role="document">
        <div class="modal-content">
            <div class="modal-header text-center">
                <h4 class="modal-title w-100 font-weight-bold">Elimina Impianto</h4>
                <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                    <span aria-hidden="true">&times;</span>
                </button>
            </div>

            <?php

             $elimina=mysqli_query($connessione,"select * from campagne_cliente where id_campagna_cliente='".$row['id_campagna_cliente']."'");


             $row_due=mysqli_fetch_array($elimina);

            ?>


            <div class="modal-body mx-3">

            <h5 style="text-align: center;">Eliminare Campagna di:&nbsp;<?php echo $row_due['nome'].'&nbsp;'?>?</h5>

            </div>
            <div class="modal-footer d-flex justify-content-center">
                <a href="elimina.php?id_campagna_cliente=<?php echo $row['id_campagna_cliente']; ?>" class="btn btn-danger"><span class="glyphicon glyphicon-trash"></span>Elimina</a>
            </div>
        </div>
    </div>
</div>

and this is the delete page from which I recover with $ _GET: id_campagna_cliente

<?php

include '../../connessione.php';

$id_campagna_cliente=$_GET['id_campagna_cliente'];

mysqli_query($connessione,"DELETE * FROM campagne 

LEFT JOIN campagne_cliente 

ON campagne.id_campagna=campagne_cliente.id_campagna_cliente 

WHERE campagne.id_campagna = '$id_campagna_cliente'");

header('location: campagne.php');



?>

but when I run delete it does not happen anything like it? I would like to delete all the data contained in these two tables


Solution

  • If you want to delete from multiple target tables using a delete join, then you need to specify all tables or aliases which you want to delete. Something like this should work:

    DELETE ca, cc
    FROM campagne ca
    LEFT JOIN campagne_cliente cc
        ON ca.id_campagna = cc.id_campagna_cliente 
    WHERE
        ca.id_campagna = ?;
    

    You would then bind some value for id_campagna in your PHP, ideally using a PHP prepared statement.

    Note that DELETE * is not valid as you were using it; only aliases are allowed to follow DELETE, and then only in the context of a multi-table delete.