Durchsuchen von einer Datenbank nach mehreren Faktoren.

icke79

Mitglied
Hallo,
Ich habe ein kleines Problem. Ich habe den Code geschrieben, wie ihr ihn unten seht.
Ich kann nach den Einträgen suchen, wenn ich mehrere Suchkriterien richtig eingebe (mind. mz und ID). Das liegt vermutlich am "and"... wie kann ich es hin bekommen, dass ich nach einer Eingabe suche und die Auswahl reduziert wird, wenn ich weitere Faktroren hinzuziehe.
Sprich, ich suche nach "mz" und habe 5 einträge. Danach suche ich nach "mz" und "Name" und habe danach nur noch 3 Einträge. (Ich hoffe ich habe es gut genug erklärt).
Danke für die Hilfe.

HTML:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">

<head>
<meta content="de" http-equiv="Content-Language" />
<meta content="text/html; charset=utf-8" http-equiv="Content-Type" />
<title>Search Mask</title>
<style type="text/css">
.auto-style1 {
    text-align: center;
}
.auto-style2 {
    text-align: center;
    font-size: xx-large;
}
.auto-style3 {
    font-size: medium;
}
.auto-style4 {
    text-align: center;
    font-size: medium;
}
.auto-style5 {
    font-size: xx-large;
    font-weight: bold;
}
.auto-style7 {
    border: 0px solid #000000;
}
.auto-style22 {
    border-style: solid;
    border-width: 1px;
}
.auto-style23 {
    text-align: left;
}
.auto-style24 {
    margin-bottom: 0px;
}
</style>
</head>

<body bgcolor="#D8D8D8">

<form action="test_anzeige_DB.php" method="POST"

<div class="auto-style1">
<div class="auto-style1">
    <strong><span class="auto-style5">Search Mask</span></strong> </div>
        <br />
<table style="width: 100%" class="auto-style24">
    <tr>
        <td style="width: 229px; height: 24px;" class="auto-style4">Name</td>
        <td style="width: 80px; height: 24px;" class="auto-style4">mz</td>
        <td style="width: 80px; height: 24px;" class="auto-style4">+/- m/z</td>
        <td style="width: 80px; height: 24px;" class="auto-style4">RRT</td>
        <td style="width: 80px; height: 24px;" class="auto-style4">+/- RRT</td>
        <td style="width: 130px; height: 24px;" class="auto-style4">Retention Time</td>
        <td style="width: 130px; height: 24px;" class="auto-style4">+/- Retention Time</td>
    </tr>
    <tr>
        <td style="width: 229px" class="auto-style1">
        <input style="width: 229px" class="auto-style3" name="mol"/></td>
        <td style="width: 80px" class="auto-style1">
        <p><input style="width: 80px" class="auto-style3" name="mz"/></p></td>
        <td style="width: 80px" class="auto-style1">
        <input style="width: 80px" class="auto-style3" name="mzvar"/></td>
        <td style="width: 80px" class="auto-style1">
        <input style="width: 80px" class="auto-style3" name="rrt"/></td>
        <td style="width: 80px" class="auto-style1">
        <input style="width: 80px" class="auto-style3" name="rrtv"/></td>
        <td style="width: 130px" class="auto-style1">
        <input style="width: 80px; height: 24px;" class="auto-style3" name="retention_time"/></td>
        <td style="width: 130px" class="auto-style1">
        <input style="width: 80px" class="auto-style3" name="retention_Time_var"/></td>
    </tr>
</table>

<p class="auto-style23"><span class="auto-style3">
        &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
        &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
        &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
        &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
        &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
        &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
        &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
        &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
