0

I cannot for the life of me figure out why the DeleteTask function will not work, when it is almost the same as RetrieveTask; only called in different files.

Now:

src/controller/RetrieveTask:

prepares a PDO database query, and passes it to src/view/DisplayTask.php, which uses a for each-loop to echo the rows and an <a> element linking to src/redir/DeleteAndReturn.php.

This file simply executes src/controller/DeleteTask.php and src/controller/ReturnToIndex.php.

The file code and order is as follows:

RetrieveTask

<?php

function RetrieveTask($db)
{
    $sql = 'SELECT * FROM task';
    return $db->query($sql);

}

?>

Which gets passed to DisplayTask:

<?php

  function DisplayTask($db)
    {
         foreach (RetrieveTask($db) as $row)
        {
            echo "<li>" . $row['tsk-name'] . "<br>" . $row['tsk-desc'] . "<a id=".$row['id']." class='btn btn-danger' href='/todo/redir/DeleteAndReturn.php'>Delete</a>" . "</li>";
        }
    }

?>

Which get passed to index.php in the /todo/home directory. All I need to do is call DisplayTask($DbHandler) Where $DbHandler is an instant of the db class.

Now for src/controller/DeleteTask.php:

<?php

function DeleteTask($db)
{
    echo "Delete";
    $sql = ' DELETE FROM task where id= 2 ';
    return $db->query($sql);
}

?>

and src/controller/ReturnToIndex.php:

<?php

function ReturnToIndex()
{
    header('Location: /todo/home');
}

?>

Leads to redir/DeleteAndReturn.php

<?php

include_once('../src/model/db/DbConnect.php');
include_once('../src/controller/DeleteTask.php');
include_once('../src/controller/ReturnToIndex.php');

$DbHandler = new DbConnect();

DeleteTask($DbHandler);

$DbHandler->CloseConnection();

ReturnToIndex();

?>

I've tried passing the item id as a get parameter in a query string. Deleting all tables. Manually selecting the id. I can't get it to work. Any help would be much appreciated. I googled and looked up documentation for hours. I feel like it is something exceedingly simple that just went way over my head.

Full code is here: https://github.com/liengesbor.

halfer
  • 19,824
  • 17
  • 99
  • 186
Alexel
  • 3
  • 4
  • So you do not see any errors? Please add that as option when you create PDO: `PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION` as well as error_reporting in PHP, etc.. Please check steps here: http://stackoverflow.com/questions/15990857/reference-frequently-asked-questions-about-pdo/15990858#15990858 – maxime_039 Dec 13 '16 at 10:17
  • 1
    maybe because you have hard coded id=2 into delete and that id has already been deleted? – e4c5 Dec 13 '16 at 10:20
  • @e4c5 I was thinking about that but he wrote "[...]I've tried passing the item id as a get parameter in a query string. Deleting all tables. Manually selecting the id.[...]" – maxime_039 Dec 13 '16 at 10:23
  • @maxime_039 passing in the id as a get parameter doesn't work because that id is never used in the delete method. There is't only the hard coded 2 that's used – e4c5 Dec 13 '16 at 10:25
  • @e4c5 well I suppose (and I hope) he tried to add the parameter in the statement when he used the parameter. But let's see his answers. – maxime_039 Dec 13 '16 at 10:26
  • @e4c5 Before, the delete method was coded so that it used $_GET to retrieve the id passed from DIsplayTask()'s dynamic id naming. I had changed it to hard coding as a simpler test, since the former wasn't working. I've checked; a task with an id of '2' is still in the database and displaying on index.php. – Alexel Dec 13 '16 at 20:16
  • @maxime_039, I've included the PDO::ATTR_ERRMODE and other parameters in the database class. I assume it works because I've gotten errors before; the class is here: https://github.com/LienGesbor/todoNew/blob/master/src/model/db/DbConnect.php. – Alexel Dec 13 '16 at 20:18
  • @Alexel, the `PDO` is not created correctly, the `$opt` is already an array so no need to use `array()`. As well `$opt` needs to be accessed like this: `$this->opt` – maxime_039 Dec 13 '16 at 20:24
  • @maxime_039 The hard code worked perfectly now. Item 2 was deleted. Thank you so much. I will see if it works with $_GET. For future reference, is using $_GET like that insecure? Also, why did it still connect - yet only stop that specific method? – Alexel Dec 13 '16 at 20:39
  • @Alexel I'm a bit confused however concerning what was the issue and how you fixed it. It can be good if you post an answer. – maxime_039 Dec 13 '16 at 20:43

1 Answers1

0

As maxim_039 suggested, I changed how a variable array was passed to the DB construct. Somehow this fixed the issue.

     function __construct()
     {
-        parent::__construct('mysql:host=localhost;dbname=todo;', 'root', 'alexel', array ('$opt'));
+        parent::__construct('mysql:host=localhost;dbname=todo;', 'root', 'alexel', $this->opt);
         echo "Connected to Database";
     }

Here is the database code:

<?php
// Connect to the database using PDO and MySQL

class DbConnect extends PDO
{
    protected $dsn        = "mysql:host=localhost;dbname=todo;";
    protected $dbPassword = 'alexel';
    protected $dbUsrName  = 'root';
    protected $opt = array(
    PDO::ATTR_ERRMODE            => PDO::ERRMODE_EXCEPTION,
    PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC
    );

     function __construct()
    {
        parent::__construct('mysql:host=localhost;dbname=todo;', 'root', 'alexel', $this->opt);
        echo "Connected to Database";
    }
    function CloseConnection()
    {
        $this->DbConnection = null;
    }
}
?>
Alexel
  • 3
  • 4