Hi,
I've been doing some really poor PHP for about 3-4 years now and with the new year, I want to do it the better way
I've been dabbling with some OOP so far this weekend, and happy with the results. Managed to create an 'AddEntry' class and added a record to my table in the database.
However, I'm having some issues with an 'Edit Entry' script I'm trying, and wondered if I could get some assistance.
// a snippet from my blog class
public function editEntry()
{
$sql = "UPDATE entries
SET title=:title
WHERE id=:id
LIMIT 1";
if ($stmt = $this->_db->prepare($sql)) {
$stmt->bindParam(':title', $title, PDO::PARAM_STR);
$stmt->bindParam(':id', $id, PDO::PARAM_INT);
if ($stmt->execute() == false) {
user_error(print_r($this->_db->errorInfo() , 1));
return false;
}
if ($stmt->rowCount() == 0) {
user_error("No rows updated.");
error_log(print_r($stmt->debugDumpParams() , 1));
return 0; // allow client code to differntiate between false & zero
}
$stmt->closeCursor();
return true;
}
user_error(print_r($stmt->errorInfo() , 1));
return false;
}
<?php
// my edit_entry.php script
include_once "common/base.php";
$pageTitle = "Edit Entry";
include_once "common/header.php";
if(!empty($_POST['title'])):
include_once "inc/class.blog.inc.php";
$entry = new BlogEntries($db);
/*echo $entry->editEntry();*/
$result = $entry->editEntry($_POST['id'], $_POST['title']);
else:
include_once "inc/class.blog.inc.php";
$entry = new BlogEntries($db);
?>
<h2>Edit Entry</h2>
<form method="post" action="editentry.php?id=<?php echo htmlspecialchars($_GET['id']); ?>" id="registerform">
<div>
<label for="title">Title:</label>
<input type="text" name="title" id="title" value="<?php echo $entry->getEntry(); ?>" /><br />
<input type="submit" name="register" id="register" value="Edit" />
<input type="hidden" name="token" value="<?php echo $_SESSION['token']; ?>" />
<input type="hidden" name="id" value="<?php echo $_GET['id']; ?>" />
</div>
</form>
<?php
endif;
include_once 'common/close.php';
?>
Basically, if I make a change to the value in the input field and submit, it always gives me the output:
user_error("No rows updated.");
Is there something obvious I'm missing from either of the 2 scripts?
I'm just wanting the script to update the table.
I would really appreciate any pointers with this.
Thank you