dongzhuo3202 2011-06-04 20: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-04 23: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.

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

报告相同问题?

手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部