This shows you the differences between two versions of the page.
Both sides previous revision Previous revision Next revision | Previous revision | ||
simple_crud [2016/05/23 13:46] mh |
simple_crud [2016/05/23 15:32] (current) mh |
||
---|---|---|---|
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> | ||
+ | |||
+ | ==== Single Page CRUD ==== | ||
+ | noch sehr unansehnlich | ||
+ | |||
+ | <code> | ||
+ | <?php | ||
+ | require('db.php'); | ||
+ | $dbh = get_db(); | ||
+ | |||
+ | if (isset($_SERVER['PATH_INFO'])) { | ||
+ | $path = explode('/', $_SERVER['PATH_INFO']); | ||
+ | } else { | ||
+ | $path = array(); | ||
+ | } | ||
+ | |||
+ | $path = array_filter($path); | ||
+ | /* | ||
+ | echo "<pre>"; | ||
+ | var_dump($path); | ||
+ | echo "</pre>"; | ||
+ | */ | ||
+ | |||
+ | if (empty($path)) { | ||
+ | list_persons($dbh); | ||
+ | exit; | ||
+ | } | ||
+ | elseif ($_SERVER['REQUEST_METHOD'] == 'GET' && $path[1] == 'write') { | ||
+ | $country_list = | ||
+ | $dbh->query('select Code, Name from country') | ||
+ | ->fetch_all(MYSQLI_ASSOC); | ||
+ | |||
+ | if ($path[2] == 'id' and isset($path[3])) { | ||
+ | $id = $path[3]; | ||
+ | echo "<h2>Update Person $id</h2>\n"; | ||
+ | $sth = $dbh->prepare( | ||
+ | 'select id, fullname, birthdate, country_code from person where id = ?' | ||
+ | ); | ||
+ | |||
+ | $sth->bind_param("d", $id); | ||
+ | $sth->execute(); | ||
+ | $sth->bind_result($read_id, $fullname, $birthdate, $country_code); | ||
+ | $sth->fetch(); | ||
+ | if ($read_id != $id) { | ||
+ | ?> | ||
+ | <meta http-equiv="refresh" content="2; url=persons.php" /> | ||
+ | <h2>unknown id, go away</h2> | ||
+ | <?php | ||
+ | exit(); | ||
+ | } | ||
+ | } else { | ||
+ | echo "<h2>Insert Person</h2>\n"; | ||
+ | $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="0.5; url=/kurs/person.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(); | ||
+ | } | ||
+ | |||
+ | function list_persons($dbh) { | ||
+ | $person_list = | ||
+ | $dbh->query('select id, fullname, birthdate, country_code from person') | ||
+ | ->fetch_all(MYSQLI_ASSOC); | ||
+ | | ||
+ | ?> | ||
+ | </pre> | ||
+ | <div id="person_list"> | ||
+ | <?php | ||
+ | foreach ($person_list as $person) { | ||
+ | printf ( | ||
+ | "<li><a href='person.php/write/id/%d'>%s</a>, %s, %s </li>\n", | ||
+ | $person['id'], | ||
+ | $person['fullname'], | ||
+ | $person['birthdate'], | ||
+ | $person['country_code'] | ||
+ | ); | ||
+ | } | ||
+ | |||
+ | ?> | ||
+ | </div> | ||
+ | <br> | ||
+ | <a href='person.php/write'>Add new person</a> | ||
+ | <html> | ||
+ | <?php | ||
+ | } | ||
+ | |||
+ | </code> |