0

I have a normal html table filled with database entries. I got a plugin to select multiple rows in this table. Now i want to click on a button and delete the database entry so I need to get the id out of the array and but it in my php script. But I have not really an idea how to do it.

</div>


   <table id="myTable" class="content-table">
    <thead>
      <tr>
        <th>ID</th>
        <th>Artikelnummer</th>
        <th>Name</th>
        <th>Preis</th>
      </tr>
    </thead>
    <tbody>
        <?php
          $sql = "SELECT * FROM artikel;";
          $result = mysqli_query($conn, $sql);
          $resultCheck = mysqli_num_rows($result);

          if ($resultCheck > 0) {
              while ($row = mysqli_fetch_assoc($result)) {

                ?> 
                <tr>
                <td> <?php echo $row["id"]?></td> <?php 
                ?> <td> <?php echo $row["artikelnummer"]?></td> <?php 
                ?> <td> <?php echo $row["name"]?></td> <?php 
                ?> <td> <?php echo $row["preis"]?> €</td> </tr><?php 
              }
          }

         ?>

    </tbody>
  </table> 
  </div>
var $rows = [];
    $(function() {
        console.log( "ready!" );
        // start plugin
        $('#myTable').TableSelection({
                sort : true, // sort or not (true | false)
                status : 'multiple', // single or multiple selection (default is 'single')
            }, function(obj){ // callback function return selected rows array
                $rows = obj.rows;
        });

    });
<?php
    include_once 'dbh.inc.php';

    $artid = ???;

    $sql = "DELETE FROM artikel WHERE id= $artid;";

    mysqli_query($conn, $sql);

    header("Location: ../index.php?daten=success");
Scherix
  • 13
  • 1
  • Multiple ways of doing this but all stem from sending the `$artid` as a request variable. – IsThisJavascript Jul 11 '19 at 11:47
  • Btw your sql code is at risk of sql injection, have a read of [When should I use prepared statments?](https://stackoverflow.com/questions/24988867/when-should-i-use-prepared-statements/24989031) – IsThisJavascript Jul 11 '19 at 11:48

1 Answers1

0

You can do it with html attributes. I make a example for you with html, css and javascript.

this is your html.

<table id="tableSelected" class="content-table">
    <thead>
      <tr>
        <th>ID</th>
        <th>Artikelnummer</th>
        <th>Name</th>
        <th>Preis</th>
      </tr>
    </thead>
    <tbody>
      <tr data-id="1">
        <td>1</td>
        <td>1-1</td>
        <td>Test</td>
        <td>1</td>
      </tr>
      <tr data-id="2">
        <td>2</td>
        <td>2-1</td>
        <td>Foo</td>
        <td>1</td>
      </tr>
  </tbody>
</table>

as you can see, every "tr" tag has "data-id" attribute.


var $rows = [];
    $(function() {
      $('#tableSelected').TableSelection({
        sort : true,
        status : 'multiple',
      }, function(obj) {
        $.each(obj.rows, function(i, row){
          var id = $('#tableSelected').RowValue(row).attr('data-id') // You can get id with this code
        });
      });
    });

Note: https://codepen.io/yusufilkeroguz/pen/vqPgzZ you can see live preview from here :)

  • You can send "id" with ajax to php. in js after id, add ajax, example url https://foo.com/bar/delete?id=[SELECTED-ID] (this is get method, if you want you can send with post). in php, $artid = $_GET['id']; .... – Yusuf İlker Oğuz Jul 11 '19 at 12:19