'Update table with value from selected option, php

I have site with selection i am using

enter image description here

i want to update value in the table when i choose from dropbox and press update button

<form action="post">
  <h5 class="card-title">Nájomník</h5>
  <select name="id_najomnik" id="id_najomnik">
    <?php while($r = $results->fetch(PDO::FETCH_ASSOC)){?>
    <option value="<?php echo $r['id_najomnik']; ?>" <?php if($r[ 'id_najomnik']==$ detaily[ 'id_najomnik']) echo 'selected'; ?> class="card-text">
      <?php echo $r['meno'];echo ' ' ;echo $r['priezvisko']; }?>
    </option>
  </select>
  </div>
  <div class="card-body">
    <a class="btn btn-warning" href="update.php" role="button">Prepoj</a>
    <a href="index.php" class="btn btn-primary">Späť</a>
  </div>
</form>

this is the update.php file

<?php
    require_once 'nav.php';
    require_once 'conn.php';
    $id = $_POST['id'];
    $meno = $_POST['meno'];
    $priezvisko = $_POST['priezvisko'];
    $isSuccess = $crud->updateNajomnici($id, $meno, $priezvisko);
?>

Here are the functions i am using to get results

public function getNajomnicidetaily($id){
    $sql = "SELECT id_najomnik, meno,priezvisko,id_byt,nazov_budovy,nazov_bloku,mesto,ulica,cislo,psc,podlazie,rozloha,pocet_izieb,mesacny_najom, if(balkon=1, 'Áno', 'Nie') as balkon,if(pivnica=1, 'Áno', 'Nie') as pivnica, if(garaz=1, 'Áno', 'Nie') as garaz FROM `detail` where id_byt= :id";
    $stmt = $this->db->prepare($sql);
    $stmt->bindparam(':id', $id);     
    $stmt->execute();  
    $result = $stmt->fetch();
    return $result;
}

public function getNajomniciudaje()
{
    $sql = "SELECT * FROM `najomnik` order by id_najomnik desc";
    $result = $this->db->query($sql);
    return $result;
}

any help? (please don't judge my code, i started learning php yesterday)

i hope someone will get the explanation

public function updateNajomnici($id, $meno, $priezvisko){
    $sql = "UPDATE `najomnik` SET meno = :meno, priezvisko = :priezvisko; WHERE id_najomnik=:id";
    $stmt = $this->db->prepare($sql);
    $stmt->bindparam(':id', $id);
    $stmt->bindparam(':meno', $meno);
    $stmt->bindparam(':priezvisko', $priezvisko);
    $stmt->execute();  
    $result = $stmt->fetch();
    return $result;
}

edit, this is in the same php file as form

    if(isset($_GET['id'])) {
    $id = $_GET['id'];
    $detaily = $crud->getNajomnicidetaily($id);
}


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source