mardi 12 janvier 2021

PDO Insert of static list with check boxes

Situation: I have a static list with check boxes and want to give the user the possibility to choose all or some items of the list. Based on the selection of the user i want to add one or more rows into my SQL table.

My try: I have made a SQL INSERT Query. I have problems to create a loop. With my code it will only insert one row of the last chosen item. Obviously i need to handle my request with a loop. Here my knowledge ends and i need some support. I have nothing found during my search which is fitting to my demands.

(For readability i have removed some information)

My PDO insert query:

// Insert prepared PDO
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
    $file_titel = trim($_POST['file_title']);
    $audit_id = 2;

    // PDO Query um die Daten in die Tabelle zu schreiben
    $statement = $pdo->prepare("INSERT INTO file_request(audit_id, file_titel) VALUES (:audit_id, :file_titel)");
    $result = $statement->execute(array('audit_id' => $audit_id, 'file_titel' => $file_titel));

    if ($result) {
        // Wenn der Tabelleneintrag erfolgreich war
        $success_msg = "Wurde gesendet";
    } else {
        // Wenn das nicht geklappt hat
        $warning_msg = "Nicht gesendet";
    }
} else {
}

My HTML List:

<!-- 1st item to select -->
<input type="checkbox" class="custom-control-input" name="file_titel" id="customCheck1" value="1" checked>
<label class="custom-control-label " for="customCheck1"> &nbsp; Sometext</label>
<input type="hidden" name="file_titel" value="somefiletitel">

<!-- 2nd item to select -->
<input type="checkbox" class="custom-control-input" name="file_titel" id="customCheck2" value="1" checked>
<label class="custom-control-label " for="customCheck2"> &nbsp; Sometext </label>
<input type="hidden" name="file_titel" value="somefiletitel">

<!-- some more items will follow with the same method -->

As you can see, i would like to choose one or more items with the checkbox. After submit the PSO Insert Into Query shall add a single row in the table for every selected item and will pick the filename from the hidden input.




Aucun commentaire:

Enregistrer un commentaire