我尝试制作功能编辑数据但不起作用。当文件/ img为空时,数据成功插入而没有文件或图像,但是,当文件已经存在然后替换它时,它会失败。
HTML code:
<form method="post" id="insert_form" enctype="multipart/form-data">
<label>Enter Employee Name</label>
<input type="text" name="name" id="name" class="form-control" />
<br />
<label>Enter Employee Address</label>
<textarea name="address" id="address" class="form-control"></textarea>
<br />
<label>Select Gender</label>
<select name="gender" id="gender" class="form-control">
<option value="Male">Male</option>
<option value="Female">Female</option>
</select>
<br />
<label>Enter Designation</label>
<input type="text" name="designation" id="designation" class="form-control" />
<br />
<label>Enter Age</label>
<input type="text" name="age" id="age" class="form-control" />
<br />
<label>File</label>
<br>
<div>
<img src="" id="pict" width="100px" class="img-thumbnail">
</div>
<br>
<input type="file" name="image" id="image">
<span id="errMess"></span>
<br/>
<br>
<input type="hidden" name="employee_id" id="employee_id" />
<input type="submit" name="insert" id="insert" value="Insert" class="btn btn-success" />
</form>
Php代码:
$conn = mysqli_connect('localhost','root','','test');
$name = mysqli_real_escape_string($conn,$_POST['name']);
$address = mysqli_real_escape_string($conn,$_POST['address']);
$gender = mysqli_real_escape_string($conn,$_POST['gender']);
$designation = mysqli_real_escape_string($conn,$_POST['designation']);
$age = mysqli_real_escape_string($conn,$_POST['age']);
$extensi = explode(".", $_FILES['image']['name']);
$img = "ND-".round(microtime(true)).".".end($extensi);
$temp = $_FILES['image']['tmp_name'];
move_uploaded_file($tmp, "assets/img/myfolder/".$img);
$id = $_POST['employee_id'];
if($id != '')
{
if($_FILES['image']['name'] == "")
{
$query = "UPDATE tbl_employee SET name='$name', address='$address',
gender='$gender', designation='$designation', age='$age' WHERE id = '$id' ";
$message = "data Update";
}
else
{
$sqlQ = "SELECT * FROM tbl_employee WHERE id = '$id'";
unlink("assets/img/myfolder/".**??????**); // confuse about this
move_uploaded_file($temp, "assets/img/myfolder/".$img);
$query = "UPDATE tbl_employee SET name='$name', address='$address',gender='$gender', designation='$designation', age='$age',image='$img' WHERE id = '$id' ";
$message = "data Update";
}
}
$sql = $conn->query($query);
答案 0 :(得分:0)
问题解决了,我尝试在unlink前添加** @ **并且它可以工作:
@unlink($folder);
move_uploaded_file($sumber, "assets/img/product/".$img);
$query = "UPDATE tbl_employee SET name='$name', address='$address',
gender='$gender', designation='$designation', age='$age', image='$img' WHERE id = '$id' ";