Advertisement
Guest User

Untitled

a guest
Apr 17th, 2016
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.32 KB | None | 0 0
  1. <?php
  2.  
  3. // baza
  4. $dbserver = 'localhost';
  5. $dbuser = 'root';
  6. $dbpassword = '';
  7. $dbname = 'myDatabaseName';
  8.  
  9. $connection = mysql_connect($dbserver, $dbuser, $dbpassword);
  10. if (!$connection) {
  11. die('Could not connect: ' . mysql_error());
  12. }
  13.  
  14. $output = '';
  15.  
  16. // usuwanie
  17. if (isset($_GET['usun']) && !empty($_GET['usun'])) {
  18. $id = $_GET['usun'];
  19.  
  20. $sql = "DELETE FROM myDatabaseTable WHERE id='{$id}'";
  21. mysql_select_db($dbname);
  22. $result = mysql_query($sql, $connection);
  23. if (!$result) {
  24. die('Could not delete data: ' . mysql_error());
  25. }
  26.  
  27. header("Location: index.php");
  28. }
  29.  
  30. // dodawanie
  31. if ($_POST && isset($_POST['dodaj'])) {
  32. $first_name = $_POST['first_name'];
  33. $last_name = $_POST['last_name'];
  34. $mail = $_POST['mail'];
  35.  
  36. $sql = 'INSERT INTO myDatabaseTable ' .
  37. '(first_name, last_name, mail) ' .
  38. 'VALUES ( "' . $first_name . '", "' . $last_name . '", "' . $mail . '" )';
  39. mysql_select_db($dbname);
  40. $result = mysql_query($sql, $connection);
  41. if (!$result) {
  42. exit('Could not enter data: ' . mysql_error());
  43. }
  44.  
  45. header("Location: index.php");
  46. }
  47.  
  48. // edytowanie formularz
  49. if (isset($_GET['edytuj']) && !empty($_GET['edytuj'])) {
  50. $id = $_GET['edytuj'];
  51. $sql = "SELECT * FROM myDatabaseTable";
  52. mysql_select_db($dbname);
  53. $result = mysql_query($sql, $connection);
  54. if (!$result) {
  55. die('Could not delete data: ' . mysql_error());
  56. }
  57.  
  58.  
  59. $output .= "<form method='post'>";
  60. $output .= "<table border='0'>";
  61. $output .= "<thead>";
  62. $output .= "<td>ID</td>";
  63. $output .= "<td>Imię</td>";
  64. $output .= "<td>Nazwisko</td>";
  65. $output .= "<td>Email</td>";
  66. $output .= "<td>Akcja</td>";
  67. $output .= "</thead>";
  68.  
  69. while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
  70.  
  71. if ($row['id']==$id){
  72.  
  73. $output .= "<form method='post'>";
  74. $output .= "<tr>";
  75. $output .= "<td><input type='hidden' name='id' value='{$row['id']}' /></td>";
  76. $output .= "<td><input type='text' name='first_name' placeholder='Wpisz imie...' value='{$row['first_name']}' require/></td>";
  77. $output .= "<td><input type='text' name='last_name' placeholder='Wpisz nazwisko...' value='{$row['last_name']}' require/></td>";
  78. $output .= "<td><input type='text' name='mail' placeholder='Wpisz email...' value='{$row['mail']}' require /></td>";
  79. $output .= "<td><input type='submit' name='zapisz' value='Zapisz' /></td>";
  80. $output .= "</tr>";
  81. $output .= "</form>";
  82. }
  83. else
  84. {
  85. $output .= "<tr>";
  86. $output .= "<td>{$row['id']}</td>";
  87. $output .= "<td>{$row['first_name']}</td>";
  88. $output .= "<td>{$row['last_name']}</td>";
  89. $output .= "<td>{$row['mail']}</td>";
  90. $output .= "<td>"
  91. . "<a href='?usun={$row['id']}' type='usun'>Usuń</a>"
  92. . " | <a href='?edytuj={$row['id']}' type='edytuj'>Edytuj</a>"
  93. . "</td>";
  94. $output .= "</tr>";
  95.  
  96. }
  97.  
  98.  
  99.  
  100. }
  101.  
  102.  
  103. }
  104.  
  105. // edytowanie wysyłka formularza
  106. if ($_POST && isset($_POST['zapisz'])) {
  107. $id = $_GET['edytuj'];
  108. $first_name = $_POST['first_name'];
  109. $last_name = $_POST['last_name'];
  110. $mail = $_POST['mail'];
  111.  
  112. $sql = "UPDATE myDatabaseTable SET "
  113. . "first_name = '{$first_name}', "
  114. . "last_name = '{$last_name}', "
  115. . "mail = '{$mail}' " .
  116. " WHERE id='{$id}'";
  117. mysql_select_db($dbname);
  118. $result = mysql_query($sql, $connection);
  119. if (!$result) {
  120. exit('Could not enter data: ' . mysql_error());
  121. }
  122.  
  123. header("Location: index.php");
  124. }
  125.  
  126. // wszystkie rekordy
  127. if (!isset($_GET['edytuj'])) {
  128. $sql = 'SELECT id, first_name, last_name, mail FROM myDatabaseTable ORDER BY id ASC, first_name ASC';
  129. mysql_select_db($dbname);
  130. $result = mysql_query($sql, $connection);
  131. if (!$result) {
  132. die('Could not get data: ' . mysql_error());
  133. }
  134.  
  135. $output .= "<form method='post'>";
  136. $output .= "<table border='0'>";
  137. $output .= "<thead>";
  138. $output .= "<td>ID</td>";
  139. $output .= "<td>Imię</td>";
  140. $output .= "<td>Nazwisko</td>";
  141. $output .= "<td>Email</td>";
  142. $output .= "<td>Akcja</td>";
  143. $output .= "</thead>";
  144.  
  145. while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
  146. $output .= "<tr>";
  147. $output .= "<td>{$row['id']}</td>";
  148. $output .= "<td>{$row['first_name']}</td>";
  149. $output .= "<td>{$row['last_name']}</td>";
  150. $output .= "<td>{$row['mail']}</td>";
  151. $output .= "<td>"
  152. . "<a href='?usun={$row['id']}' type='usun'>Usuń</a>"
  153. . " | <a href='?edytuj={$row['id']}' type='edytuj'>Edytuj</a>"
  154. . "</td>";
  155. $output .= "</tr>";
  156. }
  157.  
  158. $output .= "<tr>";
  159. $output .= "<td></td>";
  160. $output .= "<td><input type='text' name='first_name' placeholder='Wpisz imie...' /></td>";
  161. $output .= "<td><input type='text' name='last_name' placeholder='Wpisz nazwisko...' /></td>";
  162. $output .= "<td><input type='text' name='mail' placeholder='Wpisz email...' /></td>";
  163. $output .= "<td><input type='submit' name='dodaj' value='Dodaj użytkownika' /></td>";
  164. $output .= "</tr>";
  165.  
  166. $output .= "</form>";
  167. }
  168.  
  169. echo $output;
  170.  
  171. mysql_close($connection);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement