Rares Coco Rares Coco - 1 month ago 5
MySQL Question

PHP - Search Function for MySQL Database Doesn't Work

I need to retrieve something from database but the search does not work. When I input in search bar the folloowing:

alex/bobo/stano
, nothing happens, it does not return any results. Could someone point to me where the problem lies?

nume | prenume | id
stano bobo 1
alex bobo 2


Here is the code:

// conectare la baza de date
session_start();
$db = mysqli_connect("localhost", "root", "", "inregistrare");
$output = '';
//conectare
if (isset($_POST['cauta'])) {
$cauta1 = $_POST['cauta'];
$cauta1 = preg_replace("#[^0-9a-z]#i","",$cauta1);

$query = mysqli_query($db, "SELECT * FROM users WHERE nume LIKE '%$cauta1%' OR prenume LIKE '%$cauta1%'");
$count = mysqli_num_rows($db, $query);
if ($count == 0) {
$output = 'Nu a fost gasit elevul';
} else {
while ($row = mysqli_fetch_array($query)) {
$nume = $row['nume'];
$prenume = $row['prenume'];
$id = $row['id'];

$output .= '<div>'.$nume.' '.$prenume.'</div>';
}
}
}




<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Cauta Elev</title>
</head>

<body>
<form method="post">
<input type="text" name="cauta" placeholder="Cauta Elevul"/>
<input type="submit" value="Cauta"/>
</form>
<?php print("$output"); ?>
</body>
</html>




Answer

You missed $ for nume and prenume vars in the $output. The correct one is:

$output .= '<div>'.$nume.' '.$prenume.'</div>';
Comments