php mysql 通用更新查询未更新数据库中的数据

php mysql generic update query not updating data in database

我在一个文件中有一个通用更新查询,我知道它正在被我的项目控制器调用,但我不知道问题是在表单和控制器之间、表单和函数之间还是函数和控制器之间。

My code is supposed to work where from the Project list form is populated with records with edit and delete buttons (this works),when the EDIT button is chosen the Editproject form will populate with the data in that line (this作品)。编辑表单中显示的信息后,用户按下保存,这会调用项目控制器中的编辑功能,而项目控制器又会调用保存功能,然后调用更新功能,从而更新数据库(这不起作用)。我不确定错误在哪里,因为没有发生错误,而且我知道代码正在进入控制器的 EDIT 功能,因为在它进入之后它会再次打开列表。

我确定这个错误是我在某处犯的一些愚蠢的拼写错误,如果您想知道保存功能可以转到更新或插入功能,但插入功能非常好。

这就是为什么我 80% 确定它与更新函数本身或我将信息传递给更新函数的方式有关。

任何帮助将不胜感激,我的项目 table 创建在最后,就像我说的,我确信它就像我忽略的地方的错字一样简单(它总是伴随着我) .

更新函数(并保存)

private function update($fields) {
    $query = ' UPDATE `' . $this->table .'` SET ';

    foreach ($fields as $key => $value) {
        $query .= '`' . $key . '` = :' . $key . ',';
    }

    $query = rtrim($query, ',');

    $query .= ' WHERE `' . $this->primaryKey . '` = :primaryKey';

    //Set the :primaryKey variable
    $fields['primaryKey'] = $fields['id'];

    $fields = $this->processDates($fields);

    $this->query($query, $fields);
}
public function save($record) {
    try {
        if ($record[$this->primaryKey] == '') {
            $record[$this->primaryKey] = null;
        }
        $this->insert($record);
    }
    catch (PDOException $e) {
        $this->update($record);
    }
}

项目总监

<?php

class ProjectController {
private $employeesTable;
private $projectsTable;

public function __construct(DatabaseTable $projectsTable, DatabaseTable $employeesTable) {
    $this->projectsTable = $projectsTable;
    $this->employeesTable = $employeesTable;
}

public function list() {
    $result = $this->projectsTable->findAll();

    $projects = array();
    foreach ($result as $project) {

        $projects[] = [
            'IDP' => $project['IDP'],
            'ProjectID' => $project['ProjectID'],
            'ProjectName' => $project['ProjectName'],
            'ProjectDes' => $project['ProjectDes'],
            'ProjectStartDate' => $project['ProjectStartDate'],
            'ProjectEndDate' => $project['ProjectEndDate'],
            'ProjectStatus' => $project['ProjectStatus']
        ];

    }


    $title = 'Project list';

    $totalProjects = $this->projectsTable->total();

    return ['template' => 'projects.html.php',
            'title' => $title,
            'variables' => [
                    'totalProjects' => $totalProjects,
                    'projects' => $projects
                ]
            ];
}

public function home() {
    $title = 'Colpitts Design';

    return ['template' => 'home.html.php', 'title' => $title];
}

public function delete() {
    $this->projectsTable->delete($_POST['id']);

    header('location: index.php?action=list');
}


public function edit() {
    if (isset($_POST['project'])) {

        $project = $_POST['project'];
        $project['projectstartdate'] = new DateTime();
        $project['projectenddate'] = null;

        $this->projectsTable->save($project);

        header('location: index.php?action=list');

    }   else {

        if (isset($_GET['id'])) {
            $projects = $this->projectsTable->findById($_GET['id']);
        }
        $title = 'Edit Projects';

        return ['template' => 'editproject.html.php',
                'title' => $title,
                'variables' => [
                        'project' => $projects ?? null
                    ]
                ];
    }
}

} 编辑项目形式

<form action="" method="post">
<input type="hidden" name="project[IDP]" value="<?=$project['IDP'] ?? ''?>">
    <label for="ProjectID">Type the Project id here:</label>
    <textarea id="ProjectID" name="project[ProjectID]" rows="3" cols="40"><?=$project['ProjectID'] ?? ''?></textarea>

    <label for="ProjectStatus">Type the Project status here:</label>
    <textarea id="ProjectStatus" name="project[ProjectStatus]" rows="3" cols="40"><?=$project['ProjectStatus'] ?? ''?></textarea>

    <label for="ProjectName">Type the Project name here:</label>
    <textarea id="ProjectName" name="project[ProjectName]" rows="3" cols="40"><?=$project['ProjectName'] ?? ''?></textarea>

<label for="ProjectDes">Type the Project description here:</label>
<textarea id="ProjectDes" name="project[ProjectDes]" rows="3" cols="40"><?=$project['ProjectDes'] ?? ''?></textarea>

<input type="submit" name="submit" value="Save">

项目列表形式

<p><?=$totalProjects?> projects are listed in the DanClock Database.</p>

<?php foreach($projects as $project): ?>
<blockquote>
  <p>

  <?=htmlspecialchars($project['ProjectID'], ENT_QUOTES, 'UTF-8')?>
  <?=htmlspecialchars($project['ProjectDes'], ENT_QUOTES, 'UTF-8')?>
  <?=htmlspecialchars($project['ProjectStartDate'], ENT_QUOTES, 'UTF-8')?>
  <?=htmlspecialchars($project['ProjectStatus'], ENT_QUOTES, 'UTF-8')?>

  <a href="index.php?action=edit&id=<?=$project['IDP']?>">Edit</a>

  <form action="index.php?action=delete" method="post">
<input type="hidden" name="id" value="<?=$project['IDP']?>">
<input type="submit" value="Delete">
  </form>
  </p>
</blockquote>
<?php endforeach; ?>

项目table

CREATE TABLE `Projects` (
  `IDP` int(11) NOT NULL AUTO_INCREMENT,
  `ProjectID` int(11) NOT NULL,
  `ProjectName` varchar(50) NOT NULL,
  `ProjectDes` text,
  `ProjectStartDate` Datetime NOT NULL DEFAULT CURRENT_TIMESTAMP,
  `ProjectEndDate` Datetime,
  `ProjectStatus` varchar(50) NOT NULL DEFAULT 'Active',
  PRIMARY KEY  (IDP)
 ) ENGINE=InnoDB;

我发现了问题,因为我认为这是我的更新功能。它不像我想象的那样"generic",或者差不多。

这一行:

    //Set the :primaryKey variable
    $fields['primaryKey'] = $fields['id'];

应该是:

//Set the :primaryKey variable
$fields['primaryKey'] = $fields[$this->primaryKey];

一些我没注意到的小事,现在继续其他事情><