[英]Form edit for input file
我正在制作編輯數據表格。 然后,在編輯表單中,有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>
您需要檢查$ 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變量。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.