1

When I search, I get a result but it returns a blank form. I am able to complete a search with no errors but when complete the search the HTML is empty. I cannot figure out what is wrong here. MySQL connect has been changed for obvious reasons Here is my code:

<?php
    define('DB_NAME', 'name');
    define('DB_USER', 'user');
    define('DB_PASS', 'password');
    define('DB_HOST', 'localhost');

    $link = mysql_connect(DB_HOST, DB_USER, DB_PASS);

    if (!$link) {
        dir('There was a problem when trying to connect to the host. Error: ' . mysql_error());    
    }

    $db_selected = mysql_select_db(DB_NAME, $link);

    if (!$link) {
        dir('There was a problem when trying to connect to the database. Error: ' . mysql_error());    
    }

    $output = ""; 

    if (isset($_POST['search'])) {
        $searchq = $_POST['search'];

        $query = mysql_query("SELECT * FROM fac WHERE firstname LIKE '%$searchq%' OR lastname LIKE '%$searchq%'") or die("Could not complete search");
        $count = mysql_num_rows($query); 
        if ($count == 0) {
            $output = 'There where no search results';
        }else {             
            while ($row = mysql_fetch_array($query)) {
                $fname = $_POST['firstname'];
                $mname = $_POST['middlename'];
                $lname = $_POST['lastname'];
                $address = $_POST['address'];
                $car_type = $_POST['car_type'];
                $plate_number = $_POST['plate_number'];
                $intel = $_POST['intel'];

                $output .='<div> ID:'.$id.' <br /> First Name: </> '.$fname.' <br />  Middle Name: </>'.$mname.' <br />  Last Name: </> '.$lname.' <br />  Address: </> '.$address.' <br />  Car Type: </>'.$car_type.'<br />  Plate Number: </> '.$plate_number.' <br />  Intel: </> '.$intel.' <br />  </h3> </div>  <br />';
            }
        }       
    } 
?>
<html> 
    <head>
        <title>Search</title>
    </head>
    <body>
        <form action="search-entry-fac.php" method="post">
            <input type="text" name="search" placeholder="Search For Entry">
            <input type="submit" name="search_submit" value=">>"
        </form>
        <br />
        <?php print("$output"); ?>
    </body>
</html>
Ian
  • 20,207
  • 21
  • 53
  • 96

1 Answers1

2

In your code, you are using $_POST instead of $row.

It should be like this:

while ($row = mysql_fetch_array($query)) {
    $fname = $row['firstname'];
    $mname = $row['middlename'];
    $lname = $row['lastname'];
    $address = $row['address'];
    $car_type = $row['car_type'];
    $plate_number = $row['plate_number'];
    $intel = $row['intel'];

Sidenote: in this code $output .='<div> ID:'.$id.' <br /> [...]', you are using a variable called $id, there is no $id = SOMETHING;, so I assume you are missing $id = $row['YOUR_ID_COLUMN'];.


Note2: to see this type of erros, you can take a look at How do I get PHP Errors to display?

Note3: mysql_* functions are deprecated and should be avoided. And since this is probably user input, take a look at How can I prevent SQL-injection in PHP?

Community
  • 1
  • 1
FirstOne
  • 5,165
  • 5
  • 24
  • 44