所以基本上,每当单击按钮时,我都会尝试将数据库中的布尔值从 0 更新为 1。我目前拥有的代码是这个,它不起作用。
<?php
require 'init.php';
if(isset($_GET['as'], $_GET['item'])){
$as = $_GET['as'];
$item = $_GET['item'];
switch ($as) {
case 'done':
$donequery = $db->prepare("
UPDATE items
SET done = 1
WHERE id = :item
");
$donequery->execute([
'item' => $item
]);
break;
}
}
header('location: index.php');
?>
索引页的代码已在下面添加。
<!DOCTYPE html>
<html>
<head>
<title> To Do </title>
<? //Google Fonts
require 'init.php';
$itemsquery = $db->prepare("
SELECT id, name, done FROM items
");
$itemsquery->execute();
$items = $itemsquery->rowCount() ? $itemsquery : [];
?>
<link href='http://fonts.googleapis.com/css?family=Open+Sans' rel='stylesheet' type='text/css'>
<link href='http://fonts.googleapis.com/css?family=Shadows+Into+Light+Two' rel='stylesheet' type='text/css'>
<link rel='stylesheet' href='style.css' />
<meta name="viewport" content="width=device-width, initial-scale=1.0m" />
</head>
<body>
<div class='list'>
<h1 class='header'/> To Do. </h1>
<?php if(!empty($items)): ?>
<ul class='items'>
<?php foreach ($items as $item): ?>
<li>
<span class="item <?php echo $item['done'] ? ' done' : '' ?> "><?php echo $item['name']; ?></span>
<?php if(!$item['done']):?>
<a href="mark.php?as=done&item=<?php echo item['id'];?>" class='done-button'> Mark as done </a>
<?php endif; ?>
</li>
<?php endforeach; ?>
</ul>
<?php else: ?>
<p> You haven't added any items yet
<?php endif; ?>
<form class='item-add' action='add.php' method='POST'>
<input type='text' name='name' placeholder='Type a new item here' autocomplete='off' class='input' required/>
<input type='submit' name='submit' value='add' class='submit'/>
</form>
</div>
</body>
</html>
你的代码中有一个PHP错误,在这里:<?php echo item['id'];?>
您忘记了$
标志