Selectbox befüllen mit Werten aus 2 DB-Tabellen

mr-otin

Mitglied
Guten Tag,

Folgende Situation. Ich lese Daten aus einer DB in ein Formular ein.

PHP:
if (isset($_POST["btn_edit"])){
$retoure_id = $con->real_escape_string(filter_input(INPUT_POST, 'btn_edit', FILTER_VALIDATE_INT));
$sqlQuery = "SELECT
                             retoure_id,
                             eingangsdatum AS Eingangsdatum,
                             menge AS Menge,
                             bezeichnung AS Bezeichnung,
                            dot AS DOT,
                            ean AS EAN,
                            demo,
                            gefahren,
                            bestellnummer AS Bestellnummer,
                            kdname AS Kundenname,
                            kdland AS Kundenland,
                            kdplz AS KundenPLZ,
                            kdort AS KundenOrt,
                            ma_id,
                            CONCAT( m.nachname, ', ', m.vorname ) AS Lagermitarbeiter,
                            bemerkungLager AS Bemerkung,
                            retouren_retoure.grund_id,
                            retouren_grund.grund AS Retourengrund,
                            retouren_status.status_id AS Status,
                            retouren_status.status
                         FROM
                             retouren_retoure
                             LEFT JOIN ma AS m ON ma_id = m.id
                            LEFT JOIN retouren_grund ON retouren_retoure.grund_id = retouren_grund.grund_id
                            LEFT JOIN retouren_status ON retouren_retoure.status_id = retouren_status.status_id
                         WHERE
                             retoure_id = $retoure_id";
                          
                $result = sendSqlQuery($con,$sqlQuery);
                 echo createEditForm($result);

Die EditForm sieht folgendermaßen aus

PHP:
function createEditForm(&$ref_sqlResult){
        $data = $ref_sqlResult->fetch_assoc();
        $col = $ref_sqlResult->fetch_fields();
        //echo "<pre>";
        //print_r($col);
        //echo "</pre>";
        $editForm = '<form action=" '. htmlspecialchars($_SERVER["PHP_SELF"]) . ' " method="post" class="form-horizontal">';

            $editForm .= '<div class = "form-group row">';
            $editForm .= '<label for="' . htmlspecialchars(ucfirst($col[1]->name)) .'" class="col-lg-2 control-label">'. htmlspecialchars(ucfirst($col[1]->name)) .'</label>';
            $editForm .= '<div class="col-lg-2">';
            $editForm .= '<input type="date" class="form-control" id="'. htmlspecialchars($col[1]->name ).'" name="'. htmlspecialchars($col[1]->name) .'" value="'. htmlspecialchars($data[$col[1]->name]) .'">';
            $editForm .= '</div>';
            $editForm .= '</div>';
          
            $editForm .= '<div class = "form-group row">';
            $editForm .= '<label for="' . htmlspecialchars(ucfirst($col[2]->name)) .'" class="col-lg-2 control-label">'. htmlspecialchars(ucfirst($col[2]->name)) .'</label>';
            $editForm .= '<div class="col-lg-2">';
            $editForm .= '<input class="form-control" id="'. htmlspecialchars($col[2]->name) .'" name="'. htmlspecialchars($col[2]->name) .'" value="'. htmlspecialchars($data[$col[2]->name]) .'">';
            $editForm .= '</div>';
            $editForm .= '</div>';
          
            $editForm .= '<div class = "form-group row">';
            $editForm .= '<label for="' . htmlspecialchars(ucfirst($col[3]->name)) .'" class="col-lg-2 control-label">'. htmlspecialchars(ucfirst($col[3]->name)) .'</label>';
            $editForm .= '<div class="col-lg-2">';
            $editForm .= '<input class="form-control" id="'. htmlspecialchars($col[3]->name) .'" name="'. htmlspecialchars($col[3]->name) .'" value="'. htmlspecialchars($data[$col[3]->name]) .'">';
            $editForm .= '</div>';
            $editForm .= '</div>';
          
            $editForm .= '<div class = "form-group row">';
            $editForm .= '<label for="' . htmlspecialchars(ucfirst($col[4]->name)) .'" class="col-lg-2 control-label">'. htmlspecialchars(ucfirst($col[4]->name)) .'</label>';
            $editForm .= '<div class="col-lg-2">';
            $editForm .= '<input class="form-control" id="'. htmlspecialchars($col[4]->name) .'" name="'. htmlspecialchars($col[4]->name) .'" value="'. htmlspecialchars($data[$col[4]->name]) .'">';
            $editForm .= '</div>';
            $editForm .= '</div>';
          
            $editForm .= '<div class = "form-group row">';
            $editForm .= '<label for="' . htmlspecialchars(ucfirst($col[5]->name)) .'" class="col-lg-2 control-label">'. htmlspecialchars(ucfirst($col[5]->name)) .'</label>';
            $editForm .= '<div class="col-lg-2">';
            $editForm .= '<input class="form-control" id="'. htmlspecialchars($col[5]->name) .'" name="'. htmlspecialchars($col[5]->name) .'" value="'. htmlspecialchars($data[$col[5]->name]) .'">';
            $editForm .= '</div>';
            $editForm .= '</div>';
          
            $editForm .= '<div class="form-group row">';
            $editForm .= '<label class="col-lg-2 control-label" for="check">Zustand:</label>';
            $editForm .= '<div class="col-lg-2 controls">';
            $editForm .= '<label class="checkbox" for="check-0"><input type="checkbox" name="check" id="check-0" value="Demo" '. htmlspecialchars($data[$col[6]->name]) .'>Demo</label>';
            $editForm .= '<label class="checkbox" for="check-1"><input type="checkbox" name="check1" id="check-1" value="gefahren" '. htmlspecialchars($data[$col[7]->name]) .'>gefahren</label>';
            $editForm .= '</div>';
            $editForm .= '</div>';
          
            $editForm .= '<div class = "form-group row">';
            $editForm .= '<label for="' . htmlspecialchars(ucfirst($col[8]->name)) .'" class="col-lg-2 control-label">'. htmlspecialchars(ucfirst($col[8]->name)) .'</label>';
            $editForm .= '<div class="col-lg-2">';
            $editForm .= '<input class="form-control" id="'. htmlspecialchars($col[8]->name) .'" name="'. htmlspecialchars($col[8]->name) .'" value="'. htmlspecialchars($data[$col[8]->name]) .'">';
            $editForm .= '</div>';
            $editForm .= '</div>';
          
            $editForm .= '<div class = "form-group row">';
            $editForm .= '<label for="' . htmlspecialchars(ucfirst($col[9]->name)) .'" class="col-lg-2 control-label">'. htmlspecialchars(ucfirst($col[9]->name)) .'</label>';
            $editForm .= '<div class="col-lg-2">';
            $editForm .= '<input class="form-control" id="'. htmlspecialchars($col[9]->name) .'" name="'. htmlspecialchars($col[9]->name) .'" value="'. htmlspecialchars($data[$col[9]->name]) .'">';
            $editForm .= '</div>';
            $editForm .= '</div>';
          
            $editForm .= '<div class = "form-group row">';
            $editForm .= '<label for="' . htmlspecialchars(ucfirst($col[10]->name)) .'" class="col-lg-2 control-label">'. htmlspecialchars(ucfirst($col[10]->name)) .'</label>';
            $editForm .= '<div class="col-lg-2">';
            $editForm .= '<input class="form-control" id="'. htmlspecialchars($col[10]->name) .'" name="'. htmlspecialchars($col[10]->name) .'" value="'. htmlspecialchars($data[$col[10]->name]) .'">';
            $editForm .= '</div>';
            $editForm .= '</div>';
          
            $editForm .= '<div class = "form-group row">';
            $editForm .= '<label for="' . htmlspecialchars(ucfirst($col[11]->name)) .'" class="col-lg-2 control-label">'. htmlspecialchars(ucfirst($col[11]->name)) .'</label>';
            $editForm .= '<div class="col-lg-2">';
            $editForm .= '<input class="form-control" id="'. htmlspecialchars($col[11]->name) .'" name="'. htmlspecialchars($col[11]->name) .'" value="'. htmlspecialchars($data[$col[11]->name]) .'">';
            $editForm .= '</div>';
            $editForm .= '</div>';
          
            $editForm .= '<div class = "form-group row">';
            $editForm .= '<label for="' . htmlspecialchars(ucfirst($col[12]->name)) .'" class="col-lg-2 control-label">'. htmlspecialchars(ucfirst($col[12]->name)) .'</label>';
            $editForm .= '<div class="col-lg-2">';
            $editForm .= '<input class="form-control" id="'. htmlspecialchars($col[12]->name) .'" name="'. htmlspecialchars($col[12]->name).'" value="'. htmlspecialchars($data[$col[12]->name]) .'">';
            $editForm .= '</div>';
            $editForm .= '</div>';
          
            //Retourengrund SELECT-Box
            $editForm .= '<div class = "form-group row">';
            $editForm .= '<label for="' . htmlspecialchars(ucfirst($col[17]->name)) .'" class="col-lg-2 control-label">'. htmlspecialchars(ucfirst($col[17]->name)) .'</label>';
            $editForm .= '<div class="col-lg-3">';
            $editForm .= '<select class="form-control" name="'. htmlspecialchars($col[17]->name) .'" id="'. htmlspecialchars($col[16]->name) .'">';
            $editForm .= '<option value="'. $data[$col[17]->name] .'">'. $data[$col[17]->name] .'</option>';
            $editForm .= '</select>';
            //$editForm .= '<input class="form-control" id="'. htmlspecialchars($col[15]->name) .'" name="'. htmlspecialchars($col[15]->name) .'" value="'. htmlspecialchars($data[$col[15]->name]) .'">';
            $editForm .= '</div>';
            $editForm .= '</div>';
          
            //Lagermitarbeiter SELECT-Box
            $editForm .= '<div class = "form-group row">';
            $editForm .= '<label for="' . htmlspecialchars(ucfirst($col[14]->name)) .'" class="col-lg-2 control-label">'. htmlspecialchars(ucfirst($col[14]->name)) .'</label>';
            $editForm .= '<div class="col-lg-3">';
            $editForm .= '<select class="form-control" name="'. htmlspecialchars($col[13]->name) .'" id="'. htmlspecialchars($col[14]->name) .'">';
            $editForm .= '<option value="'. $data[$col[13]->name] .'">'. $data[$col[14]->name] .'</option>';
            $editForm .= '</select>';
            $editForm .= '</div>';
            $editForm .= '</div>';
          
            $editForm .= '<div class="form-group row">';
            $editForm .= '<label for="' . htmlspecialchars(ucfirst($col[15]->name)) .'" class="col-lg-2 control-label">'. htmlspecialchars(ucfirst($col[15]->name)) .'</label>';
            $editForm .= '<div class="col-lg-3">';
         $editForm .= '<textarea class="form-control" rows="3" name='. htmlspecialchars($col[15]->name) .'>'. htmlspecialchars($data[$col[15]->name]) .'</textarea>';
            $editForm .= '</div>';
            $editForm .= '</div>';
          
            $editForm .= '<div class="form-group row">';
            $editForm .= '<label for="' . htmlspecialchars(ucfirst($col[18]->name)) .'" class="col-lg-2 control-label">'. htmlspecialchars(ucfirst($col[18]->name)) .'</label>';
            $editForm .= '<div class="col-lg-3">';
        $editForm .= '<select class="form-control" name="'. htmlspecialchars($col[18]->name) .'" id="'. htmlspecialchars($col[18]->name) .'">';
            $editForm .= '<option value="'. $data[$col[18]->name] .'">'. $data[$col[19]->name] .'</option>';
            $editForm .= '</select>';
            $editForm .= '</div>';
            $editForm .= '</div>';
          
            $editForm .= '<div class="form-group row">';
            $editForm .= '<button type="submit" class="btn btn-success" name="btn_update" value="'. htmlspecialchars($data[$col[0]->name]) .'">Änderung speichern</button>';
            $editForm .= '</div>';
      
    $editForm .= '</form>';
      
        return $editForm;
    }

Im Moment wird jeweils nur 1 Option angezeigt in der Selectbox, da durch die abfrage bedingt nur die vorhandene Col ausgewählt ist. Wie schaffe ich es bspw bei der Selectbox-Mitarbeiter alle Mitarbeiter anzuzeigen und den Mitarbeiter mit der ID der im ersten SQLQuery aufgerufen wird im option-feld zu selektieren.

Meines erachtens muss dazu ein 2tes Qquery erstellt werden und die ID's verglichen werden. Nur dann wird meine EditForm zu unfelxibel wegen dem Query den ich dort direktreinschreiben muss. Wie würdet ihr das lösen?

MfG
Dave
 

Anhänge

  • selecktbox.JPG
    selecktbox.JPG
    12,1 KB · Aufrufe: 7
Zuletzt bearbeitet:
Zurück