PHP Query无法使用DELETE

时间:2017-12-08 07:55:38

标签: php mysql mysqli

我有连接,并且我不会通过选择ID删除一条记录(选项具有db中行ID的值)

<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>

没有做到这一点:/我选择选项,点击提交名称= Del后,它不会工作,只是重置到正常位置。 (向db添加信息并从中显示效果很好)

3 个答案:

答案 0 :(得分:0)

请将此更改为:

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);

我遇到过PHP只注意变量的情况,如果它们直接包含在双引号中而不是单引号中。另一种选择是使用字符串连接,以便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);

答案 1 :(得分:0)

主要问题是您没有使用表单标记设置method="post"。所以把它放在那里尝试一下。

建议:

像这样查询您的查询:

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

答案 2 :(得分:0)

试一下

您错过了表单标记中的方法。 另外,我更改了SQL查询

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

下面的代码正在处理我的系统。您必须使用SQL injection进行安全

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

设置删除记录,在数据库中创建类似状态的列并更改状态0或1.

您可以参考该网站删除记录

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>