ブログの投稿が編集されない理由がわかりません。3つのパラメーターを持つupdate_post()メソッドを持つクラス('blog')があります。これが私のコードです(接続と他の部分が機能していることがわかっているのでスキップしました):
<?php
class blog{
function update_post($id, $title, $contents) {
try {
$update = $this->db->prepare("UPDATE posts SET title = $title, contents = $contents WHERE id = $id");
$update->execute();
}
catch (PDOException $e) {
}
}
}
$post = new blog;
if (isset($_GET['id'])) {
if (isset($_POST['publish'])) { // If submit button is clicked
$id = $_GET['id'];
$title = $_POST['title'];
$contents = $_POST['contents'];
$post->update_post($id, $title, $contents);
}
}
?>
編集:それで、私は複数のエラーがあったようです。上記の元のコードは、私のclass.blog.phpファイルとHTML形式のページ('edit_post.php')の2つのファイルからのものです。いくつかの実験の結果、エラーはedit_postページにあるはずであることがわかりました。2番目の「ifステートメント」を「if(1 <2)」に置き換えた後、投稿が更新されました。これがedit_postページの大部分です。
<?php
if (isset($_GET['id'])) {
if (isset($_POST['publicera'])) {
$id = $_GET['id'];
$title = $_POST['title'];
$contents = $_POST['contents'];
$post->update_post($id, $title, $contents);
}
?>
<form method="post" action="edit_post.php">
Titel:<br />
<input type="text" name="title" size="80" value="<?php $post->get_title($_GET['id']); ?>"><br />
Inlägg:<br /><textarea name="contents" rows="20" cols="80"><?php $post->get_contents($_GET['id']); ?></textarea>
<br /><input type="submit" name="publicera" value="Publicera!">
</form>
<?php
} else {
$post->show_post_list();
}
?>
編集#2:解決しました!誤ったSQLクエリとは別に、フォームアクションの値をに変更する必要がありましたaction="edit_post.php?id=<?php echo $_GET['id']; ?>"
。