Servus, die Frage ist zwar sehr vage gestellt, jedoch kann ich dir hier ein Beispiel einer Übung welche ich vor kurzem gemacht habe einfügen:
<?php
echo '<h1>Ort hinzufügen</h1>';
makeTabelForAddress();
if(isset($_POST['save']))
{
//insert
$locname = $_POST['locationName'];
$plz = $_POST['plz'];
if ($locname != null and $plz != null)
{
$query = 'select ort_id from ort where ort_name = (?)';
$array = array($locname);
$stmt = makeStatement($query,$array);
if($stmt->rowcount() > 0)
{
echo $locname.' ist bereits vorhanden. Es können nur neue Orte hinzugefügt werden.';
}
else
{
$query = 'INSERT INTO ort (ort_name) VALUES (?)';
$stmt = makeStatement($query, $array);
if ($stmt instanceof Exception) {
echo '<h2>'.$locname.' kann nicht gespeichert werden!</h2>'. $stmt->getcode().': '.$stmt->getmessage();
}
else
{
$query = 'select plz_id from ort_plz where plz_id = (?)';
$array = array($plz);
$stmt = makeStatement($query,$array);
if($stmt->rowcount() > 0)
{
$query = 'select ort_id from ort where ort_name = (?)';
$array = array($locname);
$stmt = makeStatement($query, $array);
$row = $stmt -> fetch(PDO::FETCH_ASSOC);
$locid = $row['ort_id'];
$query = 'update ort_plz set ort_id = ? where plz_id = ?';
$array = array($locid,$plz);
$stmt = makeStatement($query,$array);
if ($stmt instanceof Exception) {
echo '<h2>'.$locname.' kann nicht gespeichert werden!</h2>'. $stmt->getcode().': '.$stmt->getmessage();
}
else{
echo '<h1>Ort zur PLZ angepasst</h1>';
}
}
else
{
$query = 'select ort_id from ort where ort_name = (?)';
$array = array($locname);
$stmt = makeStatement($query, $array);
$row = $stmt->fetch(PDO::FETCH_ASSOC);
$locid = $row['ort_id'];
/* $query = 'select plz_id from plz where plz_nr = (?)';
$array = array($plz);
$stmt = makeStatement($query,$array);
$row = $stmt->fetch(PDO::FETCH_ASSOC);
$plzid = $row['plz_id']; */
$query = 'INSERT INTO ort_plz (ort_id,plz_id) VALUES (?,?)';
$array = array($locid,$plz);
$stmt = makeStatement($query, $array);
if ($stmt instanceof Exception) {
echo '<h2>'.$locname.' kann nicht gespeichert werden!</h2>'. $stmt->getcode().': '.$stmt->getmessage();
}
else{
echo '<h1>Ort und PLZ gespeichert</h1>';
}
}
}
}
}
else
{
echo '<h2>Ort und PLZ müssen eingetragen sein</h2>';
}
}
else
{
?>
<form data-sb-form-api-token="API_TOKEN" method = "post">
<div class = "form-floating mb-3">
<input class="form-control" id="locationName" name = 'locationName' type = "text"
placeholder="location" data-sb-validations="required,email" />
<label for="locationName">Orts/Stadtname</label>
</div>
<?php
$sql = "Select plz_id,plz_nr FROM plz";
$result = makeStatement($sql);
// Dropdown-Menü erstellen
echo '<select name="plz" id="plz" >';
while ($row = $result->fetch(PDO::FETCH_ASSOC)) {
echo '<option value="' . $row['plz_id'] . '">' . $row['plz_nr'] . '</option>';
}
echo '</select>';
?>
<div class="d-grid">
<input class = "btn btn-primary btn-lg" name = "save" type = "submit" value = "speichern">
</div>
</form>
<?php
}