简体   繁体   中英

Function for deleting row from table using php

Trying to solve my problem with deleting rows from generated table.I have a page called report.php that shows all the rows from database and there is option 'delete row'. My problem is when I click on delete nothing happens and it should go on delete.php file. For now couldnt solve this by myself, maybe you see something that I dont. I dont get any errors so I am a little bit confused. Thank you.

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>


    <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_slavljenika'].' </td>
                    <td>'.$row['datum'] .'</td>
                    <td>'.$row['poruka'].' </td>
                    <td>'.$row['vreme'].'</td>
                    <td><a href="delete.php?prijavljeni_id=<?php echo $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');
$prijavljeni_id=$_GET['prijavljeni_id'];
$result = $db->prepare("DELETE FROM prijavljeni WHERE prijavljeni_id= :prijavljeni_id");
$result->bindParam(':prijavljeni_id', $prijavljeni_id);
$result->execute();
header("location: izvestaj.php");

?> 

You just need to change this in your $htmltable. (there was an error in appending strings in last block).

          $htmltable.= '<tr>
                <td>'.$row['prijavljeni_id'].'</td>
                <td>'.$row['ime'].' </td>
                <td>'.$row['ime_slavljenika'].' </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>';

Update your code

$result->bindParam(':prijavljeni_id', $prijavljeni_id);

To

$result->bindParam('prijavljeni_id', $prijavljeni_id);

Remember one thing always avoid : before on bindParam method but we using : on prepareQuery to indicate replaceable value.

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