我有一个有图像输入文件的表。当我使用"添加行"添加另一行时按钮。这意味着另一行也有一个图像输入文件。这意味着如果添加新行,每一行都有一个图像输入文件。所以我的问题是当我提交表格时,图像文件name.withExtension被保存到数据库中,但当我检查我设置存储图像的文件夹时,文件夹中找不到图像。我只能看到每行的图像名称都被添加到数据库中。以下是我的代码
<?php
error_reporting(-1);
if(isset($_POST['travel'])){
$merchant = $_POST["merchant"];
$remark = $_POST["remark"];
for ($i = 0; $i < count($_POST["merchant"]); $i++){
$merchant = $_POST["merchant"][$i];
$remark = $_POST["remark"][$i];
$file = rand(1000,100000)."-".$_FILES['bill_image']['name'][$i];
$file_loc = $_FILES['bill_image']['tmp_name'][$i];
$file_basename = substr($file, 0, strripos($file, '.'));
move_uploaded_file($file_loc,"../uploads/exp/".$file);
$save_new_record = "INSERT INTO `report`(`merchant`, `remark`, `bill_image`, `bill_image_type`) VALUES (?, ?, ?, ?)";
$stmt5 = $mysqli->prepare($save_new_record);
$stmt5->bind_param('ssiisssssbsss', $merchant, $remark, $file, $file);
if ($stmt5->execute() == false){
echo 'query failed: ' . $mysqli->error;
}
$stmt5->close();
}
echo"<meta http-equiv=\"refresh\"content=\"0;URL=index.php\">";
}
//Include Global page
include ('includes/global.php');
?>
<SCRIPT language="javascript">
function addRow(tableID) {
var table = document.getElementById(tableID);
var rowCount = table.rows.length;
var row = table.insertRow(rowCount);
var colCount = table.rows[0].cells.length;
for(var i=0; i<colCount; i++) {
var newcell = row.insertCell(i);
newcell.innerHTML = table.rows[0].cells[i].innerHTML;
//alert(newcell.childNodes);
switch(newcell.childNodes[0].type) {
case "text":
newcell.childNodes[0].value = "";
break;
case "text":
newcell.childNodes[0].value = "";
break;
case "text":
newcell.childNodes[0].value = "";
break;
case "file":
newcell.childNodes[0].value = "";
break;
}
}
}
function deleteRow(tableID) {
try {
var table = document.getElementById(tableID);
var rowCount = table.rows.length;
for(var i=0; i<rowCount; i++) {
var row = table.rows[i];
var chkbox = row.cells[0].childNodes[0];
if(null != chkbox && true == chkbox.checked) {
if(rowCount <= 1) {
alert("Cannot delete all the rows.");
break;
}
table.deleteRow(i);
rowCount--;
i--;
}
}
}catch(e) {
alert(e);
}
}
</SCRIPT>
<div class="wrapper">
<div class="row">
<div class="col-mg-8">
<ul class="nav nav-tabs tabs">
<li class="active tab">
<a href="#travel" data-toggle="tab" aria-expanded="false">
<span class="visible-xs"><i class="fa fa-home"></i></span>
<span class="hidden-xs">Travel</span>
</a>
</li>
</ul>
<div class="tab-content">
<div class="tab-pane active in" id="travel">
<div class="panel panel-info">
<div class="panel-body">
<form action="" method="post" role="form" id="dist" enctype="multipart/form-data">
<table class="table table-hover" cellspacing="0" width="100%">
<thead>
<tr>
<th><input name="checkAll" id="checkAll" type="checkbox" /></th>
<th class="text-left"><?php echo $Merchant; ?></th>
<th class="text-left"><?php echo $Remark; ?></th>
<th class="text-left"><?php echo $Image; ?></th>
</tr>
</thead>
<tbody id="dataTable">
<tr>
<td><input type="checkbox" name="chk[]" class="checkbox1"></td>
<td><input class="form-control" placeholder="<?php echo $Merchant ;?>" id="merchant" name="merchant[]" type="text" /></td>
<td><input class="form-control" placeholder="<?php echo $Remark ;?>" id="remark" name="remark[]" type="text" /></td>
<td><input type="file" name="bill_image[]" /></td>
</tr>
</tbody>
</table>
<input type="button" class="btn-sm btn-primary" value="Add Row" onclick="addRow('dataTable')" />
<input type="button" class="btn-sm btn-danger" value="Delete Row" onclick="deleteRow('dataTable')" />
<div class="panel-footer">
<button type="submit" name="travel" class="btn btn-primary btn-block"><span class="glyphicon glyphicon-log-in"></span> Save</button>
</div>
</form>
</div>
</div>
</div>
</div>
</div>
</div>
</div>