我在文件中有一个通用的更新查询,我知道它是由我的项目控制器调用的,但是我不知道问题是否出在表单和控制器之间,表单和函数之间或函数和控制器之间。
我的代码应该可以在带有编辑和删除按钮的记录中填充项目列表表单的情况下起作用(此方法有效),当选择“编辑”按钮时,Editproject表单将在该行中填充数据(此方法有效) 。在编辑了以表格形式显示的信息之后,用户按保存,该调用将在项目控制器中调用EDIT函数,然后依次调用save函数和update函数,从而更新数据库(这不起作用)。我不确定错误在哪里,因为没有错误发生,并且我知道代码正在进入控制器的EDIT功能,因为执行此操作后它将再次打开列表。
我确定错误是我在某个地方犯了一些愚蠢的错字,并且如果您想知道save函数可以去更新或插入函数,但是插入工作得很好。
这就是为什么我80%确信它与更新功能本身或将信息传递给更新功能的方式有关。
任何帮助将不胜感激,我的项目表创建已在最后,就像我说的那样,我敢肯定,它就像我所忽略的某个地方的错字一样简单(它总是和我在一起)。
更新功能(并保存)
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
]
];
}
}
} editproject表单
<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;
答案 0 :(得分:0)
我发现了问题,因为我认为这是我的更新功能。并不是我想的那样,或者说几乎是“通用”。
此行:
//Set the :primaryKey variable
$fields['primaryKey'] = $fields['id'];
应为:
//Set the :primaryKey variable
$fields['primaryKey'] = $fields[$this->primaryKey];
我没注意到的小事,现在继续其他所有事情> << / p>