0

I am working on a project and right now I am making a system where I edit posts with ckeditor. If I edit text with ckeditor it doesn't update and I don't see any errors telling me what is wrong. Help me if you can.

 <html>
<head>
    <link href='https://fonts.googleapis.com/css?family=Titillium+Web:400,300,200' rel='stylesheet' type='text/css'>
    <meta charset="utf-8">
    <script src="//cdn.ckeditor.com/4.5.7/standard/ckeditor.js"></script>
    <link rel="stylesheet" type="text/css" href="cke.css">
    <title>Nieuws</title>
</head>
<?php
include 'db.php';
include 'repeatForm.php';


if (isset($_POST['id'])) {
    if (is_numeric($_POST['id'])) {
        $id = $_GET['id'];
        $title = $_POST['cTitle'];
        $content = $_POST['ed1'];

        if ($title == '' || $content == '') {
            $error = 'Fout: vul alle velden in';

            //laat form zien
            repeatForm($id,$title,$content,$error);
        } else {
            $stmt = $dbcon->prepare("UPDATE content SET contentTitle = :title, contentText = :text WHERE contentId = :nummer");
            $stmt->bindParam(':title', $title, PDO::PARAM_STR);
            $stmt->bindParam(':content', $content, PDO::PARAM_STR);
            $stmt->bindParam(':nummer', $id, PDO::PARAM_STR);
            $stmt->execute($title,$content,$id);

            header("Location: index.php");
        }
    } else {
        echo "Fout";
        header("Location: index.php");
    }
}

else {
        if (isset($_GET['id']) && is_numeric($_GET['id']) && $_GET['id'] > 0) {
            $id = $_GET['id'];
            $query = $dbcon->query("SELECT * FROM content WHERE contentId='$id'");
            $r = $query->fetch();

            if ($r['contentId'] == $id) {
                $title = $r['contentTitle'];
                $content = $r['contentText'];
            }
            //laat form zien met variabelen
            repeatForm($id,$title,$content);
        } else {
            echo "No results";
            header("refresh:1.5;url='index.php';");
        }

}
?>
5
  • 1
    "I don't see any errors telling me what is wrong" - are you looking for errors? Probably not, because all those header() calls would throw warnings. And :text != :content Commented Mar 7, 2016 at 14:57
  • Do you really use both methods? Why you need get method for id, if you check is it number using post method (is_numeric)? Commented Mar 7, 2016 at 14:59
  • why not just $stmt->execute();? Commented Mar 7, 2016 at 15:00
  • Normis I need the get method to get the right number out of the database to show the right content. Commented Mar 7, 2016 at 21:22
  • Milan I already tried this but it didn't work. Thank you for answering. Commented Mar 7, 2016 at 21:25

2 Answers 2

1

Stay consistent when you name your variables, database fields, and input names. You'll end up making a lot less mistakes. For example, instead of using $content, use $text. In your SQL, use :text and :id instead.

$stmt = $dbcon->prepare("UPDATE content SET contentTitle = :title, contentText = :text WHERE contentId = :id");
$stmt->bindParam(':title', $title, PDO::PARAM_STR);
$stmt->bindParam(':text', $text, PDO::PARAM_STR);
$stmt->bindParam(':id', $id, PDO::PARAM_INT); // expecting an integer, not string 
$stmt->execute(); // no need to pass parameters again

Personally, I don't like to use bindParam as it seems unnecessary. Another way is to do:

$stmt = $dbcon->prepare("UPDATE content SET contentTitle = :title, contentText = :text WHERE contentId = :id");
$stmt->execute(array(':title' => $title, ':text' => $text, ':id' => $id));

Or better if the SQL is relatively short:

$stmt = $dbcon->prepare("UPDATE content SET contentTitle = ?, contentText = ? WHERE contentId = ?");
$stmt->execute(array($title, $text, $id)); // the order matters
Sign up to request clarification or add additional context in comments.

6 Comments

Mikey thanks for reacting but it didn't work out. I tried all options you have given me but none did work.
There can be many reasons. I'd suggest you first turn on your errors (if it is not already on). And also, print out your expected variables at different points in your code to see where it fails. By the way, I just noticed that it should be $id = $_POST['id']; instead of $id = $_GET['id']; when you process your POST submission. Also check that the method attribute of your form tag is a post.
Also, take a look at this answer on how to check for database errors.
Mikey I need to have the GET id because I need to get the id to get the content shown in the editor
Mikey I realized that there wasn't any if isset submit so that's why the database wouldn't update
|
0

replace content with text in this line:

$stmt->bindParam(':text', $content, PDO::PARAM_STR);

1 Comment

Gouda Elafly it did not solve my problem but thanks for reacting.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.