2016-03-07 122 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它沒有解決我的問題,但謝謝你的反應。 –