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

}  

   

...zur Antwort

Hallo, du musst hierzu eine neue conf.inc.php file generieren die so aussehen kann:

<?php

 try

 {

   global $con;

   $server = 'localhost:3307';

   $user = 'root';

   $pwd = '';

   $schema = '';

   $con = new PDO('mysql:host='.$server.';schema = '.$schema.';charset=utf8', $user,$pwd);

   $con->setAttribute(PDO::ATTR_ERRMODE,PDO::ERRMODE_EXCEPTION);

}

catch(Exception $e)

{

 echo $e->getCode().': '.$e->getMessage();

}

Zu beachten hierbei ist, das du dein Schema hier noch in den Parameter gibst.

Ebenfalls kann ich dir eine functions file zu herzen legen, in welcher du deine Funktionen rein gibst, ich schicke hierzu noch eine funciton php mit einigen Grundfunktionen mit, wie das ausgeben einer Tabelle:

<?php

function getSite($site)

{

  if(isset($_GET['site']))

  {

    include_once('site/'.$_GET['site'].'.php');

  }else {

    include_once('site/'.$site.'.php');

  }

}

 

function makeStatement ($query, $array = null)

{

  try

  {

    global $con;

    $stmt = $con->prepare($query);

    $stmt->execute($array);

    return $stmt;

  }

  catch (Exception $e)

  {

    return $e;

  }

}

function showDatabases($query, $array = null)

{

  global $con;

  $stmt = makeStatement($query, $array);

  if ($stmt instanceof Exception)

  {

      echo $stmt->getCode().': '.$stmt->getmessage();

  }

  else

  {

    //Tabelle erstellen

    $meta = array();

 

    //Spaltenüberschrift dynamisch

    echo '<table class="table">

       <tr>';

    for($i = 0; $i < $stmt->columnCount(); $i++)

    {

      $meta[] = $stmt->getColumnMeta($i);

      echo '<td>'.$meta[$i]['name'].'</td>';

    }

    echo '</tr>';

    /*zugriff auf arrayelemente

    FETCH_ASSOC: $row['id']

    FETCH_NUM: $row[0]

    */

    while($row = $stmt->fetch(PDO::FETCH_ASSOC))

    {

      echo'<tr>';

      foreach($row as $r)

      {

        echo '<form method = "post">';

        echo '<td>'.$r.'</td>';

        echo '<td>';

        

          echo '<div class="form-group">

              <button type="submit" name="save" class="btn btn-secondary">Auswahl</button>';

          echo '<input type="hidden" id="selectedSchema" name="selectedSchema" value='.$r.'>';

          echo '</div>';

        echo '</td>';

        echo '</form>';

      }

      echo'</tr>';

    }

    echo '</table>';

  }

}

 

function showTables($query, $array = null, $schema = null)

{

  global $con;

  $stmt = makeStatement($query, $array);

  if ($stmt instanceof Exception)

  {

      echo $stmt->getCode().': '.$stmt->getmessage();

  }

  else

  {

    //Tabelle erstellen

    $meta = array();

 

    //Spaltenüberschrift dynamisch

    echo '<table class="table">

       <tr>';

    for($i = 0; $i < $stmt->columnCount(); $i++)

    {

      $meta[] = $stmt->getColumnMeta($i);

      echo '<td>'.$meta[$i]['name'].'</td>';

    }

    echo '</tr>';

    /*zugriff auf arrayelemente

    FETCH_ASSOC: $row['id']

    FETCH_NUM: $row[0]

    */

    while($row = $stmt->fetch(PDO::FETCH_ASSOC))

    {

      echo'<tr>';

      foreach($row as $r)

      {

        echo '<form method = "post">';

        echo '<td>'.$r.'</td>';

        echo '<td>';

          echo '<div class="form-group">'; /*Wenn man auf einer anderen Seite mit den Werten hier arbeiten möchte muss man diese*/

                           /*mit einer versteckten Inputfeld mitschicken und alles muss in einer Form sein*/

          echo '<button type="submit" name="savedescription" class="btn btn-secondary">Tabellenbeschreibung</button>';

          echo '<input type="hidden" id="description" name="description" value='.$r.'>';

          echo  '                              ';

          echo '<button type="submit" name="savecontents" class="btn btn-secondary">Inhalt der Tabelle</button>';

          echo '<input type="hidden" id="contents" name="contents" value='.$r.'>';

          echo '<input type="hidden" id="selectedSchema" name="selectedSchema" value='.$schema.'>';

          echo '</div>';

        echo '</td>';

        echo '</form>';

      }

      echo'</tr>';

    }

    echo '</table>';

  }

}

 

 

function showTable($query, $array = null, $schema = null, $table = null)

{

  global $con;

  $stmt = makeStatement($query, $array);

  if ($stmt instanceof Exception)

  {

      echo $stmt->getCode().': '.$stmt->getmessage();

  }

  else

  {

    //Tabelle erstellen

    $meta = array();

 

    //Spaltenüberschrift dynamisch

    echo '<table class="table">

       <tr>';

    for($i = 0; $i < $stmt->columnCount(); $i++)

    {

      $meta[] = $stmt->getColumnMeta($i);

      echo '<td>'.$meta[$i]['name'].'</td>';

    }

    echo '</tr>';

    /*zugriff auf arrayelemente

    FETCH_ASSOC: $row['id']

    FETCH_NUM: $row[0]

    */

    while($row = $stmt->fetch(PDO::FETCH_ASSOC))

    {

      echo'<tr>';

      foreach($row as $r)

      {

        echo '<form method = "post">';

        echo '<td>'.$r.'</td>';

        echo '<td>';

          echo '<div class="form-group">';

          echo '<input type="hidden" id="description" name="description" value='.$r.'>';

          echo '<input type="hidden" id="contents" name="contents" value='.$r.'>';

          echo '<input type="hidden" id="selectedSchema" name="selectedSchema" value='.$schema.'>';

          echo '<input type="hidden" id="table" name="table" value='.$table.'>';

          echo '</div>';

        echo '</td>';

        echo '</form>';

      }

      echo'</tr>';

    }

    echo '</table>';

  }

}

Außerdem musst du diese beiden Files dann noch inkludieren, dazu füge folgenden Code in den Body deiner Index datei ein:

<main class="container">

  <div class="bg-body-tertiary p-5 rounded">

  <?php

    include_once('function/function.php');

    include_once('function/conf.inc.php');

    getSite('home');

  ?>

  </div>

</main>

...zur Antwort