2016-05-30 60 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

相关问题