我完全是PHP和MySQL领域的新手。我正在读凯文·扬克( Kevin )的书,在做他的一个例子时,我遇到了一个奇怪的结果。我确信我正确地遵循并键入了写在他的书上的代码,但是我想知道为什么我没有得到相同的结果。我反复检查了代码,并确定它是正确的,或者我可能遗漏了什么。
我在这里发这篇文章,是因为我知道从这里开始对像我这样的新手来说是非常有帮助的,也是非常好的。任何投入都是非常感谢的。期待评论,以便我可以继续我的研究,因为我认为我被困在这个奇怪的错误。
请参阅下面的代码以供参考。
index.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Joke CMS</title>
</head>
<body>
<h1>Joke Management System</h1>
<ul>
<li><a href="jokes/">Manage Jokes</a></li>
<li><a href="authors/">Manage Authors</a></li>
<li><a href="categories/">Manage Joke Categories</a></li>
</ul>
</body>
</html>index.php
<?php
//Display author list
include_once $_SERVER['DOCUMENT_ROOT'] . '/includes/db.inc.php';
try {
$result = $pdo->query('SELECT id, name FROM author');
} catch (PDOException $e){
$error = 'Error fetching authors from database! ' . $e->getMessage();
include 'error.html.php';
exit();
}
if (isset($_POST['action']) and $_POST['action'] == 'Delete')
{
include $_SERVER['DOCUMENT_ROOT'] . '/includes/db.inc.php';
//Get jokes belonging to author
try {
$sql = 'SELECT id FROM joke WHERE authorid = :id';
$s = $pdo->prepare($sql);
$s->bindValue(':id', $_POST['id']);
$s->execute();
} catch (PDOException $e){
$error = 'Error fetching authors with their jokes! ' . $e->getMessage();
include 'error.html.php';
exit();
}
$result = $s->fetchAll();
//Delete joke category entries
try {
$sql = 'DELETE FROM jokecategory WHERE jokeid = :id';
$s = $pdo->prepare($sql);
//For each joke
foreach ($result as $row)
{
$jokeId = $row['id'];
$s->bindValue(':id', $jokeId);
$s->execute();
}
} catch (PDOException $e){
$error = 'Error deleting joke category! ' . $e->getMessage();
include 'error.html.php';
exit();
}
//Delete jokes belonging to author
try {
$sql = 'DELETE FROM joke WHERE authorid = :id';
$s = $pdo->prepare($sql);
$s->bindValue(':id', $_POST['id']);
$s->execute();
} catch (PDOException $e){
$error = 'Error deleting joke from a specific author! ' . $e->getMessage();
include 'error.html.php';
exit();
}
//Delete the author
try {
$sql = 'DELETE FROM author WHERE id = :id';
$s = $pdo->prepare($sql);
$s->bindValue(':id', $_POST['id']);
$s->execute();
} catch (PDOException $e){
$error = 'Error deleting the author from database! ' . $e->getMessage();
include 'error.html.php';
exit();
}
header('Location: .');
exit();
}
foreach ($result as $row){
$authors[] = array(
'id' => $row['id'],
'name' => $row['name']
);
}
include 'authors.html.php';
?>authors.html.php
<?php
include_once $_SERVER['DOCUMENT_ROOT'] . '/includes/helper.inc.php';
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8"/>
<title>Manage Authors</title>
</head>
<body>
<h1>Manage Authors</h1>
<p><a href="?add">Add new author</a></p>
<ul>
<?php foreach($authors as $author): ?>
<li>
<form action="" method="post">
<div>
<?php echo htmlout($author['name']); ?>
<input type="hidden" value="<?php echo htmlout($author['id']); ?>"/>
<input type="submit" name="action" value="Edit"/>
<input type="submit" name="action" value="Delete"/>
</div>
</form>
</li>
<?php endforeach; ?>
</ul>
<p><a href="..">Return to JMS home</a></p>
</body>
</html>所有输入都是非常合适的。
发布于 2014-03-03 08:32:11
$pdo对象是否已正确初始化,以及数据库中是否有实际记录。您可以使用像Workbench这样的外部MySQL客户机来测试查询。你也可以用它来优化。只有当您对一个查询感到满意时,您才会将它放入脚本中,除非它非常简单,您可以正确地编写它。`table_names`和`column_names`转义为MySQL合法,并使用;结束查询。而且看起来好多了。foreach初始化变量$authors = array();之前。那本书应该会教你这个。var_dump()。把var_dump($result);放在$result = $pdo->query('...');后面。还可以在var_dump($row);中使用foreach。它用于简单但有效的调试。打印变量值。htmlout()确实存在吗?var_dump()添加之后,您实际上看到了什么?发布于 2014-03-04 10:11:20
在您的authors.html.php文件中,您没有给隐藏输入类型命名
<input type="hidden" value="<?php echo htmlout($author['id']); ?>"/>试着换到
<input type="hidden" name="id" value="<?php echo htmlout($author['id']); ?>"/>您正在寻找一个不存在的POST变量
$s->bindValue(':id', $_POST['id']);https://stackoverflow.com/questions/22141807
复制相似问题