0

i have a dependent drop-down list which composition is being populated depending on KNITTYPE. But when i try to order the composition i can not get any result, it doesnt work . what am i doing wrong ?

my second question is : how can i eliminate the repeat of the same composition results i know they belong to different rows but i want to merge them so for eaxmple when i select 100%COTTON i want to bring all 100%COTTON results , rightnow its bringing 100% cotton for each KNITTYPE SELECTED ?

<?php
    require_once 'dbconnect.php';


    if(!empty($_REQUEST["KNITTYPE_id"])) {

    $query ="SELECT COMPOSITION FROM egearge3 WHERE KNITTYPE =" . "'" . mysqli_escape_string($conn, $_POST["KNITTYPE_id"] ) ."' AND ORDER COMPOSITION BY ASC" ;
    $result = mysqli_query($conn, $query);
?>
    <option value="">Select COMPOSITION</option>

<?php
    while($row2=mysqli_fetch_assoc($result)){

        //var_dump($row2);
        if($bul2[$row2['COMPOSITION']] != true && $row2['COMPOSITION'] != 'COMPOSITION' || 1)        { ?>
            <option value="<?php echo $row2['COMPOSITION']; ?>"><?php echo     $row2['COMPOSITION']; ?>  </option>
 <?php  
         $bul2[$row2['COMPOSITION']] = true;
         }
     }
    }
?>
  • String escaping is not always sufficient to protect you from [**SQL injection**](https://en.wikipedia.org/wiki/SQL_injection) attacks. You should use prepared statements with bound parameters, via either the [**mysqli**](https://secure.php.net/manual/en/mysqli.prepare.php) or [**PDO**](https://secure.php.net/manual/en/pdo.prepared-statements.php) drivers. [**This post**](https://stackoverflow.com/questions/60174/how-can-i-prevent-sql-injection-in-php) has some good examples. – Alex Howansky Jun 28 '17 at 14:44

3 Answers3

0
AND ORDER COMPOSITION BY ASC

That is not valid.

You don't need a AND before ORDER and the correct syntax is ORDER BY X, and not ORDER X BY

ORDER BY COMPOSITION ASC

See DISTINCT to remove duplicate entries from the result

Clément Malet
  • 5,062
  • 3
  • 29
  • 48
0

Your MYSQL query statement is not in the right way

        if(!empty($_REQUEST["KNITTYPE_id"])) {

        $query ="SELECT COMPOSITION FROM egearge3 WHERE KNITTYPE =" . "'" . mysqli_escape_string($conn, $_POST["KNITTYPE_id"] ) ."'  ORDER BY COMPOSITION ASC LIMIT 1" ;
        $result = mysqli_query($conn, $query);
    ?>
        <option value="">Select COMPOSITION</option>

    <?php
        while($row2=mysqli_fetch_assoc($result)){

            //var_dump($row2);
            if($bul2[$row2['COMPOSITION']] != true && $row2['COMPOSITION'] != 'COMPOSITION' || 1)        { ?>
                <option value="<?php echo $row2['COMPOSITION']; ?>"><?php echo     $row2['COMPOSITION']; ?>  </option>
     <?php  
             $bul2[$row2['COMPOSITION']] = true;
             }
         }
        }
    ?>
Osama
  • 2,912
  • 1
  • 12
  • 15
0

this worked for me i guess

<?php
    require_once 'dbconnect.php';


    if(!empty($_REQUEST["KNITTYPE_id"])) {

    $query ="SELECT COMPOSITION FROM egearge3 WHERE KNITTYPE =" . "'" . mysqli_escape_string($conn, $_POST["KNITTYPE_id"] ) ."' GROUP BY COMPOSITION " ;
    $result = mysqli_query($conn, $query);
?>
    <option value="">Select COMPOSITION</option>

<?php
    while($row2=mysqli_fetch_assoc($result)){

        //var_dump($row2);
        if($bul2[$row2['COMPOSITION']] != true && $row2['COMPOSITION'] != 'COMPOSITION' || 1)        { ?>
            <option value="<?php echo $row2['COMPOSITION']; ?>"><?php echo     $row2['COMPOSITION']; ?>  </option>
 <?php  
         $bul2[$row2['COMPOSITION']] = true;
         }
     }
    }
?>