</span><input checked="checked" name="aducts" type="checkbox" value="true" class="auto-style3" /><span class="auto-style3">
Check for aduct masses</span></p>
<table style="width: 100%">
    <tr>
        <td style="width: 296px" class="auto-style4">molecular formula</td>
        <td style="width: 296px" class="auto-style4">CAS-number</td>
        <td style="width: 296px" class="auto-style4">ID</td>
        <td style="width: 296px" class="auto-style4">comment</td>
    </tr>
    <tr>
        <td style="width: 296px; height: 28px;" class="auto-style1">
        <input style="width: 296px" class="auto-style3" name="molecular_formula"/></td>
        <td style="width: 296px; height: 28px;" class="auto-style1">
        <input style="width: 296px" class="auto-style3" name="casn"/></td>
        <td style="width: 296px; height: 28px;" class="auto-style1">
        <input style="width: 296px" class="auto-style3" name="id"/></td>
        <td style="width: 296px; height: 28px;" class="auto-style1">
        <input style="width: 296px" class="auto-style3" name="comment"/></td>
    </tr>
</table>
    <br />
<div class="auto-style1">
<input name="HS" type="checkbox" value="true" class="auto-style3" /><span class="auto-style3">HS-GC-MS
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
    </span>
<input name="GC" type="checkbox" value="true" class="auto-style3" /><span class="auto-style3">GG-FID/MS
    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
    </span>
<input name="Siloxane" type="checkbox" value="true" class="auto-style3" /><span class="auto-style3">GC-Siloxane
    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
    </span>
<input name="LC" type="checkbox" value="true" class="auto-style3" /><span class="auto-style3">LC-MS
    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
    </span>
<input name="polare" type="checkbox" value="true" class="auto-style3" style="height: 20px" /><span class="auto-style3">LC-polare Substanzen
    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
    </span>
<input name="phthalate" type="checkbox" value="true" class="auto-style3" /><span class="auto-style3">LC-Phthalate
    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
    </span>
<input name="protein" type="checkbox" value="true" class="auto-style3" /><span class="auto-style3">LC-Proteinmethode</span>
</div>
<p>    <div class="auto-style1">
    <input name="End" type="button" value="end" />
    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
<input name="save" type="button" value="search" /></div>
    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
    <input type="submit"/>
