2016-03-07 128 views
0

我正在一个项目上工作,现在我正在制作一个系统,在这里我用ckeditor编辑帖子。如果我用ckeditor编辑文本,它不会更新,我也没有看到任何错误告诉我什么是错的。如果可以,请帮助我。PHP更新查询不会更新数据库

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

} 
?> 
+1

“*我没有看到任何错误,告诉我什么是错*” - 你找错误?可能不会,因为所有这些'header()'调用都会引发警告。和':text'!=':content' – Qirel

+0

你真的使用两种方法吗?为什么你需要得到id的方法,如果你使用post方法(is_numeric)检查它是否是数字? – NormundsP

+0

为什么不只是'$ stmt-> execute();'? – Milan

回答

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 
+0

Mikey感谢你的反应,但它没有奏效。我尝试了所有给你的选项,但没有一个能够工作。 –

+0

可能有很多原因。我建议你先[打开你的错误](http://stackoverflow.com/questions/1053424/how-do-i-get-php-errors-to-display)(如果它还没有)。此外,还可以在代码的不同位置打印出您的预期变量,以查看其失败的位置。顺便说一句,我只注意到它应该是'$ id = $ _POST ['id'];'而不是'$ id = $ _GET ['id'];'当你处理你的POST提交时。同时检查form标签的'method'属性是否是'post'。 – Mikey

+0

另外,看看[如何检查数据库错误](http://stackoverflow.com/a/8776392/1022914)上的这个答案。 – Mikey

0

在此行中与text替换content

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

Gouda Elafly它没有解决我的问题,但谢谢你的反应。 –