multiple upload not saved to database
var a=0;
function _add_more() {
var txt = "<br><input type=\"file\" name=\"item_file[]\"><br><input type=\"text\" name=\"text"+a+"\">";
document.getElementById("dvFile").innerHTML += txt;
alert(a);
a=a+1;
}
I am using multiple file upload and with multiple titles. I am not able to save te开发者_开发百科xt in database for title. Can you please tell the correct syntax $_POST["text"]
, where should i put a that i am increementing.
$insert=mysql_query("insert into image_upload set title='".$_POST["text"]."', image='".$filen."'") or die(mysql_error());
What John Boker said is true.
But you will need to loop through them to insert them into the database.
An easy way would be to make name=\"text"+a+"\"
, name=\"text[]\"
(like your file upload element),
So then you would have an array of titles.
So in PHP:
foreach($_POST["text"] as $title){
$insert=mysql_query("insert into image_upload set title='".$title."', image='".$filen."'") or die(mysql_error());
}
Also why not use '
s in your JavaScript instead of escaping the "
s?
You could probably just loop through your titles while you are looping through your uploaded files.
$_POST["text"]
is not defined, the names of the fields are $_POST["text0"]
, $_POST["text1"]
, $_POST["text2"]
, ... one for each input you have.
you should add a
to a hidden field then you'll know the number of inputs you have.
You can use the same strategy for your naming your title fields as you did for you file upload fields:
var txt = "<br><input type=\"file\" name=\"item_file[]\"><br><input type=\"text\" name=\"text[]"\">";
Notice I name them all text[]
. Now in PHP I have the variable $_POST['text']
, which is of itself an array containing all the titles.
To access the files as well as the titles in PHP you can do:
for($i=0; $i<length($_FILES['item_file']); $i++) {
print $_FILES['item_file'][$i]['name'] +"<br>";
print $_POST['text'][$i] + "<br>";
}
Also, I am obliged to inform you that performing mysql queries without proper validation of your input is bound to get you hacked. Please consider using:
$insert=mysql_query("insert into image_upload set title='". mysql_real_escape_string($_POST["text"]) ."', image='". mysql_real_escape_string($filen) ."'") or die(mysql_error());
精彩评论