-2

I try to make function edit data but it doesn't work. When a file/img is empty then data successfully inserted without file or image and but, when a file is already exist then replace it, it fails.

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 code:

$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);
Scath
  • 3,777
  • 10
  • 29
  • 40
Tomz
  • 139
  • 2
  • 3
  • 9
  • 1
    Whats the error you're getting? – Kasia Gogolek May 01 '18 at 12:29
  • I cannot unlink file : unlink("assets/img/myfolder/".**??????**); Warning: unlink(assets/img/barang/): Permission denied – Tomz May 01 '18 at 13:08
  • Possible duplicate of [permission denied - php unlink](https://stackoverflow.com/questions/13594898/permission-denied-php-unlink) – Kasia Gogolek May 01 '18 at 13:53
  • permission denied is nothing much to do with your PHP code, then. The account your script is running under simply doesn't have the required permission on the file or folder - you need to grant it. – ADyson May 01 '18 at 19:35

1 Answers1

0

The problem solved, I try to add ** @ ** in front of unlink and it works:

@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' ";
Tomz
  • 139
  • 2
  • 3
  • 9