输入文件的表单编辑

Form edit for input file

我正在制作编辑数据表格。然后在编辑表单中,有3种输入文件类型(图像)。所以问题是当我更新其中一种输入文件类型的 1 个文件时,另一种输入文件类型也会更新并取消链接数据库中以前的数据,因此该列为空。

所以我想制作这个编辑表单,当我更新1个文件时,另一个文件没有改变。只有正在更新的一个应该改变,另一个应该保持不变。

非常感谢所有的帮助和回答。谢谢 这是 edithotel.php

的代码
<?php
    include "koneksi.php";

                $id=$_POST['idhotel'];
                $namahotel=$_POST['namahotel'] ;
                $alamat=$_POST['alamat'] ;
                $notelp=$_POST['notelp'] ;
                $gambar1=$_FILES['gambar1']['tmp_name'];
                $gambar2=$_FILES['gambar2']['tmp_name'];
                $gambar3=$_FILES['gambar3']['tmp_name'];
                $nama_file1 = $_FILES['gambar1']['name'];
                $nama_file2 = $_FILES['gambar2']['name'];
                $nama_file3 = $_FILES['gambar3']['name'];
                $tempat_gambar1= 'file/hotel/gambar1/'.$nama_file1;
                $tempat_gambar2= 'file/hotel/gambar2/'.$nama_file2;
                $tempat_gambar3= 'file/hotel/gambar3/'.$nama_file3;
                $latitude=$_POST['latitude'] ;
                $longitude=$_POST['longitude'] ;
                $qr=mysql_query("select * FROM tbhotel WHERE idhotel='$id'");
                $r=mysql_fetch_array($qr);
                $tempat_foto1 = 'file/hotel/gambar1/'.$r['gambar1'];
                $tempat_foto2 = 'file/hotel/gambar2/'.$r['gambar2'];
                $tempat_foto3 = 'file/hotel/gambar3/'.$r['gambar3'];

                //$ccc = mysql_query("SELECT * FROM tbhotel WHERE idhotel='$id' ");
                //$z = mysql_fetch_array($ccc);

                //upload gambar

                if (isset($_POST['simpan'])){
                    if($gambar1 != 'kosong'){
                            $d1 = 'file/hotel/gambar1/'.$r['gambar1'];
                            unlink ("$d1");
                            move_uploaded_file ($_FILES['gambar1']['tmp_name'], "file/hotel/gambar1/".$nama_file1);
                            //echo '<script>alert("DATA BBB DIUBAH");location="hotel.php";</script>';
                            $sql = mysql_query("UPDATE tbhotel SET namahotel ='$namahotel', alamat = '$alamat', notelp = '$notelp' ,  gambar1='$nama_file1', latitude ='$latitude' , longitude ='$longitude'  where idhotel='$id'") or die (mysql_error());
                    }

                     if($gambar2 != 'kosong'){
                            $d2 = 'file/hotel/gambar2/'.$r['gambar2'];
                            unlink ("$d2");
                            move_uploaded_file ($_FILES['gambar2']['tmp_name'], "file/hotel/gambar2/".$nama_file2);

                            $sql2 = mysql_query("UPDATE tbhotel SET namahotel ='$namahotel', alamat = '$alamat', notelp = '$notelp' ,  gambar2='$nama_file2', latitude ='$latitude' , longitude ='$longitude'  where idhotel='$id'") or die (mysql_error());
                    }


                     if($gambar3 != 'kosong'){
                            $d3 = 'file/hotel/gambar3/'.$r['gambar3'];
                            unlink ("$d3");
                            move_uploaded_file ($_FILES['gambar3']['tmp_name'], "file/hotel/gambar3/".$nama_file3);

                            $sql3 = mysql_query("UPDATE tbhotel SET namahotel ='$namahotel', alamat = '$alamat', notelp = '$notelp' ,  gambar3='$nama_file3', latitude ='$latitude' , longitude ='$longitude'  where idhotel='$id'") or die (mysql_error());

                    }


                            if ($sql) {
                                //jika  berhasil tampil ini
                                echo '<script>alert("DATA BERHASIL DIUBAH");location="hotel.php";</script>';
                            } else {
                                // jika gagal tampil ini
                                echo '<script>alert("DATA GAGAL DIUBAH");location="formedithotel.php";</script>';
                            }
                }


