0

//my php code that connects to database

<?php
    $output = NULL;
    if(isset($_POST['submit'])){

        $search = $link->real_escape_string($_POST['search']);

        //Query the database
        $resultSet = $link->query("SELECT * FROM members WHERE city LIKE '$search%'");

        if($resultSet->num_rows > 0){
            while($rows = $resultSet->fetch_assoc())
            {
                $fname = $rows['firstname'];
                $lname = $rows['lastname'];
                $city = $rows['city'];
                $price = $rows['price'];
                $img = $rows['img'];
                $output = " $city ";

            }
        } else{
         $output = "no results";
        }

    }
?>

// DIv where i want to display the results dynamically

<ul class="products">
    <li >
        <a href="#" >
            <img width="450" height="450" src="<?php echo $img; ?>"  alt="Dancing between Red Mountain" title="nature-person-red-woman" />
            <h3><?php echo $fname ." ". $lname; ?></h3> <span class="price"><span >

                <span >&pound;</span><?php echo $price; ?></span>
                </span>

        </a>
    </li>
</ul>
RiggsFolly
  • 93,638
  • 21
  • 103
  • 149
  • Your script is at risk of [SQL Injection Attack](http://stackoverflow.com/questions/60174/how-can-i-prevent-sql-injection-in-php) Have a look at what happened to [Little Bobby Tables](http://bobby-tables.com/) Even [if you are escaping inputs, its not safe!](http://stackoverflow.com/questions/5741187/sql-injection-that-gets-around-mysql-real-escape-string) Use [prepared parameterized statements](http://php.net/manual/en/mysqli.quickstart.prepared-statements.php) – RiggsFolly Jun 04 '17 at 16:48
  • Put your DIV output INSIDE the while loop. It is the WHILE loop that is consuming all your result set – RiggsFolly Jun 04 '17 at 16:54
  • sorry i'm new to this, but if i wrap it in it if i have 10 results will they stack up one after the other like a ecommerce search result – Gabriel Koch Jun 04 '17 at 16:57

1 Answers1

0

Put your DIV output INSIDE the while loop. It is the WHILE loop that is consuming all your result set

<?php
    $output = NULL;
    if(isset($_POST['submit'])){

        $search = $link->real_escape_string($_POST['search']);

        //Query the database
        $resultSet = $link->query("SELECT * FROM members WHERE city LIKE '$search%'");

        if($resultSet->num_rows > 0){
            while($rows = $resultSet->fetch_assoc())
            {
                $fname = $rows['firstname'];
                $lname = $rows['lastname'];
                $city = $rows['city'];
                $price = $rows['price'];
                $img = $rows['img'];
                $output = " $city ";
                // DIv where i want to display the results dynamically
?>  
                <ul class="products">
                    <li>
                        <a href="#">
                            <img width="450" height="450" src="<?php echo $img; ?>" alt="Dancing between Red Mountain" title="nature-person-red-woman" />
                            <h3><?php echo $fname ." ". $lname; ?></h3> 
                            <span class="price"><span >
                            <span>&pound;</span><?php echo $price; ?></span>
                        </a>
                    </li>
                </ul>
<?php
           } // endwhile
        } else{
         $output = "no results";
        }

    }
?>
RiggsFolly
  • 93,638
  • 21
  • 103
  • 149