2015-04-05 68 views
0

我已經有一個代碼 - 請在下面檢查。這段代碼工作正常,但我只是意識到它只會將文件添加到上傳文件夾中,並且不會向數據庫添加任何內容。有人能幫我填補空白嗎?我想使用一個數組並儘可能簡單。使用數組將多個圖像名稱插入數據庫

請向下滾動到執行代碼並將圖像放入文件夾的註釋。

uploader.php

<?php 
if (isset($_POST['submit'])) { 
$j = 0;  // Variable for indexing uploaded image. 

$target_path = "uploads/test/";  // Declaring Path for uploaded images. 

for ($i = 0; $i < count($_FILES['file']['name']); $i++) { 
// Loop to get individual element from the array 

$validextensions = array("jpeg", "jpg", "png");  
// Extensions which are allowed. 

$ext = explode('.', basename($_FILES['file']['name'][$i])); 
// Explode file name from dot(.) 

$file_extension = end($ext); 
// Store extensions in the variable. 

$target_path = $target_path . md5(uniqid()) . "." . $ext[count($ext) - 1]; 
// Set the target path with a new name of image. 

$j = $j + 1;  
// Increment the number of uploaded images according to the files in array. 

if (($_FILES["file"]["size"][$i] < 100000)  
// Approx. 100kb files can be uploaded. 

&& in_array($file_extension, $validextensions)) { 

if (move_uploaded_file($_FILES['file']['tmp_name'][$i], $target_path)) { 
// If file moved to uploads folder. 

?> 

<div id="noerror">Image <?php echo $j;?>-->Image Uploaded!</div> 

<?php 

// File was moved, so execute code here 

// In this code I would like to add file name to a DB 


} else {  // If File Was Not Moved. 
?> 

<div id="error">Image <?php echo $j;?>--> <b>Please Try Again!</b></div> 

<?php 
} 

} else {  // If File Size And File Type Was Incorrect. 
?> 

<div id="error">Image <?php echo $j;?>--> <b>Check file Size or Type</b></div> 

<?php 
} 
} 
} 
?> 

回答

0

您正在尋找這樣的事情:

mysqli_query("INSERT INTO `images` SET `image_name` = '".$_FILES['file']['name'][$i]."'") or die (mysqli_error()); 

你的代碼應該是:

請添加您在下面的查詢database field namesdatabase table name現在:

<?php 
if (isset($_POST['submit'])) { 
$j = 0;  // Variable for indexing uploaded image. 

$target_path = "uploads/test/";  // Declaring Path for uploaded images. 

for ($i = 0; $i < count($_FILES['file']['name']); $i++) { 
// Loop to get individual element from the array 

$validextensions = array("jpeg", "jpg", "png");  
// Extensions which are allowed. 

$ext = explode('.', basename($_FILES['file']['name'][$i])); 
// Explode file name from dot(.) 

$file_extension = end($ext); 
// Store extensions in the variable. 

$target_path = $target_path . md5(uniqid()) . "." . $ext[count($ext) - 1]; 
// Set the target path with a new name of image. 

$j = $j + 1;  
// Increment the number of uploaded images according to the files in array. 

if (($_FILES["file"]["size"][$i] < 100000)  
// Approx. 100kb files can be uploaded. 

&& in_array($file_extension, $validextensions)) { 

if (move_uploaded_file($_FILES['file']['tmp_name'][$i], $target_path)) { 
// If file moved to uploads folder. 

?> 

<div id="noerror">Image <?php echo $j;?>-->Image Uploaded!</div> 

<?php 

// File was moved, so execute code here 

mysqli_query("INSERT INTO `images` SET `image_name` = '".$_FILES['file']['name'][$i]."'") or die (mysqli_error()); // In this code I would like to add file name to a DB 



} else {  // If File Was Not Moved. 
?> 

<div id="error">Image <?php echo $j;?>--> <b>Please Try Again!</b></div> 

<?php 
} 

} else {  // If File Size And File Type Was Incorrect. 
?> 

<div id="error">Image <?php echo $j;?>--> <b>Check file Size or Type</b></div> 

<?php 
} 
} 
} 
?> 
+0

您的查詢行似乎不工作。我用myPHP測試過它,它給我語法錯誤。 – CharlotteOswald 2015-04-05 10:05:49

+0

你能解釋我的錯誤嗎?你添加了數據庫連接文件嗎? – 2015-04-05 11:53:00

0

你的代碼是相當混亂,我是因爲:

  1. 沒有適當的縮進
  2. 在同一行代碼中的註釋是難以閱讀

這是我想更多信息:

<?php 

if (isset($_POST['submit'])) { 
// Variable for indexing uploaded image. 
$j = 0; 
// Declaring Path for uploaded images. 
$target_path = "uploads/test/"; 

// Loop to get individual element from the array 
for ($i = 0; $i < count($_FILES['file']['name']); $i++) { 

    // Extensions which are allowed. 
    $validextensions = array("jpeg", "jpg", "png");  

    // Explode file name from dot(.) 
    $ext = explode('.', basename($_FILES['file']['name'][$i])); 

    // Store extensions in the variable. 
    $file_extension = end($ext); 
    // Set the target path with a new name of image. 
    $target_path = $target_path . md5(uniqid()) . "." . $ext[count($ext) - 1]; 

    // Increment the number of uploaded images according to the files in array. 
    $j = $j + 1;  

    if (($_FILES["file"]["size"][$i] < 100000) && in_array($file_extension, $validextensions)) { 

     // If file moved to uploads folder. 
     if (move_uploaded_file($_FILES['file']['tmp_name'][$i], $target_path)) { 

      echo '<div id="noerror">Image '.$j.'-->Image Uploaded!</div>'; 

      // File was moved, so execute code here 
      //Connection to DB 

      $mysqli = new mysqli("localhost", "my_user", "my_password", "table_name"); 

      // check connection 
      if ($mysqli->connect_errno) { 
       printf("Connect failed: %s\n", $mysqli->connect_error); 
       exit(); 
      } 

      $mysqli->query("INSERT INTO images_table (img_column_1) VALUES ('".$_FILES['file']['tmp_name'][$i]."')"); 

      //Close DB connection 
      mysqli_close($con); 

     // If File Was Not Moved. 
     } else { 

      echo '<div id="error">Image '.$j.'--> <b>Please Try Again!</b></div>'; 
     } 

    //If File Size And File Type Was Incorrect. 
    } else { 

     echo '<div id="error">Image '.$j.'--> <b>Check file Size or Type</b></div>'; 

    } 
} 
} 
?> 

我通常不建議使用:

echo '<div>'.$var.'</div>'

但是你的代碼很混亂,我覺得這是一個更好的解決方案。

+0

您的查詢行給我錯誤,爲什麼?謝謝 – CharlotteOswald 2015-04-05 09:36:46

+0

什麼樣的錯誤?將它發佈在註釋 – 2015-04-05 09:48:31

+0

語法錯誤 - 帶有$ mysqli->查詢 – CharlotteOswald 2015-04-05 09:53:34