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>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 R语言卸载之后无法重装,显示电脑存在下载某些较大二进制文件行为,怎么办
  • ¥15 java 的protected权限 ,问题在注释里