php - 输入文件的表单编辑

标签 php mysql web crud edit

我正在制作编辑数据表单。然后在编辑表单中,有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)){
...
}

关于php - 输入文件的表单编辑,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52529320/

相关文章:

php - Symfony 和 Doctrine : lazy loading is not working

php - Magento 为媒体使用第二个数据库 - SQLSTATE [42S02] : Base table or view not found

mysql - sql语句选择约束

javascript - 如何从 Dojo gridx detailProvider(Dod 模块)获取声明类的引用

javascript - 链接两个网页或网络应用程序的最佳方式

php - 中断 php 进程以获取 js 变量

java - 登录成功后如何在另一个 Activity (配置文件 Activity )中加载用户的数据库?我正在尝试创建一个个人资料页面

MySQL 触发器抛出错误 1064

mysql - Group Concat 两列,但在 GROUPS 中——涉及三个分隔符

c# - 使用 C# 提供 HTML