Rares Coco Rares Coco - 1 month ago 5
MySQL Question

search MySQL database using php doesn't work

i need to get something from databse and something don't work, no error and when i write in search bar : alex/bobo/stano nothing happens no message, nothing, only the form with search bar and submit

where is the problem?i can't find it



nume | prenume | id
stano bobo 1
alex bobo 2





Here is the code:



<?php
// 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>';