dsvcqvp139098 2015-08-20 07:18
浏览 17
已采纳

如何将图像插入mysql

I am trying to upload 4 images into my file directory and also I would like to save reference to them in the database. but with my current code below, what it does is that, it inserts another row in the database instead of putting image 1 into img1 and so on.

I have given up. I don't know what is it that I am doing wrong here.

enter image description here

<?php
    if(isset($_POST['go'])) 
        {
        if(isset($_FILES['file_array']))
         {
            $name_array = $_FILES['file_array']['name'];
            $tmp_name_array = $_FILES['file_array']['tmp_name'];
            $type_array = $_FILES['file_array']['type'];
            $size_array = $_FILES['file_array']['size'];
            $error_array = $_FILES['file_array']['error'];
            $currently_item = "";
        for($i = 0; $i < count($tmp_name_array); $i++)
        {
            if(move_uploaded_file($tmp_name_array[$i],'users_posted_data/'.$name_array[$i]))
            {
                    $currently_item = current($_FILES['file_array']['name']);
                    $sql = $conn->query("INSERT INTO posts(img1, img2, img3) Values('{$currently_item}')");
                    echo $name_array[$i] . "uploaded successfully" . '<br>';
            } 
            else 
            {

            }
        }

    }
}
?>


                            <form action="" method="post" enctype="multipart/form-data">
                                <input type="file" name="file_array[]"><br><br>
                                <input type="file" name="file_array[]"><br><br>
                                <input type="file" name="file_array[]"><br><br>
                                <input type="file" name="file_array[]"><br><br> 
                                <input type="submit" name="go" value="Publish">
                        </form>
  • 写回答

2条回答 默认 最新

  • dongmu1390 2015-08-20 07:22
    关注

    Try this:

    In the loop:

     $currently_item[] = current($_FILES['file_array']['name']);
    

    Out of the loop

       $currently_item = current($_FILES['file_array']['name']);
    
        $sql = $conn[![enter image description here][1]][1]->query("INSERT INTO posts(img1, img2, img3) 
       Values('".$currently_item[0]."','".$currently_item[1]."','".$currently_item[2]."')");
    

    Do perform the validation checks such as if value exists or not..

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题
  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站
  • ¥50 成都蓉城足球俱乐部小程序抢票
  • ¥15 yolov7训练自己的数据集
  • ¥15 esp8266与51单片机连接问题(标签-单片机|关键词-串口)(相关搜索:51单片机|单片机|测试代码)
  • ¥15 电力市场出清matlab yalmip kkt 双层优化问题
  • ¥30 ros小车路径规划实现不了,如何解决?(操作系统-ubuntu)