2015-04-05 149 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

相关问题