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;
}我使用多个文件上传和多个标题。我无法将标题的文本保存在数据库中。你能告诉我正确的语法$_POST["text"]吗,我应该把我正在递增的a放在哪里?
$insert=mysql_query("insert into image_upload set title='".$_POST["text"]."', image='".$filen."'") or die(mysql_error());发布于 2011-06-15 23:02:38
约翰·博克说的都是真的。
但是您需要遍历它们以将它们插入到数据库中。
一种简单的方法是将name=\"text"+a+"\"、name=\"text[]\" (类似于您的文件上传元素)
这样你就会有一个标题数组。
所以在PHP中:
foreach($_POST["text"] as $title){
$insert=mysql_query("insert into image_upload set title='".$title."', image='".$filen."'") or die(mysql_error());
}
另外,为什么不在JavaScript中使用'%s,而不是转义"%s?
你可以在循环你上传的文件的同时循环你的标题。
发布于 2011-06-15 22:52:05
未定义$_POST["text"],字段名称为$_POST["text0"]、$_POST["text1"]、$_POST["text2"]、...每个输入对应一个。
你应该将a添加到一个隐藏字段中,这样你就可以知道你有多少个输入值了。
发布于 2011-06-15 23:03:56
您可以使用与文件上传域相同的策略来命名标题域:
var txt = "<br><input type=\"file\" name=\"item_file[]\"><br><input type=\"text\" name=\"text[]"\">";请注意,我将它们都命名为text[]。现在在PHP中我有了变量$_POST['text'],它本身就是一个包含所有标题的数组。
要访问PHP中的文件和标题,您可以执行以下操作:
for($i=0; $i<length($_FILES['item_file']); $i++) {
print $_FILES['item_file'][$i]['name'] +"<br>";
print $_POST['text'][$i] + "<br>";
}另外,我有义务告诉你,在没有对你的输入进行适当验证的情况下执行mysql查询肯定会让你被黑客攻击。请考虑使用:
$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());https://stackoverflow.com/questions/6359634
复制相似问题