I am very new to the subject of PHP and SQL working together and I have been fine so far except for updating a database row on my SQL database. I'm using parts of my lecturers code and doing exercises and my own tasks to modify the webpages and behaviour.
The purpose of this code is to update an article that I have set up, so I can edit the title or the code then click confirm but when I do this I get my failed return message telling me that it failed to bind my parameters. I have often had trouble passing parameters in other languages and I have been looking and testing this for hours that I am hoping to receive some information and guidance on the subject.
All I want to do is update the articletext and articletitle fields. On the database there is another 3 fields blogID, blogtime, blogposter which don't need changing.
If you look at RESULT at the bottom you will see that the variables do have information but are not being updated to the database instead the process crashes during the bind_param section.
My EDIT ARTICLE code section:
<?php
$db=createConnection();
// get the first two articles
$sql = "select blogID,articletitle,articletext,blogtime,
blogposter,username,userid
from blogarticle
join registerdemo on blogposter = userid where blogID=?";
$stmt = $db->prepare($sql);
$stmt->bind_param(i,$article);
$stmt->execute();
$stmt->store_result();
$stmt->bind_result($articleid,$articletitle,$articletext,$blogtime,
$blogposter,$username,$userid);
//build article html
while($stmt->fetch()) {
echo "<article id='a$articleid'>
<h1>$articletitle</h1>
<p>".nl2br($articletext)."</p>
<footer><p>Posted on <time datetime='$blogtime'>$blogtime</time> by <em>$username</em></p></footer>";
// if user is logged in and not suspended add comment button
if($currentuser['userlevel']>2 || ($currentuser['userid']==$userid && $currentuser['userlevel']>1)) {
?> <form method='post' action='applychanges.php'>
<input type="hidden" name="articleid" id="articleid" size="30" value="<?php echo $articleid; ?>"/><br />
<input type="text" name="articletitle" id="articletitle" size="30" required value="<?php echo $articletitle; ?>"/><br />
<textarea name="articletext" id="articletext" cols="60" rows="5"><?php echo $articletext; ?></textarea></br>
<button type="submit">Confirm</button>
</form>
<?php
}
echo "</article>";
}
$stmt->close();
$db->close();
?>
My APPLY CHANGES code:
This is where the parameters fail
<!doctype html>
<html lang="en-gb" dir="ltr">
<head>
</head>
<body>
<?php
ini_set('display_errors', 'On'); ini_set('html_errors', 0); error_reporting(-1);
print_r($_POST);
include('php/functions.php');
if(isset($_POST['articleid']) && isset($_POST['articletitle']) && isset($_POST['articletext'])) {
$db=createConnection();
$articleid=$_POST['articleid'];
$articletitle=$_POST['articletitle'];
$articletext=$_POST['articletext'];
$updatesql = "UPDATE blogarticle SET articletitle=?, articletext=? WHERE articleid=?";
$doupdate=$db->prepare($updatesql);
$doupdate->bind_param("ssi",$articletitle, $articletext, $articleid);
$doupdate->execute();
$doupdate->close();
$db->close();
header("location: index.php");
} else {
echo "<p>Some parameters are missing, cannot update database</p>";
print_r($_POST);
}
?>
</body>
</html>
Result:
Fatal error: Call to a member function bind_param() on boolean in /home/16018142/public_html/Assessment/applychanges.php on line 18
when I use print_r($_POST) it displays these -
Array ( [articleid] => 9
[articletitle] => THIS IS A TEST
[articletext] => Test Test Test Test Test Test Test Test Test Test Test Test Test Test Test Test Test Test Test Test Test Test Test Test Test Test Test Test Test Test Test Test Test Test Test Test Test Test ).