xSpartanCx
xSpartanCx

Reputation: 311

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. 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?

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

<?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. 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.

Upvotes: 2

Views: 275

Answers (2)

Daniel W.
Daniel W.

Reputation: 32270

$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";

Upvotes: 2

Alex
Alex

Reputation: 17289

If you use GET use GET then ;-)

$post_id = $_GET['post_id'];
$title = $_GET['title'];
$body = $_GET['body'];

if you use POST use 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>';
        }

Upvotes: 1

Related Questions