-1

Looked at a bunch of examples. Nothing happens. I try to transfer to the value of the ID of the input element. The row identifier from the database for subsequent deletion of the record. But no matter how I tried, Get does not take this value from id attribute

<?php
        require_once("dbcontroller.php");
        $db_handle = new DBController();
        $sql = "SELECT * from main";
        $faq = $db_handle->runQuery($sql);
?>

<table class="one">
      <thead>
          <tr>
            <th class="table-header" width="10%">№</th>
            <th class="table-header">Дата</th>
            <th class="table-header">Адрес</th>
            <th class="table-header" width="10%">Наименование проекта</th>
            <th class="table-header">Что изменено</th>
            <th class="table-header">Кем</th>
            <th class="table-header">Документ</th>
          </tr>
      </thead>
      <tbody>
      <?php
      foreach($faq as $k=>$v) {


      ?> 

          <tr class="table-row">
            <td><?php echo $k+1; ?></td>
            <td contenteditable="true" onBlur="saveToDatabase(this,'destination','<?php echo $faq[$k]["id"]; ?>')" onClick="showEdit(this);"><?php echo $faq[$k]["destination"]; ?></td>
            <td contenteditable="true" onBlur="saveToDatabase(this,'name_change','<?php echo $faq[$k]["id"]; ?>')" onClick="showEdit(this);"><?php echo $faq[$k]["name_change"]; ?></td>
            <td contenteditable="true" onBlur="saveToDatabase(this,'changee','<?php echo $faq[$k]["id"]; ?>')" onClick="showEdit(this);"><?php echo $faq[$k]["changee"]; ?></td>
            <td contenteditable="true" onBlur="saveToDatabase(this,'date_change','<?php echo $faq[$k]["id"]; ?>')" onClick="showEdit(this);"><?php echo $faq[$k]["date_change"]; ?></td>
            <td contenteditable="true" onBlur="saveToDatabase(this,'moderator','<?php echo $faq[$k]["id"]; ?>')" onClick="showEdit(this);"><?php echo $faq[$k]["moderator"]; ?></td>
            <td contenteditable="true" onBlur="saveToDatabase(this,'image1','<?php echo $faq[$k]["id"]; ?>')" onClick="showEdit(this);"><?php echo $faq[$k]["image1"]; ?></td>
            <td> <input type="submit" name="delete" value="Delete" onclick="location.href='delete.php'" id=''<?php echo $faq[$k]["id"]; ?>''></td>
            </tr>
    <?php
    }
    ?>
      </tbody>
    </table>

it's all about this line

<td> <input type="submit" name="delete" value="Delete" onclick="location.href='delete.php'" id=''<?php echo $faq[$k]["id"]; ?>''></td>

delete.php

<?php
        $id = $_GET['id'];

        //Connect DB
        //Create query based on the ID passed from you table
        //query : delete where Staff_id = $id
        // on success delete : redirect the page to original page using header() method
        $dbname = "registration_change";
        $conn = mysqli_connect("10.191.24.99", "asutp", "asutp", $dbname);
        // Check connection
        if (!$conn) {
            die("Connection failed: " . mysqli_connect_error());
        }

        // sql to delete a record
        $sql = "DELETE FROM main WHERE id = $id"; 
        echo($sql);
        if (mysqli_query($conn, $sql)) {
            mysqli_close($conn);
            header('Location: main-edit.php'); //If book.php is your main page where you list your all records
            exit;
        } else {
            echo "Error deleting record";
        }
?>
Capricorn
  • 33
  • 6

1 Answers1

1

Just pass the id to the url :

// Notice this ---------------------------------------------------------------------v-------------------------------v
<input type="submit" name="delete" value="Delete" onclick="location.href='delete.php?id=<?php echo $faq[$k]["id"]; ?>'" id='<?php echo $faq[$k]["id"]; ?>'>

Note that you put single quotes in excess in id=''<?php echo $faq[$k]["id"]; ?>'', I removed them in my example

Cid
  • 14,968
  • 4
  • 30
  • 45
  • edit this....but error..opened it in developer mode td> "Uncaught SyntaxError: Invalid or unexpected token" – Capricorn Jul 17 '19 at 09:01
  • Ah, yes I forgot a single quote after the URL. Edited – Cid Jul 17 '19 at 09:06
  • It's work, thanks, but I still do not understand why you need a second expression with the ID at the end of the line? First expression its a name and second as like value? – Capricorn Jul 17 '19 at 09:12
  • This is just an attribute in that html tag. I kept it because you had it. Feel free to remove it if you don't need it elsewhere – Cid Jul 17 '19 at 09:37