1

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>
3
  • 2
    Ugh, spaghetti code again. People really gotta use MVC.. Commented Jun 5, 2011 at 4:49
  • Agreed, you really might want to rethink your approach. Commented Jun 5, 2011 at 4:52
  • Why are you doing this in php and not javascript? Commented Jun 5, 2011 at 6:52

1 Answer 1

1

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

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for the help. I'm basically trying to cram PHP as fast as possible using youtube tutorials, googling, W3schools, and this site so what appears to be a silly error isn't as obvious to me.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.