SQL UPDATE .. SET ..无效

时间:2018-01-03 16:04:58

标签: php html sql

我遇到了问题。这是我编辑文章的代码。我不知道为什么,但我的更新不起作用。它只是重定向到../index.php。我尝试了一切( 引号和东西也.. .. ..你能帮助我吗?我只想编辑/更新标题和内容。代码如下。非常感谢。

editarticle.php

<?php

session_start();

include_once('../connect.php');
include_once('../includes/article.php');
header('Content-type: text/html; charset=utf-8');

if(isset($_SESSION["user_id"])){
 $query=$pdo->prepare("SELECT * FROM users WHERE id = " 
 .$_SESSION["user_id"] );
  $query->execute();
  $row=$query->fetch(PDO::FETCH_ASSOC);

  if($row['privileges']==1){
   $query = $pdo->prepare("SELECT * FROM `articles` WHERE article_id='" . 
   $_POST['id'] . "'");
   $query->execute();
   $row=$query->fetch(PDO::FETCH_ASSOC);
    var_dump($row);

     if(isset($_POST['editarticle'])){
      if(isset($_POST['title'], $_POST['content'])){
        $title= $_POST['title'];
        $content= nl2br($_POST['content']);

          $query= $pdo->prepare("UPDATE `articles` SET article_title=" 
     .$_POST['title'] .",article_content=" .$_POST['content'] ." WHERE 
      article_id='" . $_POST['id'] . "'");
          $query->execute();
          $row=$query->fetch(PDO::FETCH_ASSOC);

          header('Location: ../index.php');
       }
   }
  ?>

  <html>
   <head>
    <title>CMS Tutorial</title>
    <link rel="stylesheet" href="assets/style.css" />
  </head>
  <body>
  <div class="container">
    <br/>
    <h4>Upravit prispevok</h4>

    <?php if(isset($error)){
    echo $error;
    }?>
    <? $query = $pdo->prepare("SELECT * FROM `articles` WHERE article_id='" 
    . $_POST['id'] . "'");
    $query->execute();
    $row=$query->fetch(PDO::FETCH_ASSOC);
    ?>

    <form action="editarticle.php" method="post" autocomplete="off" 
     class="addarticle">
      <input type="text" name="title" value="<?php echo 
      ucwords($row['article_title']);?>"/><br/><br/>
      <textarea rows="15" cols="50"  name="content"><?php echo 
       ucwords($row['article_content']);?></textarea><br/><br/>
      <input type="submit" name="editarticle" value="Upraviť prispevok"/>
     </form>
    </div>
   </body>
   </html>

  <?php
} } else{
 header('Location: index.php');
}

?>

1 个答案:

答案 0 :(得分:1)

这是因为引号不起作用,但更大的问题是查询准备本身,请查看this