[英]PHP error when updating MySQL row with PDO
I'm trying to make a way to edit my posts on a blog I'm making but for some reason when I try to submit the "update post" form it will give me the error "Something went wrong..." (meaning it got to update post.php) and I'm not sure why. 我正在尝试一种方法来编辑我正在创建的博客上的帖子,但是由于某种原因,当我尝试提交“更新帖子”表格时,它将给我错误“出现了某些错误...”(意思是它必须更新post.php),但我不确定为什么。 The only thing I could see it being is because I'm using TinyMCE to edit the content of the post and the way I'm doing it is wrong?
我唯一看到的是因为我正在使用TinyMCE编辑帖子的内容,而我的操作方式是错误的吗?
editpost.php editpost.php
<?php
include 'php/mysql_connect.php'; // opens a PDO of variable $db
if(isset($_GET['id'])){
$q = $db->prepare('SELECT * FROM posts WHERE id=:post_id LIMIT 1');
$q->execute(array(':post_id'=>$_GET['id']));
$row = $q->fetch(PDO::FETCH_ASSOC);
if($row){
echo '<form method="post" action="php/update_post.php?post_id='.$_GET['id'].'">';
echo '<div class="form-group">';
echo ' <input type="text" class="form-control" name="title" id="title" placeholder="Post Title" autocomplete="off" value="'.$row['title'].'" required />';
echo '</div>';
echo '<div class="form-group">';
echo ' <textarea class="form-control" name="body" id="body">'.$row['body'].'</textarea>';
echo '</div>';
echo '<input type="submit" value="Update Post" class="btn btn-default" />';
echo '</form>';
}
else{
echo 'Post not found.';
}
}
else{
echo 'Post not found.';
}
?>
update_post.php update_post.php
<?php
$post_id = $_GET['post_id'];
$title = $_POST['title'];
$body = $_POST['body'];
include 'mysql_connect.php'; // establishes $db, a PDO connection
// insert the records
$sql = "UPDATE posts SET title=:title, body=:body WHERE id=:post_id)";
$q = $db->prepare($sql);
if($q->execute(array(':title'=>$title, ':body'=>$body, ':post_id'=>$post_id))){
echo '<script type="text/javascript">alert("Success!");location.href="../posts.php";</script>';
}
else{
echo '<script type="text/javascript">alert("Something went wrong...");location.href="../posts.php";</script>';
}
?>
I've changed the form method to GET, and it is passing the variables correctly, so that isn't the problem. 我已将form方法更改为GET,并且它正确传递了变量,所以这不是问题。 The update_post.php is a modified version of my add_post.php, which works perfectly fine so I don't understand why updating it doesn't work right.
update_post.php是我的add_post.php的修改版,可以正常运行,所以我不明白为什么更新不正确。
$sql = "UPDATE posts SET title=:title, body=:body WHERE id=:post_id)";
remove this one >-----^
you have a bracket at the end wrong ;) 你最后有一个括号错了;)
Remove it and it should work: 删除它,它应该可以工作:
$sql = "UPDATE posts SET title=:title, body=:body WHERE id=:post_id";
If you use GET
use GET then ;-) 如果使用
GET
使用GET ;-)
$post_id = $_GET['post_id'];
$title = $_GET['title'];
$body = $_GET['body'];
if you use POST
use POST: 如果您使用
POST
使用POST:
$post_id = $_POST['post_id'];
$title = $_POST['title'];
$body = $_POST['body'];
According to your last comment try change here: 根据您的最新评论,尝试在此处进行更改:
if($row){
echo '<form method="post" action="php/update_post.php">';
echo '<input type="hidden" name="post_id" value="'.$_GET['id'].'">';
echo '<div class="form-group">';
echo ' <input type="text" class="form-control" name="title" id="title" placeholder="Post Title" autocomplete="off" value="'.$row['title'].'" required />';
echo '</div>';
echo '<div class="form-group">';
echo ' <textarea class="form-control" name="body" id="body">'.$row['body'].'</textarea>';
echo '</div>';
echo '<input type="submit" value="Update Post" class="btn btn-default" />';
echo '</form>';
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.