2014-06-05 70 views
0

xxx如何修復此腳本,以便它實際上發佈3個獨立的圖片而不是3次相同的圖片。任何幫助將非常感激。我將提供代碼html代碼來包裝相關的代碼。上傳圖片並將文件名插入到MySQL

HTML:

<form method="post" action="insert.php" enctype="multipart/form-data"> 
<input type="text" name="caseName"><br> 
<input type="file" name="upload[]"/> 
<input type="file" name="upload[]"/> 
<input type="file" name="upload[]"/> 
<input type="submit" value="Submit" > 
</form> 

PHP:

if (isset($_FILES['upload'])) { 
    $name_array = $_FILES['upload']['name']; 
    $tmp_name_array = $_FILES['upload']['tmp_name']; 
    for ($i = 0; $i < count($name_array); $i++) { 


      if (move_uploaded_file($tmp_name_array[$i], "uploaded/" . $name_array[$i] )) { 
        echo $name_array[$i]; 
      } else { 
        echo "failed"; 
      } 
    } 
    $dsn = 'mysql:host=localhost;dbname=GLO12408958DB'; 
    $username = 'root'; 
    $password = 'yt987210d'; 
    // 
    // DB connection was made 
    // 
    $pdo = new PDO($dsn, $username, $password); 



    //loop over array to get names. Make sure we have actual content. 
    if (count($name_array) > 0 && $name_array !== false) { 

      //Prepare query 
      $statement = $pdo->prepare('INSERT INTO caseStudies(caseImage,caseImage2,caseImage3) VALUES (?,?,?)'); 

      //use a different index in the event that the numeric keys in the name array are not ordered correctly 
      $index = 1; 


      foreach ($name_array as $key => $filename) { 

        $statement->bindParam($index, $filename, PDO::PARAM_STR); 
        $index++; 
      } 

      $statement->execute(); 

      //etc.... 
    } 
} 
+1

現在您已經將它發佈到互聯網上,最好更改您的數據庫密碼。 – tadman

回答

0

按照manualbindParam()「綁定一個PHP變量在SQL語句對應的命名或問號佔位符,用於製備與PDOStatement :: bindValue(),不同,該變量被綁定爲引用,並且僅在PDOStatement :: execute()被調用時調用

相關問題