</p>
<p class="auto-style2">Results</p>
<br />

    <table style="width: 100%" class="auto-style7" align="center">
        <tr>
            <td class="auto-style22" style="width: 200px; height: 44px;">Name</td>
            <td class="auto-style22" style="width: 50px; height: 44px;">RRT</td>
            <td class="auto-style22" style="width: 70px; height: 44px;">Retention Time</td>
            <td class="auto-style22" style="width: 76px; height: 44px;">molecular weight</td>
            <td class="auto-style22" style="width: 50px; height: 44px;">m/z1</td>
            <td class="auto-style22" style="width: 50px; height: 44px;">m/z2</td>
            <td class="auto-style22" style="width: 50px; height: 44px;">m/z3</td>
            <td class="auto-style22" style="width: 51px; height: 44px;">m/z4</td>
            <td class="auto-style22" style="width: 50px; height: 44px;">m/z5</td>
            <td class="auto-style22" style="width: 50px; height: 44px;">m/z6</td>
            <td class="auto-style22" style="width: 50px; height: 44px;">m/z7</td>
            <td class="auto-style22" style="width: 107px; height: 44px;">molecular formula</td>
            <td class="auto-style22" style="width: 99px; height: 44px;">CAS-number</td>
            <td class="auto-style22" style="width: 50px; height: 44px;">ID</td>
            <td class="auto-style22" style="width: 80px; height: 44px;">method</td>
            <td class="auto-style22" style="height: 44px">comment</td>
            <td class="auto-style22" style="width: 50px; height: 44px;">spectra</td>

        </tr>
            <tr>
            <td class="auto-style22" style="width: 200px">&nbsp;</td>
            <td class="auto-style22" style="width: 50px">&nbsp;</td>
            <td class="auto-style22" style="width: 70px">&nbsp;</td>
            <td class="auto-style22" style="width: 76px">&nbsp;</td>
            <td class="auto-style22" style="width: 50px">&nbsp;</td>
            <td class="auto-style22" style="width: 50px">&nbsp;</td>
            <td class="auto-style22" style="width: 51px">&nbsp;</td>
            <td class="auto-style22" style="width: 50px">&nbsp;</td>
            <td class="auto-style22" style="width: 50px">&nbsp;</td>
            <td class="auto-style22" style="width: 50px">&nbsp;</td>
            <td class="auto-style22" style="width: 50px">&nbsp;</td>
            <td class="auto-style22" style="width: 107px">&nbsp;</td>
            <td class="auto-style22" style="width: 99px">&nbsp;</td>
            <td class="auto-style22" style="width: 50px">&nbsp;</td>
            <td class="auto-style22">&nbsp;</td>
            <td class="auto-style22">&nbsp;</td>
            <td class="auto-style22" style="width: 50px">&nbsp;</td>

        </tr>
            <tr>
            <td class="auto-style22" style="width: 200px">&nbsp;</td>
            <td class="auto-style22" style="width: 50px">&nbsp;</td>
            <td class="auto-style22" style="width: 70px">&nbsp;</td>
            <td class="auto-style22" style="width: 76px">&nbsp;</td>
            <td class="auto-style22" style="width: 50px">&nbsp;</td>
            <td class="auto-style22" style="width: 50px">&nbsp;</td>
            <td class="auto-style22" style="width: 51px">&nbsp;</td>
            <td class="auto-style22" style="width: 50px">&nbsp;</td>
            <td class="auto-style22" style="width: 50px">&nbsp;</td>
            <td class="auto-style22" style="width: 50px">&nbsp;</td>
            <td class="auto-style22" style="width: 50px">&nbsp;</td>
            <td class="auto-style22" style="width: 107px">&nbsp;</td>
            <td class="auto-style22" style="width: 99px">&nbsp;</td>
            <td class="auto-style22" style="width: 50px">&nbsp;</td>
            <td class="auto-style22">&nbsp;</td>
            <td class="auto-style22">&nbsp;</td>
            <td class="auto-style22" style="width: 50px">&nbsp;</td>

        </tr>
        <tr>
            <td class="auto-style22" style="width: 200px">&nbsp;</td>
            <td class="auto-style22" style="width: 50px">&nbsp;</td>
            <td class="auto-style22" style="width: 70px">&nbsp;</td>
            <td class="auto-style22" style="width: 76px">&nbsp;</td>
            <td class="auto-style22" style="width: 50px">&nbsp;</td>
            <td class="auto-style22" style="width: 50px">&nbsp;</td>
            <td class="auto-style22" style="width: 51px">&nbsp;</td>
            <td class="auto-style22" style="width: 50px">&nbsp;</td>
            <td class="auto-style22" style="width: 50px">&nbsp;</td>
            <td class="auto-style22" style="width: 50px">&nbsp;</td>
            <td class="auto-style22" style="width: 50px">&nbsp;</td>
            <td class="auto-style22" style="width: 107px">&nbsp;</td>
            <td class="auto-style22" style="width: 99px">&nbsp;</td>
            <td class="auto-style22" style="width: 50px">&nbsp;</td>
            <td class="auto-style22">&nbsp;</td>
            <td class="auto-style22">&nbsp;</td>
            <td class="auto-style22" style="width: 50px">&nbsp;</td>

        </tr>
                    <tr>
            <td class="auto-style22" style="width: 200px">&nbsp;</td>
            <td class="auto-style22" style="width: 50px">&nbsp;</td>
            <td class="auto-style22" style="width: 70px">&nbsp;</td>
            <td class="auto-style22" style="width: 76px">&nbsp;</td>
            <td class="auto-style22" style="width: 50px">&nbsp;</td>
            <td class="auto-style22" style="width: 50px">&nbsp;</td>
            <td class="auto-style22" style="width: 51px">&nbsp;</td>
            <td class="auto-style22" style="width: 50px">&nbsp;</td>
            <td class="auto-style22" style="width: 50px">&nbsp;</td>
            <td class="auto-style22" style="width: 50px">&nbsp;</td>
            <td class="auto-style22" style="width: 50px">&nbsp;</td>
            <td class="auto-style22" style="width: 107px">&nbsp;</td>
            <td class="auto-style22" style="width: 99px">&nbsp;</td>
            <td class="auto-style22" style="width: 50px">&nbsp;</td>
            <td class="auto-style22">&nbsp;</td>
            <td class="auto-style22">&nbsp;</td>
            <td class="auto-style22" style="width: 50px">&nbsp;</td>

        </tr>

            <tr>
            <td class="auto-style22" style="width: 200px">&nbsp;</td>
            <td class="auto-style22" style="width: 50px">&nbsp;</td>
            <td class="auto-style22" style="width: 70px">&nbsp;</td>
            <td class="auto-style22" style="width: 76px">&nbsp;</td>
            <td class="auto-style22" style="width: 50px">&nbsp;</td>
            <td class="auto-style22" style="width: 50px">&nbsp;</td>
            <td class="auto-style22" style="width: 51px">&nbsp;</td>
            <td class="auto-style22" style="width: 50px">&nbsp;</td>
            <td class="auto-style22" style="width: 50px">&nbsp;</td>
            <td class="auto-style22" style="width: 50px">&nbsp;</td>
            <td class="auto-style22" style="width: 50px">&nbsp;</td>
            <td class="auto-style22" style="width: 107px">&nbsp;</td>
            <td class="auto-style22" style="width: 99px">&nbsp;</td>
            <td class="auto-style22" style="width: 50px">&nbsp;</td>
            <td class="auto-style22">&nbsp;</td>
            <td class="auto-style22">&nbsp;</td>
            <td class="auto-style22" style="width: 50px">&nbsp;</td>

        </tr>

    </table>

