简体   繁体   English

如何从生成的 php 表中获取 id 的值并删除具有该 id 的行

[英]How to take value of id from generated php table and delete row with that id

I have problem with delete function.我有删除功能的问题。 I am trying to delete specific row from table, to allow admin to delete row he wants, row with certain id.我正在尝试从表中删除特定行,以允许管理员删除他想要的行,具有特定 id 的行。 I think there is problem in my script with getting value of id.我认为我的脚本中获取 id 值存在问题。 With these two scripts, I get deleted all the values from table when I click on delete option in any row, so thats why I think that I am having problem with taking value of id.使用这两个脚本,当我单击任何行中的删除选项时,我会从表中删除所有值,这就是为什么我认为我在获取 id 值时遇到问题。 Still couldnt solve this by myself so I decided to ask you, do you maybe see something that I dont?还是不能自己解决这个问题,所以我决定问你,你是否看到了我没有看到的东西?

report.php is a page with table that includes all the rows from database table, here is the code: report.php 是一个包含数据库表中所有行的表格页面,代码如下:

<!DOCTYPE>
<html>
    <head>
        <link rel="stylesheet" href="https://ajax.googleapis.com/ajax/libs/jqueryui/1.11.4/themes/smoothness/jquery-ui.css">
        <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
        <script src="https://ajax.googleapis.com/ajax/libs/jqueryui/1.11.4/jquery-ui.min.js"></script>
        <style>
        table {
            border-collapse: collapse;
        }

        table, th, td {
            border: 1px solid gray;
            padding:5px;
        }
        tr:hover {background-color: #f5f5f5}
        </style>

    <head>
    <body>
        <?php
            require_once '../include/functions.php';
            require_once '../include/db.php';

            $htmltable = "";

            $htmltable .= "<table>
            <tr>
                <th>ID</th>
                <th>Ime</th>
                <th>Ime slavljenika</th>
                <th>Datum</th>
                <th>Poruka</th>
                <th>Vreme prijave</th>
                <th>Obrisi</th>
            </tr>";


            $prep = $db->prepare("SELECT * from prijavljeni");
            $prep->execute();
            $prijavljeni = $prep->fetchAll();

            foreach($prijavljeni as $prijavljen => $row) {

                $htmltable.= '<tr>
                    <td>'.$row['prijavljeni_id'].'</td>
                    <td>'.$row['ime'].' </td>
                    <td>'.$row['ime_slavljenik'].' </td>
                    <td>'.$row['datum'] .'</td>
                    <td>'.$row['poruka'].' </td>
                    <td>'.$row['vreme'].'</td>
                <td><a href="delete.php?prijavljeni_id='.$prijavljeni["prijavljeni_id"].'">Delete</a></td>

                </tr>';
               }

                $htmltable.='</table>';

                echo $htmltable;

        ?>

        <div>
            <button onclick="return email()">Posalji</button>
            <div id="emailporuka">
            </div><br><br>
        </div>
        <p align="center"><a href="logout.php">Logout</a></p>

        <script>
            function email(){

            $.ajax({
                  type:"post",
                  url: "email.php",

                  success: function(data){
                      //window.alert(data);
                      document.getElementById("emailporuka").innerHTML = data;
                  },
                  error: function (req, status, err) {
                console.log('Something went wrong', status, err);
                }
              })
              return false;
        }
        </script>

      </body>
</html>

delete.php删除.php

<?php
    include('../include/db.php');
        include('../include/functions.php');



$prijavljeni_id=$_GET['prijavljeni_id'];


$result = $db->prepare("DELETE FROM prijavljeni WHERE prijavljeni_id = prijavljeni_id");//brise sve reove
$result->bindParam('prijavljeni_id', $prijavljeni_id);//brise sve redove


$result->execute();
header("location: izvestaj.php");

?> 

You have issue in prepare statement你在准备语句中有问题

Change改变

$result = $db->prepare("DELETE FROM prijavljeni WHERE prijavljeni_id = prijavljeni_id");//brise sve reove
$result->bindParam('prijavljeni_id', $prijavljeni_id);

To

$result = $db->prepare("DELETE FROM prijavljeni WHERE prijavljeni_id = :prijavljeni_id");// Need to add : before bind param name//brise sve reove
$result->bindParam(':prijavljeni_id', $prijavljeni_id);

According to your code prijavljeni_id = prijavljeni_id so this condition become always true so your all records are being deleted根据您的代码prijavljeni_id = prijavljeni_id因此此条件始终为真,因此您的所有记录都将被删除

Also change <a> as below.也改变<a>如下。 add $row instead of $prijavljeni添加$row而不是$prijavljeni

<td><a href="delete.php?prijavljeni_id='.$row["prijavljeni_id"].'">Delete</a></td>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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