dtdr84101 2017-10-01 12:44
浏览 251
已采纳

如何从生成的php表中获取id的值并删除具有该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. I think there is problem in my script with getting value of 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. 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:

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

?> 
  • 写回答

1条回答 默认 最新

  • doumingchen3628 2017-10-01 12:47
    关注

    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

    Also change <a> as below. add $row instead of $prijavljeni

    <td><a href="delete.php?prijavljeni_id='.$row["prijavljeni_id"].'">Delete</a></td>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥120 计算机网络的新校区组网设计
  • ¥20 完全没有学习过GAN,看了CSDN的一篇文章,里面有代码但是完全不知道如何操作
  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 海浪数据 南海地区海况数据,波浪数据
  • ¥20 软件测试决策法疑问求解答
  • ¥15 win11 23H2删除推荐的项目,支持注册表等
  • ¥15 matlab 用yalmip搭建模型,cplex求解,线性化处理的方法
  • ¥15 qt6.6.3 基于百度云的语音识别 不会改
  • ¥15 关于#目标检测#的问题:大概就是类似后台自动检测某下架商品的库存,在他监测到该商品上架并且可以购买的瞬间点击立即购买下单
  • ¥15 神经网络怎么把隐含层变量融合到损失函数中?