-2

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 process 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 there is a parameter problem. I have often had trouble passing parameters in other languages and I have been looking and testing this for a few 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.


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="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
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='$articletitle', articletext='$articletext' WHERE articleid='$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:

Some parameters are missing, cannot update database

Array ( [articletitle] => THIS IS A TEST [articletext] => hey )

2 Answers2

0

You are not posting all the parameters with your form. For example, the textarea is missing the name attribute. This will result in not posting this form field your script. Add the following line to your "Apply changes" code. This will print out the parameters you are posting.

print_r($_POST);

Check which parameters are not posted. You probably want to add some hidden form fields.

  • This is a great tip, thank you. Looks like Article text isn't working correctly. – user3361789 Jan 25 '17 at 17:10
  • Hello again, I have another question about this topic. It is passing both parameters that I want but it is not proceeding and I am not sure why. Articleid isn't listed by $_POST – user3361789 Jan 26 '17 at 01:13
  • If you post your javascript code then maybe someone can help. That is where your probelm is probably located – RiggsFolly Jan 26 '17 at 09:02
  • In your "Apply changes" code you are checking if articleid is set in the post. Like you said, it is not there. Add a hidden field to your form, for example – Daisuke Tahara Jan 26 '17 at 15:04
0

The Update query needs to include the data variable names . Query needs to be as follows:

    $updatesql="UPDATE blogarticle SET
 articletitle='$articletitle', articletext='$articletext' WHERE articleid='$articleid'";
Neha Parab
  • 76
  • 7