Postingan lainnya
Fatal error: Uncaught Error: Call to undefined function query()
Permisi, ini saya error kaya gini terus bingung mau apalagi yang diganti mohon pencerahannya..
Error : Fatal error: Uncaught Error: Call to undefined function query() in C:\xampp\htdocs\web-perpustakaan\config\functions.php:46 Stack trace: #0 C:\xampp\htdocs\web-perpustakaan\register.php(6): register(Array) #1 {main} thrown in C:\xampp\htdocs\web-perpustakaan\config\functions.php on line 46
ini Functions.php <?php
require_once 'koneksi.php';
function upload() {
$namaFoto = $_FILES['foto']['name'];
$ukuranFoto = $_FILES['foto']['size'];
$error = $_FILES['foto']['error'];
$tmpFoto = $_FILES['foto']['tmp_name'];
if($error === 4) {
echo "<script>alert('pilih gambar terlebih dahulu.');</script>";
return false;
}
$fotoValid = ['jpg','jpeg','png'];
$ektensiFoto = explode('.', $namaFoto);
$ektensiFoto = strtolower(end($ektensiFoto));
if(!in_array($ektensiFoto, $fotoValid)) {
echo "<script>alert('yang anda upload bukan gambar.');</script>";
return false;
}
// cek ukuran
if($ukuranFoto > 1000000) {
echo "<script>alert('ukuran gambar terlalu besar.');</script>";
return false;
}
$fileNameBaru = uniqid();
$fileNameBaru .= '.';
$fileNameBaru .= $ektensiFoto;
move_uploaded_file($tmpFoto, '../img/' . $fileNameBaru);
return $fileNameBaru;
}
function register($data) {
global $conn;
$nama = htmlspecialchars($data['nama']);
$username = $conn->real_escape_string($_POST['username']);
$password = $conn->real_escape_string($_POST['password']);
$password2 = $conn->real_escape_string($_POST['password2']);
// jika username sudah terdaftar
if(query("SELECT * FROM user WHERE username = '$username'")) {
echo "<script>alert('Username sudah terdaftar!');window.location='register.php';</script>";
return false;
}
if($password != $password2) {
echo "<script>alert('konfirmasi password salah.');</script>";
return false;
}
if(strlen($username) < 6 ) {
echo "<script>alert('Password terlalu pendek, maksimal 6 digit');window.location='register.php';</script>";
return false;
}
// cek gambar
$foto = upload();
if(!$foto) {
return false;
}
$password = password_hash($password, PASSWORD_DEFAULT);
$conn->query("INSERT INTO tb_user VALUES (null, '$username', '$password', '$nama', '$foto')") or die(mysqli_error($conn));
return $conn->affected_rows;
}
Tanggapan
rapikan kode kamu, taro di tag kode agar mudah dibaca, orang malas baca kode yang asal dicopypaste
ampun, baca kodenya gmna itu -_-
1 Jawaban:
<div>Function.php<br><br><?php </div><div>require_once 'koneksi.php';</div><div><br></div><div>function upload() {</div><div> $namaFoto = $_FILES['foto']['name'];</div><div> $ukuranFoto = $_FILES['foto']['size'];</div><div> $error = $_FILES['foto']['error'];</div><div> $tmpFoto = $_FILES['foto']['tmp_name'];</div><div><br></div><div> if($error === 4) {</div><div> echo "<script>alert('pilih gambar terlebih dahulu.');</script>";</div><div> return false;</div><div> }</div><div><br></div><div> $fotoValid = ['jpg','jpeg','png'];</div><div> $ektensiFoto = explode('.', $namaFoto);</div><div> $ektensiFoto = strtolower(end($ektensiFoto));</div><div><br></div><div> if(!in_array($ektensiFoto, $fotoValid)) {</div><div> echo "<script>alert('yang anda upload bukan gambar.');</script>";</div><div> return false;</div><div> }</div><div><br></div><div> // cek ukuran</div><div> if($ukuranFoto > 1000000) {</div><div> echo "<script>alert('ukuran gambar terlalu besar.');</script>";</div><div> return false;</div><div> }</div><div><br></div><div> $fileNameBaru = uniqid();</div><div> $fileNameBaru .= '.';</div><div> $fileNameBaru .= $ektensiFoto;</div><div><br></div><div> move_uploaded_file($tmpFoto, '../img/' . $fileNameBaru);</div><div> return $fileNameBaru;</div><div>}</div><div><br></div><div>function register($data) {</div><div> global $conn;</div><div> $nama = htmlspecialchars($data['nama']);</div><div> $username = $conn->real_escape_string($_POST['username']);</div><div> $password = $conn->real_escape_string($_POST['password']);</div><div> $password2 = $conn->real_escape_string($_POST['password2']);</div><div><br></div><div> // jika username sudah terdaftar</div><div> if(query("SELECT * FROM user WHERE username = '$username'")) {</div><div> echo "<script>alert('Username sudah terdaftar!');window.location='register.php';</script>";</div><div> return false;</div><div> }</div><div><br></div><div> if($password != $password2) {</div><div> echo "<script>alert('konfirmasi password salah.');</script>";</div><div> return false;</div><div> }</div><div><br></div><div> if(strlen($username) < 6 ) {</div><div> echo "<script>alert('Password terlalu pendek, maksimal 6 digit');window.location='register.php';</script>";</div><div> return false;</div><div> }</div><div><br></div><div> // cek gambar</div><div> $foto = upload();</div><div> if(!$foto) {</div><div> return false;</div><div> }</div><div><br></div><div> $password = password_hash($password, PASSWORD_DEFAULT);</div><div><br></div><div> $conn->query("INSERT INTO tb_user VALUES (null, '$username', '$password', '$nama', '$foto')") or die(mysqli_error($conn));</div><div> return $conn->affected_rows;</div><div>}</div><div><br><br></div>