php mysql generic update查询不更新数据库中的数据

xam8gpfp  于 2021-06-21  发布在  Mysql
关注(0)|答案(1)|浏览(372)

我在一个文件中有一个通用更新查询,我知道它正在被我的项目控制器调用,但我不知道问题是在窗体和控制器、窗体和函数还是函数和控制器之间。
我的代码应该在项目列表表单中用带有编辑和删除按钮的记录填充的情况下工作(这起作用),当选择编辑按钮时,editproject表单将用该行中的数据填充(这起作用)。在编辑表单中显示的信息后,用户按save键调用project controller中的edit函数,后者依次调用save函数和update函数,从而更新数据库(这不起作用)。我不确定错误在哪里,因为没有错误发生,我知道代码正在进入控制器的编辑功能,因为在它这样做之后,它会再次打开列表。
我确信这个错误是我在某个地方犯的愚蠢的错误,如果你想知道save函数可以用来更新或插入函数,但是insert可以很好地工作。
这就是为什么我80%确定它与update函数本身或者我将信息传递给update函数的方式有关。
任何帮助将不胜感激,我的项目表创建是在最后,就像我说的,我相信它就像一个简单的打字错误,在某个地方我忽略了(它总是与我在一起)。
更新函数(并保存)

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; ?>

项目表

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;
41zrol4v

41zrol4v1#

我发现了这个问题,因为我认为这是我的更新功能。它不像我想象的那么“通用”,或者几乎是。
这条线:

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

应该是:

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

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

相关问题