简体   繁体   中英

PHP Query won't work with DELETE

I have connection, and I wan't delete one record by choosing ID (option have value of row ID in db)

<form class="form">
      <?php
      require "connect.php";
      $select = $_POST['del_zaint'];
      if(isset($_POST['Del'])){
        $$que = "DELETE FROM `zainteresowania` WHERE `zainteresowania`.`id` = '".$select."'";
        mysqli_query($db, $que);
      }
      mysqli_close($db);
      ?>

      <span class="main-page__info">Usuń rekord zainteresowań.</span>
      <select name="del_zaint">
        <option disabled selected>Wybierz rekord do usunięcia</option>
        <?php
        require "connect.php";
        $que = "SELECT * from zainteresowania";
        $wynik = mysqli_query($db, $que);
        while($row = mysqli_fetch_array($wynik)){
          echo "<option value=".$row['id'].">"."[".$row['id']."] ".$row['zainteresowanie']."</option>";
        }
        mysqli_close($db);
        ?>
      </select>
      <input name="Del" type="submit" value="Usuń">
    </form>

Nothing is done by this :/ I choose option and after clicking submit with name = Del, it won't work, just reset to normal position. (Adding informations to db and showing from it works well)

Please change this to:

require "connect.php";
$select = $_POST['del_zaint'];
if(isset($_POST['Del'])){
  $que = "DELETE FROM `zainteresowania` WHERE `zainteresowania`.`id` = $select";
  mysqli_query($db, $que);
}
mysqli_close($db);

I have had cases where PHP only notices variables if they are included directly in double quotes and but not single quotes. The other option is to use string concatenation so that PHP knows where the variable is.

require "connect.php";
    $select = $_POST['del_zaint'];
    if(isset($_POST['Del'])){
      $que = "DELETE FROM `zainteresowania` WHERE `zainteresowania`.`id` = '".$select."'";
      mysqli_query($db, $que);
    }
    mysqli_close($db);

The main issue is you have not set method="post" with your form tag. So set it there and give it a try.

A suggestion:

Chage your query like this:

$que = "DELETE FROM `zainteresowania` WHERE `zainteresowania`.`id` = '".$select."'";  // Check the changes I made around $select.

Try This

You miss the method in the form tag. Also, I Change the SQL query

$que = "DELETE FROM `zainteresowania` WHERE id = '".$select."'";

Below code is working on my system. You have to use the SQL injection for secure

$db->real_escape_string($_POST['del_zaint'])

instated of delete the record, Make a column like status in the database and change the status 0 or 1.

You can refer the site for deleting the record

https://www.w3schools.com/php/php_mysql_delete.asp

 <?php
include('db/connection.php');
      if(isset($_POST['Del'])){

        echo $select = $db->real_escape_string($_POST['del_zaint']);
        $que = "DELETE FROM `zainteresowania` WHERE id = '".$select."'";
            if (mysqli_query($db, $que)) {
                  echo "Record deleted successfully";
            } else {
                echo "Error deleting record: " . mysqli_error($db);
            }
      mysqli_close($db);
  }
      ?>

<form class="form" method="POST" action="#">
      <span class="main-page__info">Usuń rekord zainteresowań.</span>

      <select name="del_zaint">
        <option disabled selected>Wybierz rekord do usunięcia</option>
        <?php
        $que = "SELECT * from zainteresowania";
        $wynik = mysqli_query($db, $que);

        while($row = mysqli_fetch_array($wynik)){
          echo "<option value=".$row['id'].">"."[".$row['id']."] ".$row['zainteresowanie']."</option>";
        }
        mysqli_close($db);
        ?>
      </select>
      <input name="Del" type="submit" value="Usuń">
    </form>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM