2

I need the user to insert multiple images into database through a form. I succeded to make one image insert. Here is the code:

$insert_sql = "INSERT INTO my_tbl VALUES ('' , \"$image\", \"$title\")";

<form name = "my_form" form action=" " enctype="multipart/form-data" method="post">
<div> Title:  <input name="title" type="text" size="50"   /></div> 
<div"> Image:   <input name="classnotes" type="file"  value="" id="file"/></div>
<input name='submit' type='submit' id="submit-go" value='Upload'>
</form>

Now I need to insert several images so I tried to insert at least 3 images but didn't work out. Only last field's image inserts into the database. Anyhow I need to make it work. So, any help is appreciated.

<form name = "my_form" form action=" " enctype="multipart/form-data" method="post">
<div> Title:  <input name="title" type="text" size="50"   /></div> 
<div"> Image:   <input name="classnotes" type="file"  value="" id="file"/></div>

<div> Title:  <input name="title" type="text" size="50"   /></div> 
<div"> Image:   <input name="classnotes" type="file"  value="" id="file"/></div>

<div> Title:  <input name="title" type="text" size="50"   /></div> 
<div"> Image:   <input name="classnotes" type="file"  value="" id="file"/></div>
<input name='submit' type='submit' id="submit-go" value='Upload'>
</form>

1 Answer 1

3

The problem is that using many inputs with the same name will cause just the last one to be sent. You should name your inputs like arrays and then loop over the array in the PHP code.

HTML

<div> Title:  <input name="title[]" type="text" size="50"   /></div> 
<div> Image:   <input name="classnotes[]" type="file"  value="" id="file"/></div>

PHP

$titles = $_POST["title"];
$images = $_FILES["classnotes"]["tmp_name"];
foreach ($images as $index => $image) {
    $imageType = $_FILES["classnotes"]["type"][$index];

    // check the file type here!!

    $title = $titles[$index];
    $insert_sql = "INSERT INTO my_tbl VALUES ('' , \"$image\", \"$title\")";

    // execute the query here!!

    // move the uploaded file here!!
}

Edit

@Jklyn your script has a lot of problems. I edited the snippet above to give you a better idea of how to refactor your code to achieve what you want. Please mind that you have an array of images to handle, so every query or operation on each single file should happen inside the foreach loop.

Read how the upload of many files works in PHP: http://php.net/manual/en/features.file-upload.multiple.php

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

1 Comment

Thank you for your effort. I'll certainly study the link. I would be more grateful if you would help me further to solve this issue. I badly need to solve it quickly for my website.

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.