php更新查询不会更新数据库

时间:2016-03-07 14:55:01

标签: php pdo

我正在开展一个项目,现在我正在创建一个用ckeditor编辑帖子的系统。如果我使用ckeditor编辑文本,它不会更新,我也没有看到任何错误告诉我什么是错的。如果可以,请帮助我。

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

}
?>

2 个答案:

答案 0 :(得分:1)

在命名变量,数据库字段和输入名称时保持一致。你最终会减少错误。例如,使用$content而不是$text。在您的SQL中,请改用:text:id

$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

就个人而言,我不喜欢使用bindParam,因为它似乎没必要。另一种方法是:

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

如果SQL相对较短,那就更好了:

$stmt = $dbcon->prepare("UPDATE content SET contentTitle = ?, contentText = ? WHERE contentId = ?");
$stmt->execute(array($title, $text, $id)); // the order matters

答案 1 :(得分:0)

在此行中将content替换为text

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