1

I am able to insert data in my database table and view everything, but now that I tried to make the delete button work I refreshedthe page and see Undefined index in delete.

The form looks like this:

<?php
foreach ($rows as $row){ ?>
    <div>
        <h3><?php echo $row['title']; ?></h3>
        <p><?php echo $row['body']; ?></p>
        <form method="post" action="<?php $_SERVER['PHP_SELF']; ?>">
            <input type="hidden" name="delete_id" value="<?php echo $row['id']; ?>">
            <input type="submit" name="delete" value="Delete">
        </form>
    </div>

<?php }
?>  

Then in the following code I want to simply echo the ID but it does not work:

if($_POST['delete']){
    echo $_POST['delete_id'];
}

Here my full code in case you need to see more:

<?php

require 'classes/Database.php';

$database = new Database;

$post = filter_input_array(INPUT_POST, FILTER_SANITIZE_STRING);

if($_POST['delete']){
    echo $_POST['delete_id'];
}

if($post['submit']){
    $id = $post['id'];
    $title = $post['title'];
    $body = $post['body'];

    $database->query('UPDATE posts SET title = :title, body = :body WHERE id = :id');
    $database->bind(':title', $title);
    $database->bind(':body', $body);
    $database->bind(':id', $id);

    $database->execute();
    if($database->lastInsertId()){
        echo '<p>Post added</p>';
    } 
}

$database->query('SELECT * FROM posts');
$rows = $database->resultSet();

?>
<h1>Add Posts</h1>
<!-- Our action is/can be the page that we are on -->
<form method="post" action="<?php $_SERVER['PHP_SELF']; ?>">
    <label for="">Post ID</label><br />
    <input type="text" name="id" placeholder="Specify the ID..."><br /><br />
    <label for="">Add title</label><br />
    <input type="text" name="title" placeholder="Add a title..."><br />
    <label>Post body</label><br />
    <textarea name="body"></textarea><br /><br />
    <input type="submit" name="submit" value="Submit">
</form>

<h1>Posts</h1>
<div>
<?php
foreach ($rows as $row){ ?>
    <div>
        <h3><?php echo $row['title']; ?></h3>
        <p><?php echo $row['body']; ?></p>
        <form method="post" action="<?php $_SERVER['PHP_SELF']; ?>">
            <input type="hidden" name="delete_id" value="<?php echo $row['id']; ?>">
            <input type="submit" name="delete" value="Delete">
        </form>
    </div>

<?php }
?>  
</div>

How can I make the delete work?

halfer
  • 19,824
  • 17
  • 99
  • 186
Sidney Sousa
  • 3,378
  • 11
  • 48
  • 99

2 Answers2

4

Just use isset to check if element in array with specified key is exist:

if(isset($_POST['delete'])) {
    echo isset($_POST['delete_id']) ? $_POST['delete_id'] : 'something else';
}

But the best way something like this:

<?php

$fields = ['delete', 'delete_id', 'post', 'body', 'title']; //every field you need
$errors = [];
foreach ($fields as $field) {
   if (!isset($_POST[$field]) {
       $errors[] = 'Please, fill field with name '.$field
   }
}

if ($errors) {
    //display errors...
}
  • I used your code, the error disappears initially, but when I click on a delete button, it echos the the id but then the error kicks in again. 1 Notice: Undefined index: submit in C:\xampp\htdocs\course\index.php on line 13 Plus I did not have to use isset in order to insert the data so I thought the same can work for delete. – Sidney Sousa Nov 24 '17 at 11:37
  • check `submit` also: `if(isset($post['submit'])){ //..}` –  Nov 24 '17 at 11:38
1

If you have default error reporting turned on then you need to change the if statement from:

if($_POST['delete']){
    echo $_POST['delete_id'];
}

to:

if(isset($_POST['delete']) && isset($_POST['deleted_id'])){
    echo $_POST['delete_id'];
}

Currently if you refresh the page (and not post it), it will throw an undefined index notice. This is because if you refresh the page you are performing a GET request and no POST data exists, so $_POST['delete_id'] is not defined.

If you click on the 'delete' button the form will submit using a POST request and the page with reload and the error should disappear.

If the error persists ensure that $row['id'] has a value.

Also you will need to add isset to the test:

if($post['submit']){

so that is becomes:

if(isset($post['submit'])){

This is for the same reason as explained above.

Springie
  • 718
  • 5
  • 8