2013-07-23 140 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

相关问题