如何在一行中将多个图像插入数据库

时间:2018-02-23 05:28:16

标签: php html mysqli

我编写了这个html表单

<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="POST" enctype="multipart/form-data">
  <input type="file" name="image1" /><br/>
  <input type="file" name="image2" /><br/>
  <input type="submit" name='submit' value="upload" />
</form>

这是我的PHP代码

<?php
 include "conf/connect.php";

            if (isset($_POST['submit'])){

                $uploadpath1 = 'upload/';

                $image1_name = $_FILES['image1']['name'];
                $image1_size = $_FILES['image1']['size'];
                $image1_type = $_FILES['image1']['type'];
                $image1_url =
                $image1_temp_name = $_FILES['image1']['tmp_name'];

                $uploadpath1 = $uploadpath1. time() . basename($image1_name);
                $image1_url = 'http://'.$_SERVER['HTTP_HOST'].rtrim(dirname($_SERVER['REQUEST_URI']), '\\/').'/'.$uploadpath1;
                ////

                if(empty($errors)) {
                move_uploaded_file($image1_temp_name, $uploadpath1);
                $success[] = 'Uploaded!';
                }
            }
            ///
            if (isset($_POST['submit'])){

                $uploadpath2 = 'upload/';

                $image2_name = $_FILES['image2']['name'];
                $image2_size = $_FILES['image2']['size'];
                $image2_type = $_FILES['image2']['type'];
                $image2_temp_name = $_FILES['image2']['tmp_name'];

                $uploadpath2 = $uploadpath2. time() . basename($image2_name);
                $image2_url = 'http://'.$_SERVER['HTTP_HOST'].rtrim(dirname($_SERVER['REQUEST_URI']), '\\/').'/'.$uploadpath2;
                ////

                if(empty($errors)) {
                move_uploaded_file($image2_temp_name, $uploadpath2);
                $success[]= 'Uploaded';
                }
            }

            if(isset($_POST['submit'])){

            $id = $_GET['id'];
            $table = 'products';

            mysqli_query($connect, "UPDATE `$table` SET `image1` = $uploadpath1, `image2` = $uploadpath2 WHERE `id` = $id");

            }
  ?>

全押:image_multi.php 因此,当我发布提交..图像上传成功但我的表中没有更新

my table

我运行此链接:mydomainname.com/image_multi.php?id=1

图片已上传但未在数据库中显示

由于

2 个答案:

答案 0 :(得分:0)

检查表格中“image1”和“image2”字段的类型和长度

答案 1 :(得分:0)

问题肯定在mysql语句中,你没有引用文件名,这就是为什么更新没有运行的原因。将来只需检查db日志中的错误。 解决这个问题应该有效

mysqli_query($connect, "UPDATE `$table` SET `image1` = '$uploadpath1', `image2` = '$uploadpath2' WHERE `id` = $id");

注意'封闭你的$ vars,这只是当你需要行中的字符串时 - 你显然需要它。我不确定Id应该是字符串,检查它是否是数字。