Postingan lainnya
Data tidak bisa ditambah dan diubah dalam PHP
Awalnya semua fungsi berjalan normal, tapi setelah menambah fungsi upload, pas mau edit dan tambah data selalu muncul alert data gagal diubah/tambah. Padahal codingan sudah sesuai. Mohon bantuannya.
Ini codingan fungsi tambah dan upload
<?php
$conn = mysqli_connect("localhost", "root", "", "iniphpdasar");
function query($query) {
global $conn;
$result = mysqli_query($conn, $query);
$rows = [];
while( $row = mysqli_fetch_assoc($result)) {
$rows[] = $row;
}
return $rows;
}
function tambah($data) {
global $conn;
$nama = htmlspecialchars($data["nama"]);
$nrp = htmlspecialchars($data["nrp"]);
$email = htmlspecialchars($data["email"]);
$jurusan = htmlspecialchars($data["jurusan"]);
$alamat = htmlspecialchars($data["alamat"]);
$gambar = upload();
if (!$gambar){
return false;
}
$query = "INSERT INTO mahasiswaaa
VALUES
('', '$nama', '$nrp', '$email', '$jurusan', '$alamat, '$gambar')";
mysqli_query($conn, $query);
return mysqli_affected_rows($conn);
}
function upload(){
$namaFile = $_FILES['gambar'] ['name'];
$ukuranFile = $_FILES['gambar'] ['size'];
$error = $_FILES ['gambar'] ['error'];
$tmpName = $_FILES ['gambar'] ['tmp_name'];
if ($error === 4) {
echo "
<script>
alert('Upload gambar terlebih dahulu!');
</script>
";
return false;
}
$ekstensiGambarValid = ['jpg', 'JPG', 'jpeg', 'JPEG', 'png', 'PNG'];
$ekstensiGambar = explode('.', $namaFile);
$ekstensiGambar = end($ekstensiGambar);
if (!in_array($ekstensiGambar, $ekstensiGambarValid)){
echo "
<script>
alert('Format file tidak sesuai!');
</script>
";
return false;
}
if ($ukuranFile > 2000000) {
echo "
<script>
alert('Ukuran gambar terlalu besar!');
</script>
";
return false;
}
$namaFileBaru = uniqid();
$namaFileBaru .= '.';
$namaFileBaru .= $ekstensiGambar;
move_uploaded_file ($tmpName, 'image/' . $namaFileBaru);
return $namaFileBaru;
}
Ini fungsi edit
function edit($data){
global $conn;
$id = $data["id"];
$nama = htmlspecialchars($data["nama"]);
$nrp = htmlspecialchars($data["nrp"]);
$email = htmlspecialchars($data["email"]);
$jurusan = htmlspecialchars($data["jurusan"]);
$alamat = htmlspecialchars($data["alamat"]);
$gambar = htmlspecialchars($data["gambarLama"]);
if ($_FILES['gambar']['error']=== 4) {
$gambarLama = "";
$gambar = $gambarLama;
} else {
$gambar = upload();
}
$query = "INSERT INTO mahasiswaaa SET
nama = '$nama',
nrp = '$nrp',
email = '$email',
jurusan = '$jurusan',
alamat = '$alamat
gambar = '$gambar'
WHERE id = $id
";
mysqli_query($conn, "INSERT INTO mahasiswaaa SET
nama = '$nama',
nrp = '$nrp',
email = '$email',
jurusan = '$jurusan',
alamat = '$alamat
gambar = '$gambar'
WHERE id = $id
"
);
return mysqli_affected_rows($conn);
}
Data yg ingin di-updateHasil
Belum ada Jawaban. Jadi yang pertama Jawaban
Login untuk ikut Jawaban