2

I am trying to perform basic CRUD with PHP data objects. I have two files: edit_list.php where I list the pages that I have, and when a page is clicked it sends the user to edit.php with the ID of the page that was clicked. In my edit.php file I run a query to populate the form like so:

if(isset($_GET['id'])){
    $ID = $_GET['id'];
    global $conn;
    $query = ('SELECT * FROM pages WHERE page_id = :page_id');
    $stmt = $conn->prepare($query);
    $stmt->execute(array(':page_id' => $ID));
    $selectPage = $stmt->fetch();`

My form looks like this:

<form action="edit.php" method="post">              
    <input style="width:500px;" type="text" name="title" placeholder="Page Title" value="<?php echo $selectPage['page_title']; ?>"/>               
    <input style="width:500px;" type="text" name="message" placeholder="Message" value="<?php echo $selectPage['page_message']; ?>"/>               
    <textarea  rows="15" value="<?php echo $selectPage['page_content']; ?>" cols="60" placeholder="Content" name="content" style="margin-left: 0px; margin-right: 177px; width: 500px!important;"></textarea>
    <input type='hidden' name='id' value='<?php echo $selectPage['page_id']; ?>' />
    <input type='hidden' name='action' value='update' />
    <input type='submit' value='Edit' />                
</form>

Where I just run $selectPage["page_title"]; etc...

I am trying to run this query:

$action = isset( $_POST['action'] ) ? $_POST['action'] : "";
            if($action == "update"){ 
            try{    
            global $conn;
            $updatequery = 'UPDATE pages SET page_title = :page_title, page_message = :page_message, page_content = :page_content WHERE page_id= :page_id';
            $statement = $conn->prepare($updatequery);
            $statement->bindValue(':page_title', $_POST['page_title']);
            $statement->bindValue(':page_message', $_POST['page_message']);
            $statement->bindValue(':page_content', $_POST['page_content']);
            $statement->bindValue(':page_id', $_POST['page_id']);
            $statement->execute();
            header('Location:index.php');

            }catch(PDOException $exception){ 
            echo "Error: " . $exception->getMessage();
    }   
}

Which redirects me back to index.php like it has executed but nothing gets updated. I have pretty much hit a wall with this and desperation has taken over.

Michael Petrotta
  • 59,888
  • 27
  • 145
  • 179
  • 1
    By default, PDO is silent on error. You either need to explicitly test the result of [`$statement->execute()`](http://www.php.net/manual/en/pdostatement.execute.php) for success, or else configure PDO to throw exceptions with [`$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);`](http://php.net/manual/en/pdo.setattribute.php). – eggyal Oct 26 '13 at 23:55
  • Did you run `$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);` somewhere to make sure your calls to `$conn` throw exceptions instead of only returning `false` when something goes wrong? – Tomas Creemers Oct 27 '13 at 00:00
  • 1
    For one thing, you're POST is `$_POST['page_message']` yet your form input is called `name="message"` and `$_POST['page_title']` input `name="title"` so that won't wash. – Funk Forty Niner Oct 27 '13 at 00:02
  • Another thing, your method is `POST` yet you're calling `if(isset($_GET['id'])){` so again, "another brick wall". `type='hidden' name='id'` – Funk Forty Niner Oct 27 '13 at 00:05
  • GAAAAAAAA! lol @Fred-ii- you were right... feel like an idiot now. thanx a lot. I changed name="message" to name="page_message", etc.. and now its working perfectly. feel like suck a noob – chickenburger Oct 27 '13 at 00:08
  • I don't know much about PDO, but if he specifies query like `SELECT... WHERE col:=some_value`, should't he bind value to `some_value` insteed of `:some_value`? – Antoniossss Oct 27 '13 at 00:10
  • With the `$_GET['id']` I am just pulling the info from the submitted ID from my list of pages to populate the form. still new to PHP so my methods and layout are somewhat questionable – chickenburger Oct 27 '13 at 00:12
  • @chickenburger You're welcome, glad I could help. Remember to also to make the appropriate change to `$ID = $_GET['id'];` – Funk Forty Niner Oct 27 '13 at 00:37
  • @Fred-ii- I would give your comment a "useful comment", but I think my rep is a bit low. will come back and upvote it when I get my rep up a bit haha. The fact that my edit page is working now is a huge weight lifted off my back :D – chickenburger Oct 27 '13 at 00:41
  • @chickenburger I can make it an answer and just click on the checkmark as accepted so we can close the question. – Funk Forty Niner Oct 27 '13 at 00:42
  • @Fred-ii- that would be great. thanx – chickenburger Oct 27 '13 at 00:43

1 Answers1

1

You have $_POST['page_message'] yet your form input is called name="message", not a match.

Also $_POST['page_title'] and input name="title" do not match.

Your method is POST yet you're calling if(isset($_GET['id'])){ in conjunction with type='hidden' name='id'

Those are factors that will break your code.

Funk Forty Niner
  • 74,450
  • 15
  • 68
  • 141
  • did you get this code to work I think I need to do the same thing with my code.http://stackoverflow.com/questions/26789494/trying-update-record-with-pdo?noredirect=1#comment42160540_26789494 – Donny Nov 07 '14 at 16:36
  • @Donny Look at the OP's question/code here, base yourself on that. Start off with one or two binds, once you've gotten those to work, then add on as you go. – Funk Forty Niner Nov 07 '14 at 17:21
  • i am having an issue on mine with action I think it is wrong can you take a look at it this has been frustrating and it is last page i need besides the email function. it keeps throwing me an error syntax error unexpected $action http://stackoverflow.com/questions/26789494/trying-update-record-with-pdo?noredirect=1#comment42160540_26789494 – Donny Nov 07 '14 at 17:43