2013-07-23 139 views
-1

我創建了一個登錄頁面和一個管理頁面。管理員頁面創建新帖子

在管理頁面上,我創建了兩個文本字段,第一個是標題,第二個是內容。我也創建了一個提交按鈕。

我有一個名爲node的表的數據庫。

在該表中我創建了:id(1,2,3 ...),author_id(1 = me),標題(它是標題),文本(它是內容)和日期(發佈時)。

現在我想要的是當我按提交按鈕時,作者,標題,文本和日期作爲新帖子出現在頁面上。當我按下「發佈您的問題」按鈕時,這幾乎是一回事。

Here's the webpage

我創建從SQL 3點測試新聞發佈,它的工作,但它要好得多,如果我可以從管理頁面做到這一點。 :)

我非常感謝,如果你能寫得很好,我從來沒有這樣做,我真的不知道如何做到這一點。

之前有人提供:我不想創建joomla或drupal網站。

最終工作的版本:(感謝半絲)

<?php 
ob_start(); 
session_start(); 
?> 
<meta charset="utf-8"> 
<link rel="stylesheet" href="style.css" type="text/css" media="screen"/> 
<?php 
if (isset($_COOKIE["user"])) 
{ 
    include('sql.php'); 
    $username =$_COOKIE["user"]; 
    $user = mysql_fetch_row(mysql_query("select * from user where 
    name='$username'")); 
     if (empty($_GET['action'])) 
     { 
      //This is where you add to database 
      if (!empty($_POST['submit']) && $_POST['submit']=='submit'){ 
      //do any validation if required, like not empty header and not empty body etc here 
      $title = mysql_real_escape_string($_POST['title']); 
      $text = mysql_real_escape_string($_POST['text']); 
      } 
      //insert to the database 
      mysql_query("INSERT INTO hearthstone.node 
      (author_id , title , text , date) 
      VALUES ('1', '$title', '$text', NOW());"); 
     } 
      ?> 
<div id="adminmenu">Logged in as <b> 
<?php print $user[1];?></b> 
<span style="margin: 15px;"><br></span> 
<?php include('logout.php'); ?> 
<span style="margin: 15px;"></span> 
<form method="post" action=""> 
<p>Title:</p> 
<p><input type="text" maxlength="255" name="title" id="title" size="60"></p> 
<p>Body content:</p> 
<p><textarea cols="60" rows="20" name="text" id="text"></textarea></p> 
<p><input type="submit" name="submit" id="submit" value="submit"></p> 
</form> 
</div> 

      <?php 
    } 
else 
{ 
     include('login.php'); 
} 
?> 
+0

您可以將代碼發佈到您的管理頁面嗎? – bansi

+0

我現在加入.... – Phoenixy

回答

1

試試這個。代碼的細節。

<?php 
ob_start(); 
session_start(); 
?> 
<meta charset="utf-8"> 
<link rel="stylesheet" href="style.css" type="text/css" media="screen"/> 
<?php 
if (isset($_COOKIE["user"])) { 
    include ('sql.php'); 
    $username = $_COOKIE["user"]; 
    $user = mysql_fetch_row(mysql_query("select * from user where 
    name='$username'")); 
    if (empty($_GET['action'])) { 
     //This is where you add to database 
     if (!empty($_POST['submit']) && $_POST['submit']=='Submit'){ 
      //do any validation if required, like not empty header and not empty body etc here 
      $header = mysql_real_escape_string($_POST['header']); 
      $body = mysql_real_escape_string($_POST['body']); 
      //insert to the database 
      mysql_query("INSERT INTO node (author_id,header,text,`date`) VALUES (1,$header,$body,now());"); 
     } 
?> 
<div id="adminmenu">Logged in as <b> 
<?php print $user[1]; ?></b> 
<span style="margin: 15px;"><br></span> 
<?php include ('logout.php'); ?> 
<span style="margin: 15px;"></span> 
<form method="post" action=""> 
<p>Title:</p> 
<p><input type="text" maxlength="255" name="header" id="title" size="60"></p> 
<p>Body content:</p> 
<p><textarea cols="60" rows="20" name="body" id="content"></textarea></p> 
<p><input type="submit" name="submit button" id="submit" value="Submit"></p> 
</form> 
</div> 

      <?php 
    } 
} else { 
    include ('login.php'); 
} 
?> 

重要:因爲它是貶值嘗試使用庫MySQLi或PDO

該是改變的代碼不使用MySQL的函數。我在保存到數據庫的代碼中添加了註釋。我還添加了<form>元素到HTML。該代碼未經測試,因爲我的服務器不再支持mysql函數。我已經盡力從你的問題中使用db名稱和字段名稱。還請注意我沒有添加任何錯誤檢查。

如果您遇到任何問題,請讓我知道。

+1

任何錯誤信息?如果是這樣的話?上述查詢的工作? – bansi

+0

它現在正在工作,感謝您的幫助,您發佈的內容很好,只需更改一下,即可將第一篇文章更新爲最終版本。 – Phoenixy

相關問題