-2

I tried making a delete button but if I use a checkbox it works fine but wont work without can anyone assist me my code is as follows:

I added my whole code for table where i'm wanting to delete an entry via a button hope this is more useful.

 <?php

if (isset($_POST['rBtn'])) {
    $sql = $odb->prepare("DELETE FROM `fe` WHERE `ID` = :id");
    $sql->execute(array(':id' => $id));
    $notify = '<div class="btn btn-outline-success btn-sm" role="alert"><button type="button" class="close" data-dismiss="alert" aria-label="Close"><span aria-hidden="true">&times;</span></button><i class="mdi mdi-check-all"></i>API has been deleted!</div><meta http-equiv="refresh" content="2;url=customers.php">';
}
?>

<div class="card-body">
    <form action="" method="POST" class="form-horizontal">
        <table id="bootstrap-data-table-export" class="table table-striped table-bordered">
            <thead>
            <tr>
                <th>IP</th>
                <th>Type</th>
                <th>Name</th>
                <th>Date</th>
                <th>Action</th>
            </tr>
            </thead>
            <tbody>
            <?php
            $SQLSelect = $odb->prepare("SELECT * FROM `fe` WHERE `userID` = :user ORDER BY `ID` DESC");
            $SQLSelect->execute(array(':user' => $_SESSION['ID']));
            while ($show = $SQLSelect->fetch(PDO::FETCH_ASSOC)) {
                $ipShow   = htmlspecialchars($show['ip']);
                $noteShow = htmlspecialchars($show['note']);
                $ids      = intval($show['ID']);
                $date     = htmlspecialchars(date("d-m-Y, h:i:s a", $show['date']));
                $type     = $show['type'] == 'f' ? '<button class="btn btn-success btn-sm">Friend</button>' : '<button class="btn btn-danger btn-sm">Enemy</button>';
                echo '<tr><td>' . htmlspecialchars($ipShow) . '</td><td>' . $type . '</td><td>' . htmlspecialchars($noteShow) . '</td><td>' . htmlspecialchars($date) . '</td><td><input type="submit" value="Delete" name="rBtn" class="btn btn-outline-danger btn-sm" /></td></tr>';
            }
            ?>
            </tbody>
        </table>
    </form>
</div>
Nico Haase
  • 6,670
  • 34
  • 28
  • 48
Private
  • 7
  • 5
  • 2
    Define "won't work". What specifically is failing? – David Mar 06 '19 at 14:32
  • I wanna delete an item with a button from a table button needs to grab each id for table entry – Private Mar 06 '19 at 14:33
  • ....but instead what happens? – Twista Mar 06 '19 at 14:36
  • For example if my select statement grabbed from DB and first item was "Test" it would have delete button near it would need to grab the ID for that item so when i click button it delete – Private Mar 06 '19 at 14:37
  • @Twista nothing happens page just reloads – Private Mar 06 '19 at 14:37
  • @Private: In your attempt to delete the record you are using a variable called `$id`. Where do you define that variable and assign it a value? – David Mar 06 '19 at 14:46
  • If this is your full code then something is wrong.There seems to be no connection to the database – Prime Jay Mar 06 '19 at 14:46
  • There isnt even a closing php tag – Prime Jay Mar 06 '19 at 14:48
  • I mean this is the full code for Select table not the full page this is the code where i'm trying to implement delete button for an entry – Private Mar 06 '19 at 14:49
  • 2
    Please explain what you are tring to do more comprehensively – Prime Jay Mar 06 '19 at 14:50
  • 1
    What **exactly** is not working with the given code? What have you tried to debug the problem? Please add all information to the question itself, not to the comment section – Nico Haase Mar 06 '19 at 14:59
  • I don't get any debug off it @NicoHaase my button `` is suppose to delete individual item from table each item has a delete button but when i click button only refreshes page nothing deletes. – Private Mar 06 '19 at 15:03
  • So, you haven't debugged anything, and you don't want to share more details to people who try to help you? Sounds strange to me – Nico Haase Mar 06 '19 at 15:47

