User Tools

Site Tools


simple_crud

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revision Previous revision
Last revision Both sides next revision
simple_crud [2016/05/23 13:46]
mh
simple_crud [2016/05/23 13:47]
mh [Update oder Insert]
Line 115: Line 115:
  
 ==== Update oder Insert ==== ==== Update oder Insert ====
 +
 +write_person.php
 +<​code>​
 +
 +<?php
 +require('​db.php'​);​
 +$dbh = get_db();
 +
 +if ($_SERVER['​REQUEST_METHOD'​] == '​GET'​) {
 +    $country_list = 
 +    $dbh->​query('​select Code, Name from country'​)
 +      ->​fetch_all(MYSQLI_ASSOC);​   ​
 +
 +  if (isset($_GET['​id'​])) {
 +   $id = $_GET['​id'​]; ​   ​
 +      $sth = $dbh->​prepare(
 +      '​select fullname, birthdate, country_code from person where id = ?'
 +      );
 +
 +
 +      $sth->​bind_param("​d",​ $id);
 +      $sth->​execute();​
 +      $sth->​bind_result($fullname,​ $birthdate, $country_code);​
 +      $sth->​fetch();​
 +    } else {
 + $id = '';​
 + $fullname = '';​
 + $birthdate = '';​
 + $country_code = '';​
 + }
 +?>
 +<​html>​
 +<​form ​
 +  name="​personform" ​
 +  id="​personform"​
 +  action="<?​php echo $_SERVER['​PHP_SELF'​];?>"​
 +  method="​POST">​
 +  ​
 +  <input name="​id"​ id="​id"​ type="​hidden"​ value ="<?​php echo $id?>"/>​
 +  <​p>​Fullname:​ <input name="​fullname"​ id="​fullname"​ type="​text"​ size="​30"​
 +    value="<?​php echo $fullname?>"></​p> ​
 +  <​p>​Birthdate:​ <input name="​birthdate"​ id="​birthdate"​ type="​text"​ size="​30"​
 +    value="<?​php echo $birthdate?>" ​     ​
 +    ></​p>  ​
 +  <​p>​Country: ​  
 +  <select name="​country_code"​ id="​country_code" ​  
 +  >
 +  <?php
 +  foreach ($country_list as $row) {
 + if ($row['​Code'​] == $country_code) {
 + $selected = '​selected';​
 + } else { $selected = '';​ }
 +    printf('<​option value="​%s"​ %s>​%s</​option>', ​
 +         $row['​Code'​],​ $selected, $row['​Name'​] );
 +  }
 +  ?>
 +  </​select>​
 +  </​p> ​    
 +  <​p><​input type="​submit"​ value="​Save"></​p>​
 +  ​
 +</​form>  ​
 +
 +<?php
 +}
 +elseif ($_SERVER['​REQUEST_METHOD'​] == '​POST'​) {
 +?>
 +<meta http-equiv="​refresh"​ content="​1;​ url=list-persons.php"​ />
 +<?php
 +
 +$fullname = $_POST['​fullname'​];​
 +$birthdate = $_POST['​birthdate'​];​
 +$country_code = $_POST['​country_code'​];​
 +
 +if (isset($_POST['​id'​]) and  $_POST['​id'​] != ''​ ) {
 +  echo "​updating<​br>​\n";​
 +  $id = $_POST['​id'​];​
 +  $sth = $dbh->​prepare(
 +  "​update person ​
 +    set fullname = ?,
 +        birthdate = ?,
 +        country_code = ? 
 +   where id = ?"
 +  );
 +  $sth->​bind_param("​sssd",​ $fullname, $birthdate, $country_code,​ $id);
 +} else {
 +  echo "​inserting<​br>​\n";​
 +  $sth = $dbh->​prepare(
 +    "​insert into person ​
 +      (fullname, ​
 +       ​birthdate, ​
 +       ​country_code) ​
 +     ​values (?, ?, ?)"
 +  );
 +  $sth->​bind_param("​sss",​ $fullname, $birthdate, $country_code);​
 +}  ​
 +$sth->​execute();​
 +
 +}
 +</​code>​
 +
 +list-persons.php
 +
 +<​code>​
 +<​html>​
 +<script src="​https://​code.jquery.com/​jquery-2.2.0.min.js"></​script>​
 +<pre>
 +<?php
 +require('​db.php'​);​
 +
 +// if (
 +
 +// $path = preg_split("/​\/​+/",​ $_SERVER['​PATH_INFO'​].'/'​);​
 +
 +// var_dump($_SERVER);​
 +// var_dump($path);​
 +
 +$dbh = get_db();
 +$person_list = 
 +  $dbh->​query('​select id, fullname, birthdate, country_code from person'​)
 +      ->​fetch_all(MYSQLI_ASSOC);​   ​
 +   ​
 +// echo format_html_table($person_list,​ '​persontable'​);​   ​
 +?>
 +</​pre>​
 +<div id="​person_list">​
 +<?php
 +foreach ($person_list as $person) {
 +  printf (
 +  "<​li><​a href='​write_person.php?​id=%d'>​%s</​a>,​ %s, %s </​li>​\n", ​
 +             ​$person['​id'​],​
 + $person['​fullname'​],​
 + $person['​birthdate'​],​
 + $person['​country_code'​]
 + );  
 +}
 +
 +?>
 +</​div>​
 +<br>
 +<a href='​write_person.php'>​Add new person</​a>​
 +<​html>​
 +</​code>​
simple_crud.txt ยท Last modified: 2016/05/23 15:32 by mh