I am trying to perform a simple update CRUD operation and this function only works if my id is static. For example, when I add id = 1
, it works. But when I try to pass an id using :id
or id = ?
, it will not work. However, I don't get an error. Any ideas what I'm missing? I have searched for an answer for two days. Any help would be appreciated. My code is below. The function below is in my Scripture class. I have the object instantiated on the edit page like this.
if(isset($_GET['id'])){
$scripture->id = $_GET['id'];
echo $scripture->id;
} else {
echo "There is no id";
}
if(isset($_POST['edit'])){
$scripture->book = $_POST['book'];
$scripture->chapter = $_POST['chapter'];
$scripture->verse = $_POST['verse'];
$scripture->body = $_POST['body'];
$scripture->editScripture($scripture->id, $scripture->book, $scripture->chapter, $scripture->verse, $scripture->body);
}
class Scripture {
public $id;
public $book;
public $chapter;
public $verse;
public $body;
public function createScripture($postData)
{
global $db;
$this->book = $_POST['book'];
$this->chapter = $_POST['chapter'];
$this->verse = $_POST['verse'];
$this->body = $_POST['body'];
try {
$createQuery = $db->prepare("INSERT INTO scriptures (book, chapter, verse, body) VALUES (:book, :chapter, :verse, :body) ");
$createQuery->execute(["book" => $this->book, "chapter" => $this->chapter, "verse" => $this->verse, "body" => $this->body]);
} catch (PDOException $ex) {
echo "An error occurred " . $ex->getMessage();
}
header("Location: show-all.php");
?>
<div class="container">
<div class="alert alert-success">
<p>Scripture created successfully</p>
</div>
</div>
<?php
}
public function editScripture($id, $book, $chapter, $verse, $body)
{
global $db;
$this->book = $_POST['book'];
$this->chapter = $_POST['chapter'];
$this->verse = $_POST['verse'];
$this->body = $_POST['body'];
try {
$query = "UPDATE scriptures SET book = :book, chapter = :chapter, verse = :verse, body = :body WHERE id = :id";
$statement = $db->prepare($query);
$statement->bindparam(":id", $this->id);
$statement->bindparam(":book", $this->book);
$statement->bindparam(":chapter", $this->chapter);
$statement->bindparam(":verse", $this->verse);
$statement->bindparam(":body", $this->body);
$statement->execute();
return true;
} catch (PDOException $ex) {
echo "An error occurred " . $ex->getMessage();
}
}
}
<form action="edit.php" method="POST">
<div class="form-group">
<label for="book">Book</label>
<input type="text" class="form-control" name="book" id="book">
</div>
<div class="form-group">
<label for="chapter">Chapter</label>
<input type="text" class="form-control" name="chapter" id="chapter">
</div>
<div class="form-group">
<label for="verse">Verse</label>
<input type="text" class="form-control" name="verse" id="verse">
</div>
<div class="form-group">
<label for="body">Body</label>
<input type="text" class="form-control" name="body" id="body">
</div>
<div class="form-group">
<input type="submit" class="btn btn-success" name="edit">
</div>
</form>