1

I created a form for a user to enter in an employee first name and last name. When the user it submits it has to select everything from the employees database where the entry is like the first_name column in the employees table. When I do that it does not show up on the page so I believe there must be something wrong that I can not find.

        <!doctype html>
<html lang="en">
<head>
    <link href="employeeStyles.css" rel="stylesheet">
    <title>Employee Search</title>
</head>
<body>
    <div id="employeeArea">
        <h1>Employee Search Results</h1>
        <?php
        $firstName = $_GET['firstName'];
        $lastName = $_GET['lastName'];
        $resultsNumber = $_GET['resultsNumber'];


        @ $db = new mysqli('localhost','root','','employees');

        $firstName = $db->real_escape_string($firstName);
        $lastName = $db->real_escape_string($lastName);
        $resultsNumber = $db->real_escape_string($resultsNumber);

        if (mysqli_connect_errno()){
            echo 'Error: Could not connect to the database. Please try again later. </body></html>';
            exit;
        }
        $query = "SELECT * FROM employees WHERE first_name LIKE .$firstName.'%'";
        $result = $db->query($query);
        $numResults = $result->num_rows;

        echo 'Number of results found '.$numResults;

        for ($i=0; $i<$result; $i++){
            $row = $result->fetch_assoc();
            echo $row ['first_name']."<br>";
            echo $row ['last_name']."<br>";
            echo $row ['emp_no']."<br>";
            echo $row ['hire_date']."<br>";
            echo $row ['birth_date']."<br>";
            echo $row ['gender']."<br>";
        }

        $db->close();
        ?>
    </div>
</body>

Funk Forty Niner
  • 74,450
  • 15
  • 68
  • 141
Matt A
  • 49
  • 7

1 Answers1

1

Well, the problem lays in your query.

The variable you pass onto the query isn't enclosed inside of quotes. Try:

$query = "SELECT * FROM employees WHERE first_name LIKE '" . $firstName . "%'";
Thoby
  • 316
  • 1
  • 6
  • That query works but I now get an error Object of class mysqli_result could not be converted to int, I think it is something to do with my for loop where I display the results – Matt A Apr 18 '17 at 13:41
  • Try `for ($i=0; $i<$numResults; $i++){`for your loop. – Thoby Apr 18 '17 at 13:46