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.