dsdf64562672 2016-05-30 13:03 采纳率: 0%
浏览 26
已采纳

在表格中插入数据和多个图像[PHP] [重复]

This question already has an answer here:

I try many days combine multiple images and simultaneously various data in which all saved in a table in mysql.

For example we have a form in 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>

How can become the association between PHP and 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;

I should be grateful to your help!!

Thanks!

</div>
  • 写回答

1条回答 默认 最新

  • doumu6997 2016-05-30 13:20
    关注

    this is may be use full for you: 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);
    

    insert "$images" variable in your table.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line