2017-01-05 67 views
1

我不知道爲什麼,但在我的圖像上傳腳本中,我的數據都沒有輸入數據庫。這是我一直在使用的腳本,但我最近添加到if(isset))語句來查看是否檢查了某些複選框。圖像正在上傳到服務器,但數據庫表保持空白。任何線索?我沒有收到任何錯誤。數據沒有進入數據庫

if(isset($_POST['submit'])) { 
    $count = count($_FILES['img_file']['name']); 

    for($i = 0; $i < $count; ++$i){ 
     $img_name = $_POST['img_name']; 
     $img_name = str_replace(' ', '_', $img_name); 
     $img_album = $_POST['img_album']; 
     $img_album = str_replace(' ', '_', $img_album); 
     $img_photographer = $_POST['img_photographer']; 
     $img_location = $_POST['img_location']; 
     if(isset($_POST['horror'])) { $horror = "1"; } else { $horror = "0"; } 
     if(isset($_POST['occult'])) { $occult = "1"; } else { $occult = "0"; } 
     if(isset($_POST['goth'])) { $goth = "1"; } else { $goth = "0"; } 
     if(isset($_POST['industrial'])) { $industrial = "1"; } else { $industrial = "0"; } 
     if(isset($_POST['fashion'])) { $fashion = "1"; } else { $fashion = "0"; } 
     if(isset($_POST['fetish'])) { $fetish = "1"; } else { $fetish = "0"; } 
     if(isset($_POST['avante-garde'])) { $avanteGarde = "1"; } else { $avanteGarde = "0"; } 
     if(isset($_POST['cosplay'])) { $cosplay = "1"; } else { $cosplay = "0"; } 
     if(isset($_POST['nude'])) { $nude = "1"; } else { $nude = "0"; } 
     $file_name = $_FILES["img_file"]["name"][$i]; 
     $file_ext = end((explode(".", $file_name))); 
     $target = $_SERVER['DOCUMENT_ROOT']."/gallery/"; 
     $img_rename = $img_name . '_' . $i . '.' . $file_ext; 
     $target = $target . $img_rename; 
     if(move_uploaded_file($_FILES['img_file']['tmp_name'][$i], $target)){ 
      mysqli_query($conn, "INSERT INTO gallery_img (img_name, img_album, img_photographer, img_location, horror, occult, goth, industrial, fashion, fetish, avante-garde, cosplay, nude, file_location) VALUES ('$img_name', '$img_album', '$img_photographer', '$img_location', '$horror', '$occult', '$goth', '$industrial', '$fashion', '$fetish', '$avanteGarde', '$cosplay', '$nude', '$img_rename')") ; 
      echo '<div class="alert alert-success margin-top">Image "'.$file_name.'" successfully uploaded and renamed to '.$img_rename.'.</div>'; 
     }else { 
      echo '<div class="alert alert-danger margin-top">Sorry, there was a problem uploading your images.</div>'; 
     } 
    } 
} 
+2

你再也檢查代碼中的錯誤。這就是爲什麼你不知道你的錯誤是什麼。 –

+0

該網站啓用了錯誤檢查功能,並且已通過語法檢查程序運行。沒有錯誤。 –

+2

兩者都不會顯示mysqli錯誤'mysqli_error($ conn)' – nogad

回答

2

您顯然沒有檢查您的查詢中的錯誤。

請注意你的一列的連字符?看起來其他人可能沒有滾動到正確的位置(足夠)看到它,並通知你。

  • avante-garde

MySQL是解釋,作爲:

  • avante減去garde和思考你想要做的數學。它應該使用下劃線重新命名,如同爲其他某些列所做的那樣,或者使用蜱包裹它。

即:

`avante-garde` 

順便說一句,(我不是在批評);那個單詞實際上拼寫爲"avant-garde",所以請確保它實際上是實際的名稱。無論哪種情況,它都會使你失敗。

  • 注:我真的希望這不是你一個錯字和你/用下劃線畢竟。

在條件語句中對查詢使用錯誤檢查會有所幫助。

I.e.和可變分配給它:

$query = mysqli_query($conn, "INSERT INTO gallery_img (...) VALUES (...)"); 

然後

if($query){ 
    echo "Success"; 
} else { 
echo "Error: " . mysqli_error($conn); 
} 

另一件事。確保列類型正確並且長度正確。如果長度不夠長以容納數據,MySQL可能會自動失敗。

是否使用準備好的語句;你的代碼目前對SQL注入是開放的。


腳註:

你可能要考慮使用三元運算符,而不是一些/所有這些if{...} else{...}報表,再加上它的很多短碼。

實施例:

$action = (empty($_POST['action'])) ? 'default' : $_POST['action']; 

當然,可以用isset替換empty


有指出價值的東西以及可能(也)防止您的查詢從執行,這是其查詢地點。

你有它在以下條件。如果上傳失敗,查詢也會失敗。

if(move_uploaded_file($_FILES['img_file']['tmp_name'][$i], $target)){ 
    mysqli_query($conn, "INSERT INTO gallery_img (...) VALUES (...)"); 
    echo '<div class="...">Success...</div>'; 
}else { 
    echo '<div class="...">Error...</div>'; 
} 

幾個可能的原因上傳失敗,可能是下列任何一個:

  • 文件(S)的(是)太大
  • 權限設置爲要寫入的文件夾。
  • 錯字(個),該文件(一個或多個)輸入端(S)
  • 其他

參考用於上載錯誤代碼/消息:

+0

啊。我沒有意識到它會被解釋爲這樣,這就是爲什麼我的其他人是'avanteGarde',我在那裏發現了它,但是沒有到你指出的地方,現在像一種魅力一樣工作,感謝其他的指針。仍在開發中,所以我肯定會對你的提示做一些改動。謝謝。 –

+1

@JesseElser你非常歡迎Jesse,很高興得到了幫助; *歡呼* –

+0

@JesseElser關於連字符的補充說明。這也適用於PHP中的變量。 I.e .:'$ a-b' - Php也會將其解釋爲數學運算;記住這一點 ;-) –