Subject: I just started to learn PHP So I'll try to make a search using form feature then to prevent SQLijections I add mysqli_real_escape_string will it work? And please help me correct is there something wrong with my code??
my index.php :
<?php
include 'connect.php';
$sql = "SELECT * FROM anime";
$result = mysqli_query($conn, $sql);
$queryresult = mysqli_num_rows($result);
$anime = query("SELECT * FROM anime");
if (isset($_GET['s'])) {
$search = mysqli_real_escape_string($conn, $_GET['s']);
$sql = "SELECT * FROM anime WHERE Judul LIKE '%$search%'";
$anime = query($sql);
}
?>
<!DOCTYPE html>
<html lang="en" dir="ltr">
<head>
<meta charset="utf-8">
<title></title>
</head>
<body>
<form action="" method="get">
<input type="text" name="s" placeholder="keyword">
<button type="submit">Search</button>
</form>
<?php foreach ($anime as $a) : ?>
<li><?php echo $a ['Judul']; ?></li>
<?php endforeach ; ?>
</body>
</html>
connect.php :
<?php
$server = "localhost";
$username = "root";
$password = "";
$databasename = "anime";
$conn = mysqli_connect("$server", "$username", "$password", "$databasename");
function query($query) {
global $conn;
$result = mysqli_query($conn, $query);
$rows = [];
while( $row = mysqli_fetch_assoc($result) ) {
$rows[] = $row;
}
return $rows;
}