我有一些问题,当我上传图片时,图片没有出现 注意 : 甘巴->图片
Index.php
<script>
$(document).ready(function(){
$('#insert_form').on("submit", function(event) {
event.preventDefault();
if($('#id_barang').val() == "") {
alert("ID Barang is required");
} else if ($('#nama_barang').val() == '') {
alert("Nama Barang is required");
} else if ($('#gambar').val() == '') {
alert("Gambar is required");
} else {
$.ajax({
url:"insert.php",
method:"POST",
data:$('#insert_form').serialize(),
beforeSend:function(){
$('#insert').val("Inserting");
},
success:function(data){
$('#insert_form')[0].reset();
$('#add_data_Modal').modal('hide');
$('#barang_table').html(data);
}
});
}
});
$(document).on('click', '.view_data', function(){
//$('#dataModal').modal();
var barang_id = $(this).attr("id");
$.ajax({
url:"select.php",
method:"POST",
data: {barang_id:barang_id},
success:function(data){
$('#barang_detail').html(data);
$('#dataModal').modal('show');
}
});
});
});
</script>
Insert.php数据库查询
<?php
//insert.php
$connect = mysqli_connect("localhost", "root", "", "db_test_nutech");
if(!empty($_POST)) {
$output = '';
$id_barang = mysqli_real_escape_string($connect, $_POST["id_barang"]);
$nama_barang = mysqli_real_escape_string($connect, $_POST["nama_barang"]);
// $gambar = mysqli_real_escape_string($connect, $_POST["gambar"]);
$harga_beli = mysqli_real_escape_string($connect, $_POST["harga_beli"]);
$harga_jual = mysqli_real_escape_string($connect, $_POST["harga_jual"]);
$stock = mysqli_real_escape_string($connect, $_POST["stock"]);
if (!empty($_FILES['gambar']['name'])) {
$upload = '../assets/upload/'. basename($_FILES['gambar']['name']);
$nama_gambar = $_FILES['gambar']['name'];
$tipe_gambar = $_FILES['gambar']['type'];
$size_gambar = $_FILES['gambar']['size'];
$dataTipeGambar = array("image/png","image/jpg/","image/jpeg");
// jika tipe gambar sama dengan data tipe gambar maka tersedia
// kalau tidak berarti tipe gambar tidak tersedia atau error
if (!in_array($tipe_gambar, $dataTipeGambar)) {
echo "Tipe Gambar Tidak Tersedia!"; die;
} else {
// move_uploaded_file berfungsi untuk memindahkan sebuah gambar kedalam folder yang kita siapkan untuk gambar
if ($size_gambar <= 160000){
if (move_uploaded_file($_FILES['gambar']['tmp_name'], $upload)) {
unlink('../assets/upload/'.$row['gambar']);
$query = mysqli_query($connect, "INSERT INTO tbl_barang (id_barang,nama_barang, gambar, harga_beli, harga_jual,stock) VALUES ('$id_barang','$nama_barang','$nama_gambar','$harga_beli','$harga_jual','$stock')");
if ($query) {
header("location:index.php?tambah=berhasil");
} else {
header("location:insert.php?tambah=gagal");
}
}
} else {
echo "Max Size Upload Gambar adalah 100KB";die;
}
}
} else {
$query = mysqli_query($connect, "INSERT INTO tbl_barang (id_barang,nama_barang, gambar, harga_beli, harga_jual,stock) VALUES ('$id_barang','$nama_barang','$nama_gambar','$harga_beli','$harga_jual','$stock')");
if ($query) {
header("location:index.php?tambah=berhasil");
} else {
header("location:insert.php?tambah=gagal");
}
}
}
此代码和我的问题的查询.. 如果需要更多代码,请发表评论,我将发布其余代码
答案 0 :(得分:0)
问题是您的ajax没有设置用于文件上传。
要上传文件,您需要将contentType和processdata设置为false,并使用未序列化的表单数据。
var formData = new FormData(this);
$.ajax({
url:"insert.php",
method:"POST",
contentType: false,
processData: false,
data:formData,
beforeSend:function(){
$('#insert').val("Inserting");
},
success:function(data){
$('#insert_form')[0].reset();
$('#add_data_Modal').modal('hide');
$('#barang_table').html(data);
}
});