?>

这里是 editform.html

的代码
<form name="edithotel" enctype="multipart/form-data" action="edithotel.php" method="post" enctype="multipart/form-data" id="demo-form2" data-parsley-validate class="form-horizontal form-label-left">
  <div class="form-group">
    <label class="control-label col-md-12 col-sm-12 col-xs-12" for="last-name">Nama Hotel</label>
    <div class="col-lg-12">
      <input type="text" id="namahotel" name="namahotel" value="<?php echo $result['namahotel']; ?>">
    </div>
  </div>

  <div class="form-group">
    <label class="control-label col-md-12 col-sm-12 col-xs-12" for="last-name">Alamat</label>
    <div class="col-lg-12">
      <textarea class="form-control" rows="5" id="alamat" name="alamat" ><?php echo $result['alamat']; ?></textarea>
    </div>
  </div>

  <div class="form-group">
    <label class="control-label col-md-12 col-sm-12 col-xs-12" for="last-name">No Telp</label>
    <div class="col-lg-12">
      <input type="text" id="notelp" name="notelp" value="<?php echo $result['notelp']; ?>">
    </div>
  </div>

  <div class="form-group">
    <label class="control-label col-md-12 col-sm-12 col-xs-12">Gambar 1</label><br>
    <div class="col-md-6 col-sm-6 col-xs-12">
      <img src="file/hotel/gambar1/<?php echo $result['gambar1']; ?>" width='80' height='60'/>
      <input type="file" id="gambar1" name="gambar1" value="kosong">
    </div>
  </div><br>

  <div class="form-group">
    <label class="control-label col-md-12 col-sm-12 col-xs-12">Gambar 2</label><br>
    <div class="col-md-6 col-sm-6 col-xs-12">
      <img src="file/hotel/gambar2/<?php echo $result['gambar2']; ?>" width='80' height='60'/>
      <input type="file" id="gambar2" name="gambar2" value="kosong">
    </div>
  </div><br>

  <div class="form-group">
    <label class="control-label col-md-12 col-sm-12 col-xs-12">Gambar 3</label><br>
    <div class="col-md-6 col-sm-6 col-xs-12">
      <img src="file/hotel/gambar3/<?php echo $result['gambar3']; ?>" width='80' height='60'/>
      <input type="file" id="gambar3" name="gambar3" value="kosong">
    </div>
  </div><br>

  <div class="form-group">
    <label class="control-label col-md-12 col-sm-12 col-xs-12" for="last-name">Latitude</label>
    <div class="col-lg-12">
      <input type="text" id="latitude" name="latitude" value="<?php echo $result['latitude']; ?>">
    </div>
  </div>
  <div class="form-group">
    <label class="control-label col-md-12 col-sm-12 col-xs-12" for="last-name">Longitude</label>
    <div class="col-lg-12">
      <input type="text" id="longitude" name="longitude" value="<?php echo $result['longitude']; ?>">
    </div>
  </div>

  <div class="ln_solid"></div>
  <div class="form-group">
    <div class="col-md-6 col-sm-6 col-xs-12 col-md-offset-3">
      <button type="submit" name="simpan" class="btn btn-success">Update</button>
    </div>
  </div>
</form>

你需要检查 $nama_file1 和其他空的。

if($gambar1 != 'kosong' && !empty($nama_file1)){
...
}

在每个图像字段之后添加第一个代码,显然您需要为每一行更改为隐藏字段的名称。

然后在您的查询中您需要检查

<input type="file" id="gambar1" name="gambar1" value="kosong">
<input type="hidden" name="gambar1_current" value="<?php echo $result['gambar1']; ?>">

if (isset($_POST['gambar1']) !="") {
    $image = $_POST['gambar1'];
}else{
$image = $_POST['gambar1_current'];
}

并对所有 3 个图像执行此操作并将 $image 变量插入数据库。