我正在尝试建立待办事项列表[CRUD]。我只有更新/编辑问题。我的代码在数据库中用NULL
值填充变量。所以我认为我在get_params()
函数中犯了一个错误
我的代码在create函数上起作用,不同之处不能那么大,但我找不到错误
<?php
$keys = [
'title',
'description',
'date',
'status'
];
require_once 'init.php';
$pdo= db();
if(isset($_GET['id'])){
$id = $_GET['id'];}
$query = 'UPDATE todolist SET dotitle = :title, dodescription = :description, date = :date, status = :status
WHERE id = :id ';
$update = $pdo->prepare($query);
$params = get_params($id);
var_dump($params);
//echo $params[':title'];
$params[':status'] = 0;
//$params[':date'] = '2019-05.20';
$update->execute($params);
function get_params($id)
{
//print_r($_POST);
global $keys;
//$params = [];
$params[':id'] = $id;
$params += get_values($keys);
return $params;
}
function get_values($keys)
{
foreach ($keys as $key) {
$value = NULL;
//print_r($_POST);
if (isset($_POST[$key])) {
$value = filter_input(INPUT_POST, $key, FILTER_SANITIZE_SPECIAL_CHARS);
}
//print_r($key);
//print_r($value);
$params[":$key"] = $value;
}
return $params;
}
echo'
<h2>Edit Todo</h2>
<form action="edit.php?id=$id" method="post">
<p>Title</p>
<input type="text" name=title >
<p>Description</p>
<input type="text" name=description >
<p>Date</p>
<input type="datetime-local" name=date value="2018-06-07 00:00" >
<br>
<input type="submit" name=submit value="edit">
</form>
'
?>
我输入的值应该在数据库中更新。