检查是否在php中的字段数组中选择了文件
我必须更新由多个文件输入类型字段组成的数据库。 所有输入字段的名称都相同检查是否在php中的字段数组中选择了文件,php,multiple-file-upload,Php,Multiple File Upload,我必须更新由多个文件输入类型字段组成的数据库。 所有输入字段的名称都相同 <input type="text" name="title[]"> <input type="file" name="image[]"> <input type="text" name="title[]"> <input type="file" name="image[]"> <input type="text" name="title[]"> <inpu
<input type="text" name="title[]">
<input type="file" name="image[]">
<input type="text" name="title[]">
<input type="file" name="image[]">
<input type="text" name="title[]">
<input type="file" name="image[]">
第二个输入字段中的选定图像首先,如果($upload=1),则需要将代码中的
=
更改为=
。因为=
是赋值
运算符而不是比较
运算符
检查一下,也许你的问题已经解决了。否则,请检查以下可能的解决方案
1.
<?php
$title = $_POST['title'];
for($i=0; $i<sizeof($title); $i++)
{
if(!empty($_FILES['image']['name'][$i]))
{
// check if file uploaded then run below query
$qry = "UPDATE <table_name> SET title='$title[$i]', image='$image' WHERE <match found>"; // you can apply if else here based on move_uploaded_file output
} else {
// set flag upload=0
$qry = "UPDATE <table_name> SET title='$title[$i]' WHERE <match found>";
}
}
?>
<?php
$title = $_POST['title'];
$upload = 0; // define here
for($i=0; $i<sizeof($title); $i++)
{
if(!empty($_FILES['image']['name'][$i]))
{
// upload file and set flag $upload = 1
} else {
// set flag $upload = 0
}
if($upload == 1) // you need to change = (assignment) to == (comparision)
{
$qry = "UPDATE <table_name> SET title='$title[$i]', image='$image' WHERE <match found>";
// execute $qry
} else {
$qry = "UPDATE <table_name> SET title='$title[$i]' WHERE <match found>";
// execute $qry
}
}
?>
我已经更正了有问题的代码(那是打字错误)。实际上,我试着在if条件下打印echo在if(!empty($_FILES['image'][$i]){echo'in if condition…';}
中的code>if(!empty($_FILES['image'][$i]){echo'in if condition…}
但它不打印任何内容..首先尝试检查echo';打印(美元文件)代码>在任何条件之前。并检查显示的内容?我已经用print\r($\u文件)的结果更新了问题
。您还必须在那里添加['name']
。请检查我编辑的答案
<?php
$title = $_POST['title'];
for($i=0; $i<sizeof($title); $i++)
{
if(!empty($_FILES['image']['name'][$i]))
{
// check if file uploaded then run below query
$qry = "UPDATE <table_name> SET title='$title[$i]', image='$image' WHERE <match found>"; // you can apply if else here based on move_uploaded_file output
} else {
// set flag upload=0
$qry = "UPDATE <table_name> SET title='$title[$i]' WHERE <match found>";
}
}
?>
<?php
$title = $_POST['title'];
$upload = 0; // define here
for($i=0; $i<sizeof($title); $i++)
{
if(!empty($_FILES['image']['name'][$i]))
{
// upload file and set flag $upload = 1
} else {
// set flag $upload = 0
}
if($upload == 1) // you need to change = (assignment) to == (comparision)
{
$qry = "UPDATE <table_name> SET title='$title[$i]', image='$image' WHERE <match found>";
// execute $qry
} else {
$qry = "UPDATE <table_name> SET title='$title[$i]' WHERE <match found>";
// execute $qry
}
}
?>