2016-05-30 38 views
0

我嘗試了很多天結合了多個圖像,同時各種數據都保存在一個表中的mysql。在表中插入數據和多個圖像[PHP]

例如我們在HTML表單,PHP:

<form method="post" enctype="multipart/form-data"> 
    <input type="text" name="firstname"> 
    <input type="text" name="lastname"> 
    <input type="text" name="phone"> 
    <input type="file" name="images[]" multiple="multiple" accept="image/*" /> 
    <input type="submit" name="submit" value="Upload!" /> 
</form> 

怎樣才能成爲PHP和SQL之間的關聯?

<?php 
include "config.php"; 
$erors = array(); // set an empty array that will contains the errors 

// Check for form submission 
if (isset($_POST['firtname']) && isset($_POST['lastname'])) { 
    // chech if all form fields are filled in correctly 
    // (email address and the minimum number of characters in "name" and "pass") 
    if (strlen($_POST['firstname'])<3) $erors[] = 'Name must contain minimum 3 characters'; 
    if (strlen($_POST['lastname'])<6) $erors[] = 'Password must contain minimum 6 characters'; 

    // if no errors ($error array empty) 
    if(count($erors)<1) { 

    // store the values in an Array, escaping special characters for use in the SQL statement 
    $adds['firstname'] = $mysqli->real_escape_string($_POST['firtname']); 
    $adds['lastname'] = $mysqli->real_escape_string($_POST['lastname']); 
    $adds['phone'] = $mysqli->real_escape_string($_POST['phone']); 

    /* 

    CODE FOR UPLOAD MULTIPLE IMAGES 

    */ 

    // sql query for INSERT INTO users 
    $sql = "INSERT INTO `insert_data` (`firstname`, `lastname`, `phone`) VALUES ('". $adds['firtname']. "', '". $adds['lastname']. "', '". $adds['phone']. "')"; 

    // Performs the $sql query on the server to insert the values 
    if ($mysqli->query($sql) === TRUE) { 
     echo 'users entry saved successfully'; 
    } 
    else { 
     echo 'Error: '. $mysqli->error; 
    } 

    $mysqli->close(); 
    } 
    else { 
    // else, if errors, it adds them in string format and print it 
    echo implode('<br>', $erors); 
    } 
} 
?> 

CREATE TABLE IF NOT EXISTS `insert_data` (
    `id` int(9) NOT NULL AUTO_INCREMENT, 
    `firstname` varchar(100) COLLATE utf8_unicode_ci NOT NULL, 
    `lastname` varchar(100) COLLATE utf8_unicode_ci NOT NULL, 
    `phone` varchar(100) COLLATE utf8_unicode_ci NOT NULL, 
    /* 

    HERE SAVED MULTIPLE IMAGES?? 

    */ 
    PRIMARY KEY (`id`) 
) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci; 

我應該感謝您的幫助!

謝謝!

+0

您應該通過在一列中提供逗號分隔圖像的名稱並將其存儲在表格中來實現。 – RJParikh

+0

這是不是很清楚你問 - 你想要將圖像本身存儲在數據庫中,或將圖像存儲在一個目錄中,並且只有他們的路徑保存到數據庫?在大多數情況下,選擇第二個選項,但有些更喜歡直接在數據庫中將圖像另存爲base64字符串。這一切都取決於你打算如何處理它。無論如何,你可能需要一個一對多的關係來存儲圖像/路徑。 – Shovalt

+0

@RuchishParikh正確地說,你可以說我可以完成任務嗎?想想像一個註冊表格Realestate! – VictoryCode

回答

0

這是可使用全給你:Multiple file upload in php

$total = count($_FILES['upload']['name']); 

    // Loop through each file 
    for($i=0; $i<$total; $i++) { 
     //Get the temp file path 
     $tmpFilePath = $_FILES['upload']['tmp_name'][$i]; 

     //Make sure we have a filepath 
     if ($tmpFilePath != ""){ 
     //Setup our new file path 
     $filename[] = $_FILES['upload']['name'][$i]; 
     $newFilePath = "./uploadFiles/" . $_FILES['upload']['name'][$i]; 

     //Upload the file into the temp dir 
     if(move_uploaded_file($tmpFilePath, $newFilePath)) { 

      //Handle other code here or insert query here 

     } 
     } 
    } 
$images = implode(",",$filename); 

插入 「$圖像」 在您的表變量。

+0

其中是插入代碼?問題是:在表中插入數據和多個圖像 – RJParikh

相關問題