1

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>

4
  • if(isset($_POST['cauta'])) Maybe is not set? Hint: check your form code again ;) Commented Oct 19, 2016 at 15:46
  • ops, i changed in the form, but now : Notice: Use of undefined constant cauta - assumed 'cauta' in .. on line 8 Commented Oct 19, 2016 at 15:52
  • You forgot to place cauta in quotes Commented Oct 19, 2016 at 15:55
  • 1
    You're just ASSUMING that nothing ever goes wrong. you don't check if you connected to the db properly, you don't check if the query succeeded (or even ran). That is exactly the WRONG attitude to have. Never EVER assume success when dealing with external resources. Always assume failure, check for failure, and treat success as a pleasant surprise. Commented Oct 19, 2016 at 17:38

2 Answers 2

2

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

$output .= '<div>'.$nume.' '.$prenume.'</div>';
Sign up to request clarification or add additional context in comments.

5 Comments

Notice: Use of undefined constant cauta - assumed 'cauta' in .. on line 8 Warning: mysqli_query() expects at least 2 parameters, 1 given in ... on line 11 Warning: mysqli_num_rows() expects parameter 1 to be mysqli_result, null given in .. on line 12
You have to pass $db as first param of mysqli_query
And use $_POST['cauta']
Warning: mysqli_num_rows() expects exactly 1 parameter, 2 given in .. on line 12
Remove $db from mysqli_num_rows. Pass only $query
1

I think it should be your final 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($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>

2 Comments

yes, works, it.s a way to add a function when get info from database to capitalize first letter?
To show in capitalize first letter you can use $output .= '<div>'.ucfirst($nume).' '.ucfirst($prenume).'</div>';

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.