【问题标题】:insert data and multiple images in a table [PHP] [duplicate]在表中插入数据和多个图像[PHP] [重复]
【发布时间】:2016-05-31 18:05:32
【问题描述】:

我尝试了很多天将多个图像和同时保存在 mysql 中的表中的各种数据。

例如,我们有一个 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;

我应该感谢你的帮助!!

谢谢!

【问题讨论】:

  • 你应该在一列中给出以逗号分隔的图像名称并将其存储在表格中。
  • 不太清楚你在问什么 - 你想将图像本身存储在数据库中,还是将图像存储在一个目录中,只有它们的路径保存到数据库?在大多数情况下,选择了第二个选项,但有些人更喜欢将图像作为 base64 字符串直接保存在数据库中。这完全取决于您以后打算用它做什么。在任何情况下,您都可能需要与将存储图像/路径的表建立一对多关系。
  • @RuchishParikh 你说我能做到吗?想想像房地产登记表之类的东西!
  • 您可以通过implode() 逗号分隔字符串的图像数组来实现并保存。
  • @RuchishParikh 有什么例子吗?谢谢!!

标签: php html mysql image


【解决方案1】:

这可能对你有用: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);

在表中插入“$images”变量。

【讨论】:

  • 插入代码在哪里?问题是:在一个表中插入数据和多张图片
猜你喜欢
  • 2020-05-16
  • 2021-03-25
  • 1970-01-01
  • 2020-02-24
  • 1970-01-01
  • 1970-01-01
  • 2019-07-13
  • 2019-05-28
  • 1970-01-01
相关资源
最近更新 更多