0

first let me say that i'm pretty much new to PHP. Im having fun trying it out and learning it! Now I ran into something that is out of my league.

What Im trying to do is : only return the results where there is an X in a certain column.

The Screenshot is partly in german but i will explain :

What it looks like

enter image description here

Currently I have 2 search forms. My code is probably pretty bad or can be made way simpler.. I just dont know how. The problem is most of it is in 1 table and 1 part in a different table.

Now I want to have it where if I search a product, it should only put out the products where there is an X in the column 'BSTHINW3'

The 2 scripts I use to make it possible right now :

<?php
mysql_connect("localhost", "root", "") or die("Fehler: ".mysql_error());


mysql_select_db("database") or die(mysql_error());

?>

<html>
<head>
<title>Artikelsuche</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<link rel="stylesheet" type="text/css" href="style.css"/>
</head>
<body>


<form action="index.php" method="GET">
    Artikelnummer : 
    <input type="text" name="query" />
    <input type="submit" value="Search" />
</form>

<br>

<div id="aktivsu">
<form action="index2.php" method="GET">
    Aktiv : 
    <input type="text" name="query2" />
    <input type="submit" value="Active" />
</form>
</div>

<br>

<?php
$query = $_GET['query']; 


$min_length = 1;


if(strlen($query) >= $min_length){ 

    $query = htmlspecialchars($query); 


    $query = mysql_real_escape_string($query);


    $raw_results = mysql_query("SELECT BSTARTMASS, BSTKATNR, BSTLIEFBST,  
                                        BSTARTBES1, BSTANF, BSTLIEFMIN, 
                                        BSTSTLIO, BSTMIND, BSTKUMST, 
                                        BSTKUMVK, BSTKUMER 
                                FROM elebest
                                WHERE (`BSTLIEFBST` LIKE '%".$query."%' 
                                   OR `BSTKATNR` LIKE '%".$query."%' 
                                   OR `BSTLIEFTXT` LIKE '%".$query."%')") 
                or die(mysql_error());

    $raw_results = mysql_query("SELECT BSTARTMASS, BSTKATNR, BSTLIEFBST, 
                                        BSTARTBES1, BSTANF, BSTLIEFMIN, 
                                        BSTSTLIO, BSTMIND,  BSTKUMSTVJ, 
                                        BSTKUMVKVJ, BSTKUMERVJ, 
                                        BSTKUMST, BSTKUMVK, BSTKUMER 
                            FROM vorjahr
                            WHERE (`BSTLIEFBST` LIKE '%".$query."%' 
                               OR `BSTKATNR` LIKE '%".$query."%' 
                               OR `BSTLIEFTXT` LIKE '%".$query."%')") 
                    or die(mysql_error());



    if(mysql_num_rows($raw_results) > 0){ 

        while($results = mysql_fetch_array($raw_results)){

            echo '<br>' ;
            echo '<br>' ;
            echo '<br>' ;
            echo "<div id='artnr'> Number: ".$results['BSTKATNR']. "</div>";
            echo "<div id='lfnr'> Nr LF / Code : ".$results['BSTLIEFBST']."</div>";

            echo '<br>' ;
            echo "<div id='3'> Desc: ".$results['BSTARTBES1']."</div>";

            echo '<br>' ;

            echo "<div id='bestandverf'>" ;
            echo "Bestand : ".$results['BSTANF']."";
            echo '<br>' ;
            echo "Verfügbar : ".$results['BSTSTLIO']."";
            echo "</div>" ;

            echo "<div id='minbes'>" ;
            echo "Mindestbestellung : ".$results['BSTLIEFMIN']."";
            echo '<br>' ;
            echo "Mindestbestand : ".$results['BSTMIND']."";
            echo "</div>" ;

            echo "<div id='verkakt'>" ;
            echo "Current Year ".$results['BSTARTMASS']."";
            echo "</div>" ;             

            echo '<br>';
            echo "<div id='aktjahr'>" ;
            echo "Quantity : ".$results['BSTKUMST']."";
            echo '<br>' ;
            echo " Sales : ".$results['BSTKUMVK']." &euro;";
            echo '<br>' ;
            echo " Income : ".$results['BSTKUMER']." &euro;";
            echo "</div>" ;

            echo '<br>';

            echo "<div id='verkvj'>" ;
            echo "Last Year ".$results['BSTARTMASS']."";
            echo "</div>" ;

            echo "<div id='vj'>" ;
            echo "Quantity : ".$results['BSTKUMSTVJ']."";
            echo '<br>';
            echo "Sales : ".$results['BSTKUMVKVJ']." &euro;";
            echo '<br>';
            echo "Income : ".$results['BSTKUMERVJ']." &euro;";
            echo "</div>" ;



        }

    }

    else{ 
        echo "No results";
    }

}
else{ 
    echo "Minimum input length : ".$min_length;
}
?>
</body>

<?php
mysql_connect("localhost", "root", "") or die("Fehler: ".mysql_error());


mysql_select_db("database") or die(mysql_error());

?>

<html>
<head>
<title>Artikelsuche</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<link rel="stylesheet" type="text/css" href="style.css"/>
</head>
<body>


<form action="index.php" method="GET">
    Artikelnummer : 
    <input type="text" name="query" />
    <input type="submit" value="Search" />
</form>

<br>

<div id="aktivsu">
<form action="index2.php" method="GET">
    Aktiv : 
    <input type="text" name="query2" />
    <input type="submit" value="Active" />
</form>
</div>

<br>

<?php
$query2 = $_GET['query2'];


$min_length = 0;


if(strlen($query2) >= $min_length){ 

    $query2 = htmlspecialchars($query2); 


    $query2 = mysql_real_escape_string($query2);


    $raw_results = mysql_query("SELECT BSTHINW3, BSTARTMASS, BSTKATNR, 
                                        BSTLIEFBST, BSTARTBES1, BSTANF, 
                                        BSTLIEFMIN, BSTSTLIO, BSTMIND, 
                                        BSTKUMST, BSTKUMVK, BSTKUMER 
                                FROM elebest
                                WHERE (`BSTLIEFBST` LIKE '%".$query2."%' 
                                   OR `BSTKATNR` LIKE '%".$query2."%' 
                                   OR `BSTLIEFTXT` LIKE '%".$query2."%')") 
                        or die(mysql_error());

    $raw_results = mysql_query("SELECT BSTHINW3, BSTARTMASS, BSTKATNR, 
                                        BSTLIEFBST, BSTARTBES1, BSTANF, 
                                        BSTLIEFMIN, BSTSTLIO, BSTMIND,  
                                        BSTKUMSTVJ, BSTKUMVKVJ, BSTKUMERVJ, 
                                        BSTKUMST, BSTKUMVK, BSTKUMER 
                                FROM vorjahr
                                WHERE (`BSTLIEFBST` LIKE '%".$query2."%' 
                                   OR `BSTKATNR` LIKE '%".$query2."%' 
                                   OR `BSTLIEFTXT` LIKE '%".$query2."%')
                                ORDER BY BSTHINW3") 
                        or die(mysql_error());



    if(mysql_num_rows($raw_results) > 0){ 

        while($results = mysql_fetch_array($raw_results)){

            echo '<br>' ;
            echo '<br>' ;
            echo '<br>' ;
            echo "<div id='artnr'> Number: ".$results['BSTKATNR']. "</div>";
            echo "<div id='lfnr'>Nr LF / Code : ".$results['BSTLIEFBST']."</div>";

            echo '<br>' ;
            echo "<div id='3'> Desc: ".$results['BSTARTBES1']."</div>";

            echo '<br>' ;

            echo "<div id='bestandverf'>" ;
            echo "Bestand : ".$results['BSTANF']."";
            echo '<br>' ;
            echo "Verf&uuml;gbar : ".$results['BSTSTLIO']."";
            echo "</div>" ;

            echo "<div id='minbes'>" ;
            echo "Mindestbestellung : ".$results['BSTLIEFMIN']."";
            echo '<br>' ;
            echo "Mindestbestand : ".$results['BSTMIND']."";
            echo "</div>" ;

            echo "<div id='verkakt'>" ;
            echo "Current Year ".$results['BSTARTMASS']."";
            echo "</div>" ;             

            echo '<br>';
            echo "<div id='aktjahr'>" ;
            echo " Quantity : ".$results['BSTKUMST']."";
            echo '<br>' ;
            echo " Sales : ".$results['BSTKUMVK']." &euro;";
            echo '<br>' ;
            echo " Income : ".$results['BSTKUMER']." &euro;";
            echo "</div>" ;

            echo '<br>';

            echo "<div id='verkvj'>" ;
            echo " Last Year ".$results['BSTARTMASS']."";
            echo "</div>" ;

            echo "<div id='vj'>" ;
            echo "Quantity : ".$results['BSTKUMSTVJ']."";
            echo '<br>';
            echo "Sales : ".$results['BSTKUMVKVJ']." &euro;";
            echo '<br>';
            echo "Income : ".$results['BSTKUMERVJ']." &euro;";
            echo "</div>" ;

            echo '<br>';
            echo '<br>';
            echo '<br>';

            echo "<div id='artakt'>" ;
            echo "Active : ".$results['BSTHINW3']."";
            echo "</div>" ;                 



        }

    }

    else{ 
        echo "Keine Ergebnisse";
    }

}
else{ 
    echo " Minimum length : ".$min_length;
}
?>
</body>
</html>

I hope someone understands what Im trying to do and is able to help me out. Thanks in advance!

RiggsFolly
  • 93,638
  • 21
  • 103
  • 149
Exelion
  • 27
  • 4
  • 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 07 '17 at 20:14
  • So add that criteria to your WHERE clause. Its a long way from rocket science – RiggsFolly Jan 07 '17 at 20:16
  • Why have you coded `$raw_results = .....` twice everywhere??? – RiggsFolly Jan 07 '17 at 20:21
  • Because, as i said, im new to php and need to read from 2 different tables. I found no other way to do it properly :( But it works like that... – Exelion Jan 07 '17 at 20:23
  • No it does not. The second execution of the query overwrites the first. So the first `mysql_query()` it totally destroyed by the second. – RiggsFolly Jan 07 '17 at 20:24
  • You need to hit a lot of books and few tutorials and of corse the manual so you have at least a minimal understanding of what you are doing – RiggsFolly Jan 07 '17 at 20:25
  • yea, you are right. It doesn't work the way i thought it would.. I just have no idea how to do it then... thanks for the help.. – Exelion Jan 07 '17 at 20:33

0 Answers0