2011-12-28 105 views
0

我有一個簡單的頁面(下面)和一個表單。如果action ='ask.php',表單將提交。 (ask.php是當前頁面和下面的代碼)。當我將ask.php更改爲question.php(文件夾中的另一頁)時,表單不提交。爲什麼是這樣?數據放入數據庫之前表單是否重定向?表單在操作頁面發生變化時不會提交

ask.php code;

<?php session_start(); ?> 
     <!DOCTYPE html> 
     <html xmlns="http://www.w3.org/1999/xhtml"> 
     <head> 
     <title></title> 

     <script type='text/javascript' src='http://code.jquery.com/jquery-1.7.1.min.js'></script> 
     <script type='text/javascript' src='http://twitter.github.com/bootstrap/1.4.0/bootstrap-modal.js'></script> 
     <link rel="stylesheet" href="../css/bootstrap.css" type="text/css" media="screen" /> 

     </head> 
      <body> 
     <?php include '../css/bar.php'; ?> 
    <div id='content'> 
<?php include '../nav.php'; 
?> 



<?php 


if (isset($_POST['submit'])){ 
include '../connect.php'; 
$question=mysql_real_escape_string($_POST['question']); 
$detail=mysql_real_escape_string($_POST['detail']); 
$date=date("d M Y"); 
$time=time(); 
$user=$_SESSION['id']; 
$put=mysql_query("INSERT INTO questions VALUES ('','$question','$detail','$date','$time','$user','subject','0')"); 

$result=mysql_query("SELECT * FROM questions WHERE user='$user' AND time='$time'"); 
    while ($row = mysql_fetch_assoc($result)){ 

     $q=$row['id']; 

     } 
} 

?> 

<form method='POST' action='question.php'> //won't submit because action is question.php? 
    <p>Question:</p> 
    <p><input type='text' name='question' id='question' maxlength='200'></p> 
    <p>Add some detail (optional):</p> 
    <p><textarea id='detail' name='detail' ></textarea></p> 

    <p><input type='submit' value='submit' name='submit'></p> 
</form> 


</div> 
<?php include '../footer.php'; ?> 
</body> 

+1

您可以張貼到任何現有的頁面,但如果該頁面不處理貼值,什麼都不會從其他頁面的加載除了發生。 – jeroen 2011-12-28 01:21:35

+0

@jeroen我不介意question.php是否處理數據,但如果我將'ask.php'更改爲'question.php',它甚至不會被提交到數據庫中。如果數據被提交,爲什麼表單的操作很重要?在重定向之前不應該將它輸入到數據庫中嗎?如果我可以錯誤地發佈解決方案? – kirby 2011-12-28 01:40:50

+0

@ jeroen是在數據插入數據庫之前頁面正在被重定向的問題? – kirby 2011-12-28 01:45:38

回答

0

的Jeroen是正確的:

動作頁應包括這樣的信息,以獲取信息到數據庫:

第一部分和兩個使數據準備被插入到數據庫:

$question = mysql_real_escape_string($_POST['question']); // Part 1 
$detail = mysql_real_escape_string($_POST['detail']); // Part 2 
$con = mysql_connect("server","username","password"); 
mysql_select_db("YourDatabase", $con); 

$sql="INSERT INTO YourTable (question, details) VALUES ('$question','$detail')"; 
if (!mysql_query($sql,$con)) { 
    //Some error handling here, 
    // Stay away from using die in all but development 
} 
mysql_close($con) 

您應該測試FO與preg_match rmat,以檢查數據是在預期的格式。 希望有幫助

編輯!

這種連接方法已過時! 使用PDO進行數據庫連接。 一些文檔

PDO Article

PDO php docs

+1

將*插入到數據庫時唯一需要的清理是「mysql_real_escape_string」或「intval」。除此之外,特別是只需要HTML輸出的東西,應該在*讀取數據庫中的數據之後完成。 – ThiefMaster 2012-02-19 12:32:14

+0

沒有意識到,謝謝,我的帖子會改正。 (我曾聲稱你應該消毒帶標籤,修剪等等,我的不好)。 – 2012-02-19 14:13:38

+0

修剪不是一個壞主意,但是應該在輸出期間完成html編碼 - 更可能是'strip_tags'的'htmlspecialchars',因爲它使得數據無用於純文本環境(如文本電子郵件)。 – ThiefMaster 2012-02-19 14:49:01