Jump to content

problem with search function


postbil.com

Recommended Posts

Hello everybody.

I had a small problem with a search function on my site. The idea was to create a function there make it possible for the user to search for friends on the site. I had written the code but of some reason it doesn’t work, now I have spend a lot of time to find the error but I have no more ideas.. Somebody please help me, tell me what I do wrong..

Here is my code:

 

<?php

    include '../DB/db.connect.inc.php'; 

?>

<html>

    <head>

        <title>WWW.POSTBIL.COM - Projekt arbejde.</title>

        <link rel="stylesheet" type="text/css" href="../style/style.css">

        <style type="text/css">

            th { background-color: #999;}

            .odd_row { background-color: #EEE;}

            .even_row { background-color: #FFF;}

        </style>

    </head>

    <body>

        <h2>Search for frinds</h2>

        <form method="post" action="add_contact.php" >

        <table>

            <tr>

                <td><input type="text" name="search" id="search"></td>

                <td><input type="submit" name="submit" id="submit" value="search"></td>

            </tr>

        </table>

 

        <table style="width="100%">

            <tr><th>Email</th><th>Fifstnavn</th><th>Lastnavn</th><th>Adresse</th><th>City</th><th>contry</th><th>Phone</th><th>Homepage</th><th>Work</th><th>Company</th><th>Work adress</th><th>Work city</th><th>Work phone</th><th>Direct work phone</th><th>Work homepage</th></tr>

<?php

    if(isset($_POST['submit']) && $_POST['submit'] == 'search'){ 

        $search = (isset($_POST['search'])) ? trim($_POST['search']) : '';       

// Here I had my Mysql statement

    $query = 'SELECT firstName, lastName, email, adress, city, contry, phone, homepage, work, workCompany, workAdress, workCity, workPhone, workDirectPhone, workHomepage

        FROM site_user

        WHERE firstName LIKE '%$search%'';

        $result = mysql_query($query, $conn) or die(mysql_error($conn));

            $odd = true;

            while ($row = mysql_fetch_array($result)){

                echo ($odd == true) ? '<tr class="odd_row">' : '<tr class="even_row">';

                $odd = !$odd;

                echo '<td><a href="user.php?id=' . $row['user_id'] . '">' .

                    $row['email'] . '</a></td>';

                echo '<td><a href="user.php?id=' . $row['user_id'] . '">' .

                    $row['firstName'] . '</a></td>';

                echo '<td><a href="user.php?id=' . $row['user_id'] . '">' .

                    $row['lastName'] . '</a></td>';

                echo '<td><a href="user.php?id=' . $row['user_id'] . '">' .

                    $row['adress'] . '</a></td>';

                echo '<td><a href="user.php?id=' . $row['user_id'] . '">' .

                    $row['city'] . '</a></td>';

                echo '<td><a href="user.php?id=' . $row['user_id'] . '">' .

                    $row['contry'] . '</a></td>';

                echo '<td><a href="user.php?id=' . $row['user_id'] . '">' .

                    $row['phone'] . '</a></td>';

                echo '<td><a href="user.php?id=' . $row['user_id'] . '">' .

                    $row['homepage'] . '</a></td>';

                echo '<td><a href="user.php?id=' . $row['user_id'] . '">' .

                    $row['work'] . '</a></td>';

                echo '<td><a href="user.php?id=' . $row['user_id'] . '">' .

                    $row['workCompany'] . '</a></td>';

                echo '<td><a href="user.php?id=' . $row['user_id'] . '">' .

                    $row['workAdress'] . '</a></td>';

                echo '<td><a href="user.php?id=' . $row['user_id'] . '">' .

                    $row['workCity'] . '</a></td>';

                echo '<td><a href="user.php?id=' . $row['user_id'] . '">' .

                    $row['workPhone'] . '</a></td>';

                echo '<td><a href="user.php?id=' . $row['user_id'] . '">' .

                    $row['workDirectPhone'] . '</a></td>';

                echo '<td><a href="user.php?id=' . $row['user_id'] . '">' .

                    $row['workHomepage'] . '</a></td>';

                echo '</tr>';

      }

 

      mysql_free_result($result);

      mysql_close($conn);

    }

?>           

        </table>

        </form>

    </body>

</html>

 

Postbil.com

 

 

Link to comment
https://forums.phpfreaks.com/topic/191603-problem-with-search-function/
Share on other sites

Keynames in result row are lowercased

so $row['userWebsite'] = null but $row['userwebsite'] should have some values

 

And btw if you selecting all (or almost all) columns from table u can use

"SELECT * FROM blab blab bla" instead "SELECT col1,col2,col3, ..., col100 FROM ..."

 

and yes LIKE '%%' should return all rows

After query result is fetched php automaticlly makes index name of array lowercased

 

for example

$row['workHomepage'] doesn't exists what u realy want to echo is

$row['workhomepage'] 

 

simply u can var_dump $row and u will see

 

while($row = ...)

{

var_dump($row);

 

//rest of the code

}

 

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.