编辑/删除帖子与PHP无法正常工作

时间:2015-02-15 20:30:42

标签: php mysql

首先,对于长代码感到遗憾,但我认为这很重要。所有这些都是关于在我的页面上编辑和删除新闻。但我不知道为什么它说:语法错误,意外(最后)elseif。有人可以帮我解决这个问题吗?此外,当我试图删除elseif以知道将出现什么它没有显示任何东西。只有CSS背景样式,但没有其他类似形式等。

编辑:现在没有错误,但此处没有任何内容:http://armando.wc.lt/admin/pages/edit.php

  <?php
          $servername = "localhost";
          $username = "u296093122_admin";
          $password = "a";
          $dbname = "u296093122_datab";


        if(isset($a)){
            $conn = new mysqli($servername, $username, $password, $dbname);

          if ($conn->connect_error) {
           die("->Pripojenie neúspešné: " . $conn->connect_error);
                                    }
            $sql = "SELECT id, nadpis, timestamp FROM Akcie ORDER BY timestamp DESC";
            $result = $conn->query($sql);

        if (mysql_num_rows($result) > 0){
            while($row = mysql_fetch_object($result))
                {
    ?>

 <font size="-1"><b><?php echo $row->nadpis; ?></b> <i><?php echo $row->timestamp; ?></i></font>
<br>
  <font size="-2"><a href="edit.php?a=edit&id=<?php echo $row->id; ?>">upraviť</a> | 
  <a href="edit.php?a=delete&id=<?php echo $row->id; ?>">vymazať</a></font>

<?php
    }
?>

 <font size="-2">No news in the database</font>

<?php
    }
mysql_close($conn);
    }elseif($a == 'edit'){
        if(!isset($update)){
    $conn = new mysqli($servername, $username, $password, $dbname);

    $sql = "SELECT nadpis, obsah FROM Akcie WHERE id = '$id'";
    $result = $conn->query($sql);

mysql_fetch_object($result);
?>

 <form name="form2" method="post" action="edit.php?a=edit&id=<?php echo($id) ?>&update=1">
 <table width="50%" border="0" cellspacing="0" cellpadding="0">

  <tr> 
  <td>Nadpis</td>
  <td><input name="nadpis" type="text" id="nadpis" value="<?php echo($row->nadpis) ?>"></td>
</tr>
<tr> 
  <td>Obsah</td>
  <td><textarea name="obsah" id="obsah" value="<?php echo($row->obsah) ?>"></textarea></td>
</tr>
<tr> 
  <td colspan="2"><div align="center">
      <input name="hiddenField" type="hidden" value="update">
      <input name="add" type="submit" id="add" value="Update">
    </div></td>
</tr>
</table>
</form>

 <?php
}else{
    $sql = "UPDATE Akcie SET nadpis = '$nadpis', obsah = '$obsah', timestamp = NOW() WHERE id = '$id'";
    $result = $conn->query($sql);

    if(!$result){
        echo('Error updating news item: ' . $mysql_error());
        exit();
    }else{
        mysql_close($conn);
        echo('Update successful!');
        }
    } }elseif($a == 'delete'){
        $conn = new mysqli($servername, $username, $password, $dbname);

    $sql = "DELETE FROM Akcie WHERE id = '$id'";
    $result = $conn->query($sql);


    if(!$result){
        echo('Error deleteing news item: ' . $mysql_error());
         exit();
            }

    mysql_close($conn);
    echo('News item deleted.');
    }
     ?>

1 个答案:

答案 0 :(得分:2)

打开/关闭括号似乎有些混乱。花一些时间检查订单。 一般来说,尽量避免混合php逻辑和标记。