【问题标题】:Storing multiple images in MySQL database with PHP使用 PHP 在 MySQL 数据库中存储多个图像
【发布时间】:2017-07-22 18:54:06
【问题描述】:

我是 PHP 新手。

我正在尝试将多个带有文本的图像添加到数据库中,但我被卡住了。如果我选择一个,它将被存储。如果我选择多个,它将​​在数据库中保留为空白。

我需要帮助来解决这个问题。另外,我很困惑:数据库中的一张图片可以吗?我的意思是,是否有一种特殊的数据类型或方法可以在数据库中的同一条目上拥有多个图像?

这是我的代码:

<html>
<body>
    <form class="container" enctype ='multipart/form-data' action="add.php" method="post">
        <label><b> Name: </b></label><br><br>
        <input type="text" name="name"><br><br>
        <label><b> Type: </b></label><br><br>
        <input type="text" name="type"><br><br>
        <label><b> Detail: </b></label><br><br>
        <input type="text" name="detail"><br><br>
        <label><b> Area: </b></label><br><br>
        <input type="text" name="area"><br><br>

        Select image to upload:
        <input type="file" name="filename" size='10000' multiple>
        <input type="submit" value="Upload Image">
    </form>
    <?php
        // error_reporting(E_ALL); ini_set('display_errors', 1);

        require_once 'login.php';

        $db_server = mysqli_connect($db_hostname, $db_username, $db_password, $db_database);
        if (!$db_server) die("unable to connect to mysqli:" . mysqli_error());

        mysqli_select_db($db_server, "dbase1") or die("db not selected" . mysqli_error());

        $name = $_POST["name"];
        $type = $_POST["type"];
        $detail = $_POST["detail"];
        $area = $_POST["area"];

        if($_FILES)
        {
            $namee = $_FILES['filename']['name'];
            move_uploaded_file($_FILES['filename']['tmp_name'], $namee);
            echo "uploaded image'$namee'<br><img src ='$namee'>";
        }

        $source= "pictures/".$namee;

        $sql = "INSERT INTO adprop (name, type, text,area,filename) VALUES ('$name','$type','$detail','$area','$source')";
        $db = $db_server->query($sql);
    ?>
</body>
<html>

提前感谢您的帮助!

【问题讨论】:

  • 您可以保存多个图像路径,并通过逗号在表格字段中写入它们。并在用户界面中拆分它们。
  • 你能把它显示在我冻结的代码上吗?谢谢你的回复

标签: php mysql database image mysqli


【解决方案1】:

例如,如果您想在一个字段中保存多个图像路径,您可以将它们添加到 db 中,如下所示:

/image_folder/image_name1.jpg,/image_folder/image_name2.jpg

并使用split 和for loop 在用户界面中动态打印它们。

【讨论】:

  • 您能帮我编辑这段代码吗?
猜你喜欢
  • 1970-01-01
  • 2015-10-26
  • 2015-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-11-26
相关资源
最近更新 更多