输入文件的表格编辑

时间:2018-09-27 04:09:02

标签: php mysql web crud edit

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

因此,我想制作此编辑表单,当我更新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>

2 个答案:

答案 0 :(得分:0)

您需要检查$ nama_file1以及其他是否为空。

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

答案 1 :(得分:0)

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

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

<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变量。