<br />

<input name="Export" type="button" value="Export" />

</div>
</body>

</html>


PHP:
<!DOCTYPE html><html>

<head>
<meta content="text/html; charset=utf-8" http-equiv="Content-Type" />
<title>Untitled 1</title>
</head>

<body>

<?php
            //Definition Verbindung
        $con = mysqli_connect("localhost", "root", "PW", "ms");
       
            //Definition Variablen
        $mzx = doubleval($_POST['mz']);
        $mzv = doubleval($_POST['mzvar']);
        $iden = doubleval($_POST['id']);
        $mole = ($_POST['mol']);
        $cas = ($_POST['casn']);
        $If = 1;
       
       
            //Berechnungen
        $mze = doubleval($_POST['mz']) + doubleval($_POST['mzvar']);
        $mze2 = doubleval($_POST['mz']) - doubleval($_POST['mzvar']);
       
            //Definition Suche
        $sql =    "SELECT * FROM alkohole where
                (Name LIKE '%" . $mole . "%' AND `CAS-Number` like '%" . $cas . "%'  AND `ID` LIKE $iden AND mz1 BETWEEN $mze2 AND $mze) OR
                (Name LIKE '%" . $mole . "%' AND `CAS-Number` like '%" . $cas . "%'  AND `ID` LIKE $iden AND mz2 BETWEEN $mze2 AND $mze) OR
                (Name LIKE '%" . $mole . "%' AND `CAS-Number` like '%" . $cas . "%'  AND `ID` LIKE $iden AND mz3 BETWEEN $mze2 AND $mze) OR
                (Name LIKE '%" . $mole . "%' AND `CAS-Number` like '%" . $cas . "%'  AND `ID` LIKE $iden AND mz4 BETWEEN $mze2 AND $mze) OR
                (Name LIKE '%" . $mole . "%' AND `CAS-Number` like '%" . $cas . "%'  AND `ID` LIKE $iden AND mz5 BETWEEN $mze2 AND $mze)";              
               
           
                               
        $res = mysqli_query ($con, $sql);
        $num = mysqli_num_rows($res);
       
       
    if($num > 0)     echo "Ergebnis:<br>";
    else             echo "Kein Ergebnis:<br>";  
   
    // Tabellenbeginn
   
    echo "<table border='1'>";
   
    echo "<tr><td>ID</td><td>Name</td> </td>";
       echo "<td>CAS_Number</td> <td>m/z1</td>";
      echo "<td>m/z2</td> <td>m/z3</td> </td>";
     echo "<td>m/z4</td> <td>m/z5</td> </tr>";

   
   
   
    while ($dsatz = mysqli_fetch_assoc($res))
    {

                echo "<tr>";
                 
                echo    "<td>" .    $dsatz["ID"] . "</td>";
                echo     "<td>" .    $dsatz["Name"] . "</td>";
                echo     "<td>" .    $dsatz["CAS-Number"] . "</td>";
                echo     "<td>" .    $dsatz["mz1"] . "</td>";
                echo     "<td>" .    $dsatz["mz2"] . "</td>";
                echo     "<td>" .    $dsatz["mz3"] . "</td>";
                echo     "<td>" .    $dsatz["mz4"] . "</td>";
                echo     "<td>" .    $dsatz["mz5"] . "</td>";
                echo "</tr>";
               
}
                mysqli_close($con);
