Error PHP database mysqli_real_escape_string()

Halo semua, di code PHP saya mendapat error seperti ini

" Fatal error: Uncaught ArgumentCountError: mysqli_real_escape_string() expects exactly 2 arguments, 1 given in C:\xampp\htdocs\PRPL2021\registerdb.php:9 Stack trace: #0 C:\xampp\htdocs\PRPL2021\registerdb.php(9): mysqli_real_escape_string('') #1 C:\xampp\htdocs\PRPL2021\register.php(2): include('C:\\xampp\\htdocs...') #2 {main} thrown in C:\xampp\htdocs\PRPL2021\registerdb.php on line 9"

Apakah ada yang tahu solusinya?

Code saya :

<?php
$username = " ";
$email = " ";
$errors = array();

$db = mysqli_connect('localhost', 'root', '', 'prplregistration');

if (isset($_POST['register'])) {
    $username = mysqli_real_escape_string($_POST['username']);
    $email = mysqli_real_escape_string($_POST['email']);
    $password1 = mysqli_real_escape_string($_POST['password1']);
    $password2 = mysqli_real_escape_string($_POST['password2']);
}

if (empty($username)) {
    array_push($errors, "Username is required");
}
if (empty($email)) {
    array_push($errors, "Email is required");
}
if (empty($password1)) {
    array_push($errors, "Password is required");
}
if (empty($password2)) {
    array_push($errors, "Password does not match");
}

if (count($errors) == 0) {
    $password = md5($password1);
    $sql = "INSERT INTO Users (username, email, password)
    VALUES ('$username', '$email', '$password')";
    mysqli_query($db, $sql);
}

?>
avatar anggito
@anggito

2 Kontribusi 1 Poin

Diperbarui 3 tahun yang lalu

1 Jawaban:

<div>Ubah bagian ini :</div><pre>if (isset($_POST['register'])) { $username = mysqli_real_escape_string($_POST['username']); $email = mysqli_real_escape_string($_POST['email']); $password1 = mysqli_real_escape_string($_POST['password1']); $password2 = mysqli_real_escape_string($_POST['password2']); }</pre><div><br>Menjadi seperti ini :</div><pre>if (isset($_POST['register'])) { $username = mysqli_real_escape_string($db, $_POST['username']); $email = mysqli_real_escape_string($db, $_POST['email']); $password1 = mysqli_real_escape_string($db, $_POST['password1']); $password2 = mysqli_real_escape_string($db, $_POST['password2']); }</pre><div><br>Semoga membantu</div>

avatar syahid246
@syahid246

70 Kontribusi 160 Poin

Dipost 3 tahun yang lalu

Login untuk ikut Jawaban