1

I should delete values ​​from the database but end now, the only result that I managed to get is to make a certain value null, and then this then becomes invalid, but if I want to repeat the process I can no longer do it. My goal would be to freely delete all the products I want from a given event, place the code for deletion:

<a href="#elimina<?php echo $row['impianto_id_campagna']; ?>" 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 

modal:

<div class="modal fade" id="elimina<?php echo $row['impianto_id_campagna']; ?>" 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 impianto_id_campagna='".$row['impianto_id_campagna']."'"); $row_due=mysqli_fetch_array($elimina); ?> <div class="modal-body mx-3"> <h5 style="text-align: center;">Eliminare l'impianto dalla campagna?</h5> </div> <div class="modal-footer d-flex justify-content-center"> <a href="eliminainfo.php?impianto_id_campagna=<?php echo $row['impianto_id_campagna']; ?>" class="btn btn-danger"><span class="glyphicon glyphicon-trash"></span>Elimina</a> </div> </div> </div> </div> 

delete_page:

<?php include '../../connessione.php'; $impianto_id_campagna=$_GET['impianto_id_campagna']; mysqli_query($connessione,"UPDATE campagne_cliente SET impianto_id_campagna = NULL WHERE impianto_id_campagna = '$impianto_id_campagna'"); header('location: campagne.php'); ?> 

my db structure: my db structure

in the picture you will see a value set to 0, but if I later want to set another value to 0, I can not do it, the maximum would be to delete the whole row, starting from the id_impianto_campagna

2
  • 3
    I juste want to point out that putting $impianto_id_campagna directly inside the query without escaping is open for sql injection. Proper sanitization or prepared statement should be used. Commented Nov 3, 2018 at 20:43
  • As said by another user, your code is open to SQL injections, I updated my anwser below to show you something that you could use to prevent this. Commented Nov 3, 2018 at 20:58

2 Answers 2

4

You're using an UPDATE query where you should be using a DELETE query instead.

It should work if you replace this:

"UPDATE campagne_cliente SET impianto_id_campagna = NULL WHERE impianto_id_campagna = '$impianto_id_campagna'"; 

With this:

"DELETE * FROM campagne_cliente WHERE impianto_id_campagna = '$impianto_id_campagna'" 

As said by other users, your code is open to SQL injections, use prepared statements instead.

// ... Set your connection variables $conn = new mysqli($host, $user, $password, $database); // Query $sql = "DELETE * FROM campagne_cliente WHERE impianto_id_campagna = ?"; $stmt = $conn->prepare($sql); if(!$stmt) { // ... Couldn't prepare statment, handle your error } $id = $_GET['impianto_id_campagna']; if(!$stmt->bind_param('i', $id)) { // ... Couldn't bind parameters, handle your error } if(!$stmt->execute()) { // ... Couldn't execute query, handle your error } else { echo 'Record deleted.'; } 
Sign up to request clarification or add additional context in comments.

Comments

-1

If you are deleting the record against primary key then replace:

"UPDATE campagne_cliente SET impianto_id_campagna = NULL WHERE impianto_id_campagna = '$impianto_id_campagna'" 

with:

"DELETE FROM campagne_cliente WHERE impianto_id_campagna = $impianto_id_campagna" 

and check it again.

1 Comment

Your answer could be improved by describing what your code does and why it solves the problem.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.