?>
</body>
</html>

Und noch eine Frage, wie bekomme ich es bei der Eingabe, für ein lehrere Eingabefeld in der Tabelle nichts eingetragen wird und keine null.. mein bisheriger Code
PHP:
<!DOCTYPE html><html><head><meta charset="utf-8">
<?php
   if (isset($_POST["gesendet"]))
   {
      $con = mysqli_connect("localhost", "root", "PW", "ms");
      $mole = $_POST["mole"];
      $casn = $_POST["casn"];
      $mz1 = doubleval($_POST["mz1"]);
      $mz2 = doubleval($_POST["mz2"]);
      $mz3 = doubleval($_POST["mz3"]);
      $mz4 = doubleval($_POST["mz4"]);
      $mz5 = doubleval($_POST["mz5"]);
      $sql = "INSERT INTO alkohole (Name, `CAS-number`,mz1, mz2, mz3, mz4, mz5)"
         . "VALUES('$mole', '$casn ', $mz1, $mz2, '$mz3', '$mz4', '$mz5')";
      mysqli_query($con, $sql);

      $num = mysqli_affected_rows($con);
      if ($num>0)
      {
         echo "<p><font color='#00aa00'>";
         echo "Ein Datensatz hinzugekommen";
         echo "</font></p>";
      }
      else
      {
         echo "<p><font color='#ff0000'>";
         echo "Es ist ein Fehler aufgetreten, ";
         echo "es ist kein Datensatz hinzugekommen";
         echo "</font></p>";
      }

      mysqli_close($con);
   }
?>
</head>
<body>
<p>Geben Sie bitte einen Datensatz ein<br>
und senden Sie das Formular ab:</p>
<form action = "Eintraege_hinzufügen.php" method = "post">
   <p><input name="mole"> Name</p>
   <p><input name="casn"> CAS-Number</p>
   <p><input name="mz1"> mz1</p>
   <p><input name="mz2"> mz2</p>
   <p><input name="mz3"> mz3</p>
   <p><input name="mz4"> mz4</p>
   <p><input name="mz5"> mz5</p>
   <p><input type="submit" name="gesendet">
   <input type="reset"></p>
</form>

<p>Alle <a href="db_tabelle.php">anzeigen</a></p>
</body></html>
 
Zuletzt bearbeitet:
Du musst deine WHERE-Klausel einfach Erweitern. :)

Da du es immer mit einem Submit übergibst kannst Du wie folgt prüfen, so würde Ich es machen

PHP:
<?php
$klausel = '';
// EXAMPLE
if(isset($_POST['senden'])) {
    if(isset($_POST['feld1'])) {
        $klausel = "WHERE ((feld1 LIKE '%{$_POST['feld1']}%')";
    }
    
    if(isset($_POST['feld2'])) {
        $klausel .= "AND (feld2 LIKE '%{$_POST['feld2']}%')";
    }

    if(isset($_POST['feld3'])) {
        $klausel .= "AND (feld3 LIKE '%{$_POST['feld3']}%')";
    }

    
    if(isset($_POST['feld4'])) {
        $klausel .= "AND (feld4 LIKE '%{$_POST['feld4']}%')";
    }

    
    if(isset($_POST['feld5'])) {
        $klausel .= "AND (feld5 LIKE '%{$_POST['feld5']}%')";
    }

    if(isset($_POST['feld6'])) {
        $klausel .= "AND (feld6 LIKE '%{$_POST['feld6']}%')";
    }

    $klausel .= ")";
}

