PHP select Tag auswerten und Datenbankabfrage

katerchen1982

Grünschnabel
Erstellen Sie ein PHP-Skript, das für eine ausgewählte Region für jedes Land den
Namen, die Fläche, das durchschnittliche BIP, die durchschnittliche Population und
den Kontinent aus der Datenbank nation anzeigt. Die Auswahl der Region soll über
ein select-Element erfolgen, das Sie dynamisch aus der Tabelle regions generieren

Dies ist meine Lösungsansatz:

PHP:
<!DOCTYPE HTML>
<html lang="de">
<head>
    <meta charset="utf-8">
    <title>Länderdaten nach Regionen</title>
<?php
try {
    $pdo = new PDO ( 'mysql:dbname=nation;charset=utf8', 'root', '' );
    //$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    //$pdo->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
} catch ( PDOException $e ) {
    die ( $e->getMessage () );
}
?>
</head>
<body>
<form method="post">
<?php
    $sql = "SELECT name FROM regions ORDER BY name";
    $stmt = $pdo -> prepare($sql);
    $stmt -> execute();
    //print_r($stmt->fetchAll(PDO::FETCH_ASSOC));
    echo "<select name ='konti' id = 'konti'>";
    foreach ($stmt->fetchAll(PDO::FETCH_COLUMN,0) as $daten)
        echo "<option value = $daten>".$daten."</option>";
    echo "</select>";
    if(isset($_POST["konti"])){
        print_r($_POST["konti"]);
        $auswahl = "SELECT countries.name, countries.area, country_stats.gdp AS 'AVG BIP', country_stats.population, continents.name FROM
                    countries, country_stats, regions INNER JOIN continents ON regions.continent_id = continents.continent_id

                    WHERE regions.region_id = countries.region_id && countries.country_id = country_stats.country_id AND regions.name =". $_POST['konti']." GROUP BY countries.country_id";
        $stmt = $pdo -> prepare($auswahl);
       $ausgabe[] = $stmt -> execute();
       foreach ($ausgabe as $ausgeben)
       echo $ausgeben;
    }
?>
<br>
<input type = "submit" value ="anzeigen">
</form>   
    </body>
</html>

So sieht die Datenbank aus:
Bild_2022-02-05_171454.png

Mein Problem: L�nderdaten nach Regionen Fatal error: Uncaught PDOException: SQLSTATE[42S22]: Column not found: 1054 Unknown column 'Baltic' in 'where clause' in D:\xampp\htdocs\YSQL08D-XX01\nation.php:38 Stack trace: #0 D:\xampp\htdocs\YSQL08D-XX01\nation.php(38): PDOStatement->execute() #1 {main} thrown in D:\xampp\htdocs\YSQL08D-XX01\nation.php on line 38
Kann mir bitte jemand helfen den Fehler zu finden?

Viele Dank im vorraus.

LG
 
Zuletzt bearbeitet von einem Moderator:
habe mir jetzt nur den Fehlercode angeschaut, aber sieht so aus als würde die Spalte "Baltic" in "where clause" fehlen.
 
SQL:
 WHERE regions.region_id = countries.region_id && countries.country_id = country_stats.country_id AND regions.name =". $_POST['konti']." GROUP BY countries.country_id";
Er übergibt 'Konti' "as is", woraus dann "..... regions.name = Baltic ..." entsteht.
Entweder Apostrophen herum setzen
SQL:
.... regions.name ='". $_POST['konti']."' GROUP BY...
Oder wie Scatello hingewiesen hat, wie man Parameter benutzt
 
SQL:
 WHERE regions.region_id = countries.region_id && countries.country_id = country_stats.country_id AND regions.name =". $_POST['konti']." GROUP BY countries.country_id";
Er übergibt 'Konti' "as is", woraus dann "..... regions.name = Baltic ..." entsteht.
Entweder Apostrophen herum setzen
SQL:
.... regions.name ='". $_POST['konti']."' GROUP BY...
Oder wie Scatello hingewiesen hat, wie man Parameter benutzt
Danke für die Antwort. Ich werde es mal ausprobieren
 
Zurück