dongzhuo3202 2011-06-05 04:45
浏览 48
已采纳

如何使用php创建基于输入创建更多表单字段的文件上载表单?

I have have ten columns in my SQL table: id, imgid, urlid, image1, image2, image3, image4, image5, and comment. Id, imgid, and urlid are int type. Image[1-5] are mediumblob type. Url and comment are text type. Imgid is the number of images uploaded (max should be 5), urlid is the number of urls submitted (which should be one right now), url holds the url, and comment holds user comments.

The form starts by asking the user how many images he wants to upload (max number is 5 in my script). If the user picks, for example, 3, then 3 file upload fields should be created with a new submit button too. A variable called $imgid will store the number 3 which is the number of files the user wants to upload. The user then picks 3 images and clicks the newly created submit button to submit the three images into the first three image columns in the SQL table. My problem is that when I click the second submit button which appears after you click the first one, I get this error:

Warning: file_get_contents() [function.file-get-contents]: Filename cannot be empty in dbpform2.php on line 75

Here is what I have made so far:

<html>
<body>

<form action="dbpform2.php" method="POST">
How many images do you want to upload?<br>
<input type="text" name="imgid"  /><br />
<input type="submit" name="submit" value="submit" />
</form>

<?php 
mysql_connect ("","","") or die(mysql_error());
mysql_select_db ("") or die(mysql_error());


$imgid = $_POST['imgid'];

if (isset($_POST['submit']))
{
    echo"<form action='dbpform2.php' method='POST' enctype='multipart/form-data'>
";

    if ($imgid >= 5)
    {
        for ($i=1; $i<= 5; $i++)
        {

        ${'img' . $i} = "img".$i;
        echo "<input type='file' name='${'img' . $i}'  /> <br />";

        }

        echo"
        <input type='hidden' name='imgid' value='$imgid' />
        <input type='submit' name='submit2' value='submit2' />
        </form>";
    }

    if ($imgid <= 5)
    {
        for ($i=1; $i<=$imgid; $i++)
        {

        ${'img' . $i} = "img".$i;
        echo "<input type='file' name='${'img' . $i}'  /> <br />";

        }
        echo"

        <input type='hidden' name='imgid' value='$imgid' />
        <input type='submit' name='submit2' value='submit2' />
        </form>";
    }
}

?>

<?php

$imgid = $_POST['imgid'];

for ($i=1; $i <= $imgid; $i++)
{

${'img' . $i} = "img".$i;
${'file' . $i}  = $_FILES['${'.img.' . $i}']['tmp_name'];

}

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

        for ($i=1; $i<=$imgid; $i++)
        {
            ${'img' . $i} = "img".$i;
            ${'file' . $i} = addslashes(file_get_contents ($_FILES['${'.img.' . $i}']['tmp_name']));

        }

        mysql_query ("INSERT INTO dbp VALUES ('','$imgid','$urlid','$url', '$file1', '$file2','$file3','$file4','$file5','$comment')"); 

    }
?>



</body>
</html>
  • 写回答

1条回答 默认 最新

  • dongsi4815 2011-06-05 07:45
    关注

    $_FILES['${'.img.' . $i}']
    Replace to
    $_FILES[${'img' . $i}]

    Use IDE to edit scripts, to avoid such silly errors. Try NetBeans, PhpStorm.
    And try to read about MVC.

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

报告相同问题?

悬赏问题

  • ¥15 怎样才能让鼠标沿着线条的中心线轨迹移动
  • ¥60 用visual studio编写程序,利用间接平差求解水准网
  • ¥15 Llama如何调用shell或者Python
  • ¥20 谁能帮我挨个解读这个php语言编的代码什么意思?
  • ¥15 win10权限管理,限制普通用户使用删除功能
  • ¥15 minnio内存占用过大,内存没被回收(Windows环境)
  • ¥65 抖音咸鱼付款链接转码支付宝
  • ¥15 ubuntu22.04上安装ursim-3.15.8.106339遇到的问题
  • ¥15 blast算法(相关搜索:数据库)
  • ¥15 请问有人会紧聚焦相关的matlab知识嘛?