我有一个表单,该表单也可以选择上传文件,但是,如果没有文件上传,当前表单将引发错误4。我想使文件上传是可选的,而不是强制性的。 为了实现这一点,我必须对以下代码进行哪些更改?任何帮助,将不胜感激。
<?php
include('db.php');
if (!isset($_FILES['documents']['tmp_name'])) {
echo "";
}else{
$file=$_FILES['documents']['tmp_name'];
$image = $_FILES["documents"] ["name"];
$image_name= addslashes($_FILES['documents']['name']);
$size = $_FILES["documents"] ["size"];
$error = $_FILES["documents"] ["error"];
if ($error > 0){
die("Error uploading file! Code $error.");
}else{
if($size > 10000000) //conditions for the file
{
die("Format is not allowed or file size is too big!");
} else {
move_uploaded_file($_FILES["documents"]["tmp_name"],"upload/" . $_FILES["documents"]["name"]);
$documents=$_FILES["documents"]["name"];
$emp_id= $_POST['emp_id'];
$name= $_POST['name'];
$title= $_POST['title'];
$department= $_POST['department'];
$leavetype= $_POST['leavetype'];
$startdate= $_POST['startdate'];
$enddate= $_POST['enddate'];
$comments= $_POST['comments'];
$status= $_POST['status'];
$user= $_POST['user'];
mysql_query("insert into medical (emp_id, name, title, department, leavetype, startdate, enddate, comments, documents, status, user)
values('$emp_id', '$name','$title', '$department', '$leavetype', '$startdate', '$enddate', '$comments', '$documents', '$status', '$user')") or die(mysql_error());
}
header('Location:index.php');
}
}
?>
这是表格:
<form method="post" action="adds.php" enctype="multipart/form-data">
<table class="table1">
<tr>
<td><label style="color:#3a87ad; font-size:18px;">Employee ID</label></td>
<td width="30"></td>
<td><input type="text" name="emp_id" placeholder="Employee Number" required /></td>
</tr>
<tr>
<td><label style="color:#3a87ad; font-size:18px;">Full name</label></td>
<td width="30"></td>
<td><input type="text" name="name" placeholder="Full Name" required /></td>
</tr>
<tr>
<td><label style="color:#3a87ad; font-size:18px;">Job Title</label></td>
<td width="30"></td>
<td><input type="text" name="title" placeholder="Job Title" /></td>
</tr>
<tr>
<td><label style="color:#3a87ad; font-size:18px;">Department</label></td>
<td width="30"></td>
<td><input type="text" name="department" placeholder="Department" /></td>
</tr><tr>
<td><label style="color:#3a87ad; font-size:18px;">Type of Leave:</label></td>
<td width="30"></td>
<td><select name="leavetype" >
<option value="Medical Leave">Medical Leave</option>
<option value="Personal Leave">Personal Leave</option>
<option value="Recruitment fairs/Interviews">Recruitment fairs/Interviews</option>
<option value="Paternity Leave">Paternity Leave</option>
<option value="Maternity Leave">Maternity Leave</option>
<option value="Compassionate Leave">Compassionate Leave</option>
<option value="Marriage Leave">Marriage Leave</option>
<option value="Bereavement Leave">Bereavement Leave</option>
<option value="Professional Development">Professional Development</option>
</select></td>
</tr><tr>
<td><label style="color:#3a87ad; font-size:18px;">Start Date:</label></td>
<td width="30"></td>
<td><input type="date" id="startdate" name="startdate" placeholder="0000-00-00"/></td>
</tr><tr>
<td><label style="color:#3a87ad; font-size:18px;">End Date:</label></td>
<td width="30"></td>
<td><input type="date" id="enddate" name="enddate" placeholder="0000-00-00"/></td>
</tr><tr>
<td><label style="color:#3a87ad; font-size:18px;">Additional Notes:</label></td>
<td width="30"></td>
<td><input type="text" id="comments" name="comments"/></td>
</tr>
<tr>
<td><label style="color:#3a87ad; font-size:18px;">Supporting Documents</label></td>
<td width="30"></td>
<td><input type="file" name="documents" /></td>
</tr>
<tr>
<td><label style="color:#3a87ad; font-size:18px;">Status</label></td>
<td width="30"></td>
<td><input type="text" id="status" name="status" value="Pending" readonly></td>
</tr>
</tr><tr>
<td><label style="color:#3a87ad; font-size:18px;">Signature:</label></td>
<td width="30"></td>
<td><input type="text" id="user" name="user" value="<?php echo strtoupper(substr($_SERVER["AUTH_USER"], 4));?>" readonly></td>
</tr>
</table>
</div>
<div class="modal-footer">
<button class="btn" data-dismiss="modal" aria-hidden="true">Close</button>
<button type="submit" name="Submit" class="btn btn-primary">Add</button></div></form>
答案 0 :(得分:0)
使用此最小代码行
<?php
include('db.php');
$emp_id= $_POST['emp_id'];
$name= $_POST['name'];
$title= $_POST['title'];
$department= $_POST['department'];
$leavetype= $_POST['leavetype'];
$startdate= $_POST['startdate'];
$enddate= $_POST['enddate'];
$comments= $_POST['comments'];
$status= $_POST['status'];
$user= $_POST['user'];
$document = "";
//if file is empty directly insert in table
if (isset($_FILES['documents']) && is_array($_FILES['documents']) && count($_FILES['documents']) > 0) {
$file=$_FILES['documents']['tmp_name'];
$image = $_FILES["documents"] ["name"];
$image_name= addslashes($_FILES['documents']['name']);
$size = $_FILES["documents"] ["size"];
$error = $_FILES["documents"] ["error"];
if ($error > 0){
die("Error uploading file! Code $error.");
exit;
}else{
if($size > 10000000) //conditions for the file
{
die("Format is not allowed or file size is too big!");
exit;
}else{
move_uploaded_file($_FILES["documents"]["tmp_name"],"upload/" . $_FILES["documents"]["name"]);
$documents=$_FILES["documents"]["name"];
}
}
}
mysql_query("insert into medical (emp_id, name, title, department,leavetype, startdate, enddate, comments,status, user) values('$emp_id', '$name','$title', '$department', '$leavetype', '$startdate', '$enddate', '$comments','$status', '$user')") or die(mysql_error());
header('Location:index.php');
?>