0

Search results

index.php

enter image description here

The textarea values are passing to the another page but its only executing one query

Search Page

<?php 
$text = trim($_POST['textarea']);
$textAr = explode("\n", $text);
$textAr = array_filter($textAr, 'trim'); // remove any extra \r characters left behind

foreach ($textAr as $line) {

} 

$count=count($textAr);


for($i=0;$i<$count;$i++)
{   
$cusdet = "SELECT * from `product` WHERE category LIKE '%" .$textAr[$i]. "%'"; 
echo "SELECT * from products where name='$textAr[$i]'<br><br>"; ?>
 <?php
$query =  mysql_query($cusdet) or die(mysql_error()) ;  
while ($row = mysql_fetch_array($query)) { ?> 
<br><b><?php    echo $row['product']; ?></b><br> <?php echo $row['category']; 
} 

 }   
?>

Index Page

 <form method="post" action="search.php">
  <div class="form-group">
  <textarea class="form-control" id="textarea" name="textarea" rows="15" placeholder="Eg:Milk Powder"></textarea>
  </div>
  <div class="col-md-8 col-xs-12 col-sm-7" style="background-color:#1b934d;margin-top: -14px;">
  <h3 style="color:white;font-size: 21px;">To start shopping with the shoppinglist.</h3>
  </div>
  </form>

I need it to be displayed

Bangles
All the products of bangles

Rings
All the products of Rings

Necklaces
All the products of necklaces

  • var_dump($textAr) ; and see the problem – Sanooj T Jan 03 '17 at 11:27
  • 1
    Every time you use [the `mysql_`](http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php) database extension in new code **[a Kitten is strangled somewhere in the world](http://2.bp.blogspot.com/-zCT6jizimfI/UjJ5UTb_BeI/AAAAAAAACgg/AS6XCd6aNdg/s1600/luna_getting_strangled.jpg)** it is deprecated and has been for years and is gone for ever in PHP7. If you are just learning PHP, spend your energies learning the `PDO` or `mysqli` database extensions. [Start here](http://php.net/manual/en/book.pdo.php) – RiggsFolly Jan 03 '17 at 11:27
  • 2
    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 Jan 03 '17 at 11:27
  • array(3) { [0]=> string(8) "bangles " [1]=> string(9) "earrings " [2]=> string(9) "necklaces" } array(3) { [0]=> string(8) "bangles " [1]=> string(9) "earrings " [2]=> string(9) "necklaces" } array(3) { [0]=> string(8) "bangles " [1]=> string(9) "earrings " [2]=> string(9) "necklaces" } var_dump($textAr) ; @SanoojT – Aß RahuMan Jan 03 '17 at 13:44

0 Answers0