标签:style blog http java color os
本例实现问答的板块。
相关sql:
1.questions: {id, asker, title, content, keyword, time};
2.answers : {id, answer, question_id, content, time};
问题前台:question.php
1 <?php 2 $str = "SELECT * FROM questions"; 3 $res = mysql_query($str); 4 for($i=1; $res && $row=mysql_fetch_array($res); $i++){ 5 echo "<div>"; 6 echo "$row[id]<br/>"; 7 echo "<a href=‘answer.php?question_id=$row[id]‘>$row[title]</a><br/>"; 8 echo "$row[content]<br/>"; 9 echo "$row[keyword]\t\t$row[time]\t\t$row[asker]<br/>"; 10 echo "<br/><br/></div>"; 11 } 12 ?>
回答前台:answer.php
1 <?php 2 include "process.php"; 3 include "login.php"; 4 // question 5 if(@!$_GET["question_id"]) 6 exit(‘非法访问!点击此处 <a href="javascript:history.back(-1);">返回</a><br/>‘); 7 else{ 8 $str = "SELECT * FROM questions WHERE id=‘$_GET[question_id]‘"; 9 $res = mysql_query($str); 10 $row = mysql_fetch_array($res); 11 echo "<div><h1>问题</h1></div>"; 12 echo "<div>"; 13 echo "$row[id]<br/>"; 14 echo "$row[title]<br/>"; 15 echo "$row[content]<br/>"; 16 echo "$row[keyword]\t\t$row[time]\t\t$row[asker]<br/>"; 17 echo "<br/><br/></div>"; 18 } 19 // answers 20 echo "<div><h1>回答</h1></div>"; 21 $str = "SELECT * FROM answers WHERE question_id=‘$_GET[question_id]‘"; 22 $res = mysql_query($str); 23 for($i=1; $res && $row=mysql_fetch_array($res); $i++){ 24 echo "<div>"; 25 echo "$row[content]<br/>"; 26 echo "$row[time]\t\t$row[answer]<br/>"; 27 echo "<br/><br/></div>"; 28 } 29 ?> 30 31 <form action="process.php?question_id=<?php echo "$_GET[question_id]"; ?>" method="post" /><br/> 32 回答:<textarea name="content" rows=10 cols=20></textarea><br/> 33 <input type="submit" name="answer_submit", value="submit" /> 34 </form>
Q&A后台:process.php
1 // submit the ask form 2 if(@$_POST["ask_submit"]){ 3 session_start(); 4 if(empty($_SESSION["username"])) 5 exit("您还未登录!<a href=‘javascript:history.back(-1);‘>返回</a>"); 6 7 $str = "SELECT COUNT(*) FROM questions"; 8 $res = mysql_fetch_array( mysql_query($str) ); 9 $str = "INSERT INTO questions VALUES($res[0]+1, ‘$_SESSION[username]‘, ‘$_POST[title]‘, ‘$_POST[content]‘, ‘$_POST[keyword]‘, now())"; 10 //$str = "INSERT INTO questions(asker, title) VALUES(‘$_POST[asker]‘, ‘$_POST[title]‘)"; 11 if(mysql_query($str)) 12 exit("提交成功 点击此处 <a href=‘javascript:history.back(-1);‘>返回</a>"); 13 else 14 exit("提交失败 点击此处 <a href=‘javascript:history.back(-1);‘>返回</a>"); 15 } 16 17 // submit the answer form 18 if(@$_POST["answer_submit"]){ 19 session_start(); 20 if(empty($_SESSION["username"])) 21 exit("您还未登录!<a href=‘javascript:history.back(-1);‘>返回</a>"); 22 23 $str = "SELECT COUNT(*) FROM answers"; 24 $res = mysql_fetch_array( mysql_query($str) ); 25 $str = "INSERT INTO answers VALUES($res[0]+1, ‘$_SESSION[username]‘, ‘$_GET[question_id]‘, ‘$_POST[content]‘, now())"; 26 if(mysql_query($str)) 27 exit("提交成功 点击此处 <a href=‘javascript:history.back(-1);‘>返回</a>"); 28 else 29 exit("提交失败 点击此处 <a href=‘javascript:history.back(-1);‘>返回</a>"); 30 }
标签:style blog http java color os
原文地址:http://www.cnblogs.com/fashare/p/3860783.html