2011-06-24 43 views
0

我一直在努力工作幾天,才能上傳到我的網站的管理部分,並且幾乎在那裏......! 我在我的數據庫中有一個表,名爲測試4個字段 - id(int),title(varchar),desc(varchar)和photo(varchar) - 照片字段表示服務器上圖像的來源。 我的代碼是:重複空行在文件上傳時輸入到mysql

<?php include 'dbc.php'; page_protect(); 

if(!checkAdmin()) {header("Location: login.php"); 
exit(); 
} 

$host = $_SERVER['HTTP_HOST']; 
$host_upper = strtoupper($host); 
$login_path = @ereg_replace('admin','',dirname($_SERVER['PHP_SELF'])); 
$path = rtrim($login_path, '/\\'); 

foreach($_GET as $key => $value) { 
    $get[$key] = filter($value); 
} 

foreach($_POST as $key => $value) { 
    $post[$key] = filter($value); 
} 
?> 

<?php 

$target = "images/test/"; 
$target = $target . basename($_FILES['photo']['name']); 

$title = mysql_real_escape_string($_POST['title']); 
$desc = mysql_real_escape_string($_POST['desc']); 
$pic = "images/test/" .(mysql_real_escape_string($_FILES['photo']['name'])); 

mysql_query("INSERT INTO `test` (`title`, `desc`, `photo`) VALUES ('$title', '$desc', '$pic')") ; 

if(move_uploaded_file($_FILES['photo']['tmp_name'], $target)) 
{ 

echo "The file ". basename($_FILES['uploadedfile']['name']). " has been uploaded, and your information has been added to the directory"; 
} 
else { 
echo "Sorry, there was a problem uploading your file."; 
} 
?> 

<form enctype="multipart/form-data" action="uploader.php" method="POST"> 
Title: <input type="text" name="title"><br> 
Description: <input type="text" name = "desc"><br> 
Photo: <input type="file" name="photo"><br> 
<input type="submit" value="Add"> 
</form> 

出於某種原因,當該行進入MySQL是插入一個重複的空行,以便該表的樣子:

ID    Title    Desc     Photo 
15               images/test/ 
16    test title   test description  images/test/test1.jpg 

沒有任何理由,這是從發生上面的代碼 - 它相當簡陋,但考慮到它的痛苦和鬥爭,以獲得這個工作,我真的不能再次面對開始!

在此先感謝您的幫助。

JD

回答

1

,據我看到,你的數據庫代碼執行時,形式是第一次加載,但沒有文件上傳過。所以yo'll需要移動內部

if(move_uploaded_file($_FILES['photo']['tmp_name'], $target)) 

整個代碼數據庫相關的代碼:

<?php include 'dbc.php'; page_protect(); 

if(!checkAdmin()) {header("Location: login.php"); 
exit(); 
} 

$host = $_SERVER['HTTP_HOST']; 
$host_upper = strtoupper($host); 
$login_path = @ereg_replace('admin','',dirname($_SERVER['PHP_SELF'])); 
$path = rtrim($login_path, '/\\'); 

foreach($_GET as $key => $value) { 
    $get[$key] = filter($value); 
} 

foreach($_POST as $key => $value) { 
    $post[$key] = filter($value); 
} 
?> 

<?php 
if($_FILES['photo']) //check if we uploading a file 
{ 
    $target = "images/test/"; 
    $target = $target . basename($_FILES['photo']['name']); 

    $title = mysql_real_escape_string($_POST['title']); 
    $desc = mysql_real_escape_string($_POST['desc']); 
    $pic = "images/test/" .(mysql_real_escape_string($_FILES['photo']['name'])); 
if(move_uploaded_file($_FILES['photo']['tmp_name'], $target)) 
{ 
    mysql_query("INSERT INTO `test` (`title`, `desc`, `photo`) VALUES ('$title', '$desc', '$pic')") ;  

    echo "The file ". basename($_FILES['uploadedfile']['name']). " has been uploaded, and your information has been added to the directory"; 
} 
else 
{ 
    echo "Sorry, there was a problem uploading your file."; 
    var_dump($_FILES); //for debug purposes 
} 
} 
?> 

<form enctype="multipart/form-data" action="uploader.php" method="POST"> 
Title: <input type="text" name="title"><br> 
Description: <input type="text" name = "desc"><br> 
Photo: <input type="file" name="photo"><br> 
<input type="submit" value="Add"> 
</form> 
+0

嗨 - 試過,但現在它似乎並沒有做任何事情 - 無論是張貼文件或進入數據庫表......? – JD2011

+0

我的不好,你需要檢查是否上傳文件沒有得到錯誤信息。如果文件尚未上傳,我添加了一些調試信息。如果這段代碼無法正常工作 - 請告訴我輸出問題,我會盡力解決問題出在哪裏 – Greenisha

+0

嗨,謝謝你回到我身邊。輸出是 抱歉,上傳您的file.array(1){[「photo」] => array(5){[「name」] => string(12)「IMG_0161.jpg」[「type 「] => string(10)」image/jpeg「[」tmp_name「] => string(14)」/ tmp/phpM9Qa9k「[」error「] => int(0)[」size「] => int 35516)}} – JD2011

相關問題