gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 17:20:43 25 4
gpt4 key购买 nike

我的文件中有一个通用更新查询,我知道它正在由我的项目 Controller 调用,但我不知道问题是否出在表单和 Controller 、表单和函数或函数和 Controller 之间。

我的代码应该可以在项目列表表单中填充带有编辑和删除按钮的记录(这有效),当选择编辑按钮时,编辑项目表单将填充该行中的数据(这有效) 。编辑表单中显示的信息后,用户按“保存”,这将调用项目 Controller 中的“编辑”功能,而项目 Controller 又调用“保存”功能,然后调用“更新”功能,从而更新数据库(这不起作用)。我不确定错误在哪里,因为没有发生错误,并且我知道代码正在进入 Controller 的编辑功能,因为它会再次打开列表。

我确信这个错误是我在某个地方犯的一些愚蠢的拼写错误,如果您想知道保存功能可以去更新或插入功能,但插入工作得很好。

这就是为什么我 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
]
];
}
}

}编辑项目表格

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

最佳答案

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

这一行:

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

应该是:

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

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

关于mysql - php mysql通用更新查询不更新数据库中的数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51270766/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com