2012-03-02 52 views
0

我創建了一個表單,用戶需要在第一個文本字段處插入名稱並在第二個文本字段一個(一些信息)。 這是我爲我的表單代碼:獲取文本框的內容,從中創建一個.txt文件並將其存儲到數據庫中

echo "<form method='post' action='insert.php?selected=$openFolder&id=$student'> 
     Information name:</br> <input name='infoName' type='text' /><br /> 
     Information:<br /> 
     <textarea name='text' rows='15' cols='60'> 
     </textarea><br /> 
     <input type='submit' name='submit' value='Submit Information'/> 
     </form>"; 

到目前爲止好。

現在,當表單提交我需要創建一個文件:

文件名 - >名用戶在「infoName」文本字段

插入然後插入內容'文本'文本字段放入此文件並將整個文件存儲到我的數據庫中。

這裏是我的代碼:

if (isset($_POST['submit']))  
{  
//Make sure that a file name is given by the user  
if (!$_POST['infoName'])  
{  
echo "<center><font color='red'>You must complete the Information name field</font></center>";    
}  
else  
{  
$informationName = $_POST['infoName']; 
$informationContent = $_POST['text']; 

$newFile = fopen($informationName,"w");   
$content = fwrite($newFile,$informationContent . "\n"); 
$content = addslashes($content);   
fclose($newFile); 

$theSize = filesize($informationName); 

if(!get_magic_quotes_gpc())  
{  
    $informationName = addslashes($informationName);  
}  

$query = "INSERT INTO $openFolder (studentID, name, size, type, content, insertedBy) ". 
    "VALUES ('$student','$informationName', '$theSize', 'txt', '$content', '$username')"; 

mysql_query($query) or die('Error, query failed');          


}  
}   

我的問題:
1)文件被創建正確的,並且存儲在我的數據庫,但與沒有它的正確內容。出於某種原因,我沒有插入正確的文本到文件中...

2)除了存儲在我的數據庫中的文件,在我的目錄中創建了一個文件,我不希望發生這種情況。

請幫助我。隨意對我的代碼進行任何更改! 請告訴我什麼即時做錯了!

回答

0

我看到原因是聲明$content = addslashes($content);fwrite返回寫入的字節數,而不是數據本身。 正確的語句應該是$informationContent = addslashes($informationContent);,並且應該在您的if(!get_magic_quotes_gpc())條件中。同樣改變你的查詢並按照建議替換變量。

此外請確保您檢查fwrite的返回值。如果沒有寫入(=== FALSE),則不要繼續進行並報告錯誤。許多錯誤難以調試(像這種情況),只是因爲我們經常編寫假設編程。 :)

相關問題