3 Answers3

2

It is probably because there is no connection to the database.

You need to connect to the database before you can do anything.I can't see any where that you connect to the sql database.

To connect to the database PDO way,you can use:

<?php
$servername = "localhost" //by default is set to localhost;
$username = "username"//or whatever your username is;
$password = "password" //or whatever your password is;

try {
    $conn = new PDO("mysql:host=$servername;dbname=myDB", $username, $password);
    // set the PDO error mode to exception
    $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    echo "Connected successfully"; 
    }
catch(PDOException $e)
    {
    echo "Connection failed: " . $e->getMessage();
    }
?>
Prime Jay
  • 121
  • 1
  • 11
1

As stated multiple times in the comment, you have not sent a value for $id to the backend on submitting the form. There are two possible ways to solve this:

1) Send it through the button by using the following part in your template:

<input type="submit" value="Delete" name="rBtn" class="btn btn-outline-danger btn-sm" value="<?php echo (int)$show['ID']; ?>"/>

Afterwards, read that ID value intially:

if (isset($_POST['rBtn'])) {
    $id = $_POST['rBtn'];
    $sql = $odb->prepare("DELETE FROM `fe` WHERE `ID` = :id");
    $sql->execute(array(':id' => $id));
    $notify = '<div class="btn btn-outline-success btn-sm" role="alert"><button type="button" class="close" data-dismiss="alert" aria-label="Close"><span aria-hidden="true">&times;</span></button><i class="mdi mdi-check-all"></i>API has been deleted!</div><meta http-equiv="refresh" content="2;url=customers.php">';
}

2) Use one form per row and put that ID value in a hidden field to be transmitted

Additionally, have a look at debugging - it really helps you to spot such errors on your own the next time. You would have seen simply that, given the code you've shared is complete, the variable $id holds no value. Secondly, the execution of that query would have thrown an error (which would have told you that there is something missing).

Nico Haase
  • 6,670
  • 34
  • 28
  • 48
-1

You can use a button within a form per row and then use javascript to submit :

<form action="...">  
    <input name="id" value="5"/>
    <button name="del" onclick="javascript:this.form.submit();">Delete</button>
</form>

...

<?php
if (isset($_POST['del']) {
    ...
}
?>
  • Please don't use `onclick` handlers anymore. There are much better ways. Additionally, explain why you think that it solves the problem - submitting a form using a button **and** JS code is not needed – Nico Haase Mar 06 '19 at 15:00
  • If i'm correct that would delete all not individual items correct? Sorry if i mislead my words. – Private Mar 06 '19 at 15:00
  • NicoHaase: Who says he is allowed or not to allowed to use onclick() for submitting a form. What is better is up to the developer. By the way where is your solution? Private: you can wrap the form tag around you delete code and use a n ID in the name of the button or a hidden field, that way you know which button was clicked. – Martin Newman Mar 06 '19 at 15:10
  • I just tried a hidden field doesn't seem to wanna wrok – Private Mar 06 '19 at 15:15
  • @MartinNewman I have no clue what the initial problem is, so I cannot provide a solution for that. And the question of `oncllick` attributes vs. pure JS solutions is covered in https://stackoverflow.com/questions/12627443/jquery-click-vs-onclick – Nico Haase Mar 06 '19 at 15:47
  • You don't know the question but you criticize me.. :-) It's supposed to give him an easy hint, he can elaborate. I agree with you, there are many ways which seem to be "nicer", but just because many people prefer it, it is not a mandatory approach. The developer and the one answering the question can choose freely ;-) – Martin Newman Mar 06 '19 at 16:36
  • But could you even explain why you need JS to submit a form? What would happen if you skip the JS part? – Nico Haase Mar 06 '19 at 20:23
  • @NicoHaase I use to prefer – Martin Newman Mar 07 '19 at 10:39