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';");
        }

}
?>
darkomen
  • 3,789
  • 8
  • 28
  • 56
Remco de Baas
  • 51
  • 2
  • 11

2 Answers2

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
Mikey
  • 6,432
  • 4
  • 20
  • 36
  • Mikey thanks for reacting but it didn't work out. I tried all options you have given me but none did work. – Remco de Baas Mar 07 '16 at 21:45
  • There can be many reasons. I'd suggest you first [turn on your errors](http://stackoverflow.com/questions/1053424/how-do-i-get-php-errors-to-display) (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`. – Mikey Mar 07 '16 at 22:36
  • Also, take a look at this answer on [how to check for database errors](http://stackoverflow.com/a/8776392/1022914). – Mikey Mar 07 '16 at 22:42
  • Mikey I need to have the GET id because I need to get the id to get the content shown in the editor – Remco de Baas Mar 08 '16 at 09:07
  • I also checked out the how to check for database errors. I did try it out but it didn't put out any errors. – Remco de Baas Mar 08 '16 at 09:16
  • 1
    Mikey I realized that there wasn't any if isset submit so that's why the database wouldn't update – Remco de Baas Mar 10 '16 at 08:28
0

replace content with text in this line:

$stmt->bindParam(':text', $content, PDO::PARAM_STR);
Gouda Elalfy
  • 6,337
  • 1
  • 22
  • 37