简体   繁体   English

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

[英]php Update query won't update database

I am working on a project and right now I am making a system where I edit posts with ckeditor. 我正在做一个项目,现在正在制作一个系统,在其中可以使用ckeditor编辑帖子。 If I edit text with ckeditor it doesn't update and I don't see any errors telling me what is wrong. 如果我使用ckeditor编辑文本,它不会更新,并且看不到任何错误告诉我什么地方不对。 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';");
        }

}
?>

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 . 例如,使用$text代替使用$content In your SQL, use :text and :id instead. 在您的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

Personally, I don't like to use bindParam as it seems unnecessary. 就个人而言,我不喜欢使用bindParam因为这似乎是不必要的。 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: 如果SQL相对较短,则更好:

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

在此行中用text替换content

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM