user701510
user701510

Reputation: 5763

How to create a file upload form that creates more form fields based on input using 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>

Upvotes: 1

Views: 353

Answers (1)

user680786
user680786

Reputation:

$_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.

Upvotes: 1

Related Questions