$sql = mysqli_query("SELECT * FROM TABLE {$klausel}");
while($row = mysqli_fetch_assoc($sql)) {
    //CODE HERE
}
?>

Natürlich musst Du deine Abfrage noch anpassen, denn aktuell ist die Abfrage nicht Optimal :)
 
Noch besser. Das AND und das WHERE vorerst weglassen, alles in einen Array packen und erst am Schluss das WHERE erstellen.
Dann bist du noch flexibler.
[PHP] [MySQL] Einfacher SQL-Filter aus $_POST zusammenstellen [Yaslaw.Info]
PHP:
if([URL='http://www.php.net/isset']isset[/URL]($_POST['submit'])){
    // Array initializieren. Falls kein argument angegeben wurde, 
    // dann ist die Argumentliste ein leeres Array
    $wheres = [URL='http://www.php.net/array']array[/URL]();
    //Jedes Feld prüfen und ggf in den Where-Array einfügen
    if([URL='http://www.php.net/isset']isset[/URL]($_POST['text1'])) $wheres[] = "text1='{$_POST['text1']}'";
    if([URL='http://www.php.net/isset']isset[/URL]($_POST['text2'])) $wheres[] = "text2='{$_POST['text2']}'";
    if([URL='http://www.php.net/isset']isset[/URL]($_POST['zahl3'])) $wheres[] = "zahl3={$_POST['zahl3']}";
 
    // Wenn Bedinungen vorhanden sind, WHERE-Teil des SQL-String zusammenstellen
    if([URL='http://www.php.net/count']count[/URL]($wheres)>0) $wherestring = "WHERE ".[URL='http://www.php.net/implode']implode[/URL](' AND ', $wheres);
    //SQL-String zusammensetzen
    $sql = "SELECT * FROM mytable {$wherestring}";
    //TODO: Sql an die DB absetzen
    echo $sql;
}
 
Du musst deine WHERE-Klausel einfach Erweitern. :)

Da du es immer mit einem Submit übergibst kannst Du wie folgt prüfen, so würde Ich es machen


Natürlich musst Du deine Abfrage noch anpassen, denn aktuell ist die Abfrage nicht Optimal :)


Ich habe es jetzt mal adaptiert aber ich bekomme nen Fehler.. Ich habe mit den definierten Variablen gerechnet, da das, finde ich, übersichtlicher ist.
Das ist die Ausgabe

Warning: mysqli_query() expects at least 2 parameters, 1 given in C:\xampp\htdocs\Webpage\Test_Anzeige_DB.php on line 58

Warning: mysqli_fetch_assoc() expects parameter 1 to be mysqli_result, null given in C:\xampp\htdocs\Webpage\Test_Anzeige_DB.php on line 59

Warning: mysqli_query(): Empty query in C:\xampp\htdocs\Webpage\Test_Anzeige_DB.php on line 62

Warning: mysqli_num_rows() expects parameter 1 to be mysqli_result, boolean given in C:\xampp\htdocs\Webpage\Test_Anzeige_DB.php on line 63
Kein Ergebnis:

Warning: mysqli_fetch_assoc() expects parameter 1 to be mysqli_result, boolean given in C:\xampp\htdocs\Webpage\Test_Anzeige_DB.php on line 81
IDNameCAS_Numberm/z1m/z2m/z3m/z4m/z5


PHP:
<!DOCTYPE html><html>

<head>
<meta content="text/html; charset=utf-8" http-equiv="Content-Type" />
<title>Untitled 1</title>
</head>

<body>

<?php
            
            
            
            //Definition Verbindung
        $con = mysqli_connect("localhost", "root", "PW", "ms");
        
            //Definition Variablen
        $mzx = doubleval($_POST['mz']);
        $mzv = doubleval($_POST['mzvar']);
        $iden = doubleval($_POST['id']);
        $mole = ($_POST['mol']);
        $cas = ($_POST['casn']);
        $If = 1;
        
        
            //Berechnungen
        $mze = doubleval($_POST['mz']) + doubleval($_POST['mzvar']);
        $mze2 = doubleval($_POST['mz']) - doubleval($_POST['mzvar']);
        
        
        
        
$klausel = '';
if(isset($_POST['senden'])) {
    if(isset($_POST['mz'])) {
        $klausel = "WHERE (mz1 BETWEEN '$mze2' AND $mze or mz2 BETWEEN '$mze2' AND $mze or mz3 BETWEEN '$mze2' AND $mze or mz4 BETWEEN '$mze2' AND $mze or mz5 BETWEEN '$mze2' AND $mze)";
    }
    
    if(isset($_POST['mol'])) {
        $klausel .= "AND (`Name` LIKE '$mole')";
    }

    if(isset($_POST['id'])) {
        $klausel .= "AND (`ID` = '$iden')";
    }

    
    if(isset($_POST['casn'])) {
        $klausel .= "AND (`CAS-number` = '$cas')";
    }

    
    }

    $klausel .= ")";


$sql = mysqli_query("SELECT * FROM TABLE {$klausel}");
while($row = mysqli_fetch_assoc($sql)) {
}               
                
        $res = mysqli_query ($con, $sql);
        $num = mysqli_num_rows($res);
        
        
    if($num > 0)     echo "Ergebnis:<br>";
    else             echo "Kein Ergebnis:<br>";   
    
    // Tabellenbeginn
    
    echo "<table border='1'>";
    
    echo "<tr><td>ID</td><td>Name</td> </td>";
       echo "<td>CAS_Number</td> <td>m/z1</td>";
      echo "<td>m/z2</td> <td>m/z3</td> </td>";
     echo "<td>m/z4</td> <td>m/z5</td> </tr>";

    
    
    
    while ($dsatz = mysqli_fetch_assoc($res))
    {

                echo "<tr>";
                  
                echo    "<td>" .    $dsatz["ID"] . "</td>";
                echo     "<td>" .    $dsatz["Name"] . "</td>";
                echo     "<td>" .    $dsatz["CAS-Number"] . "</td>";
                echo     "<td>" .    $dsatz["mz1"] . "</td>";
                echo     "<td>" .    $dsatz["mz2"] . "</td>";
                echo     "<td>" .    $dsatz["mz3"] . "</td>";
                echo     "<td>" .    $dsatz["mz4"] . "</td>";
                echo     "<td>" .    $dsatz["mz5"] . "</td>";
                echo "</tr>";
                
}
                mysqli_close($con);
?>
</body>
</html>
 
Neben dem, dass du einen komischen Aufbau hast, lohnt es sich mal anzuschauen wie den sql aussieht.
Hier ein Tutorial dazu: [PHP][MySQLi] Debug Queries [Yaslaw.Info]

Hallo,

Auch dir danke für den Input. Ich habe mich für den anderen weg wie benutzt entschieden, da ich den schneller verstanden habe... oder auch nicht richtig.
Bin auch noch Anfänger, wie du sicher gemerkt hast.
Was meinst du mit komischen Aufbau?
und das SQL wird nicht ausgegeben, also dort ist nichts definiert, zumindest wenn ich versuche den Befehl über ein "echo" auszugeben.
Auf was klares wie

"UPDATE myTable SET feld_1 = test WHERE id = 6;
Unknown column 'feld_1' in 'field list'"

komme ich mit mysqli_error() nicht.

P.s.: einen dummen Fehler hab ich schon, statt "table" gehört natürlicvh "Alkohole" in die $SQL.. daran lag es aber nicht.
 
Zuletzt bearbeitet:
Gut. Hast du mein Turial weiter gelesen? Da steht auch was man machen muss, wenn keine solche Meldung kommt. Mit einem Echo das SQL ausgeben lassen. Schon versucht?
 

Neue Beiträge

Zurück