Advertisement
drunkspinda02

bd exemplo

Feb 12th, 2019
113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.07 KB | None | 0 0
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <title></title>
  5. </head>
  6. <style type="text/css">
  7. body{
  8. background-image: url("fogo.jpg");
  9. background-color: red;
  10. }
  11. div{
  12. height: 400px;
  13. width: 50%;
  14. background-color:yellow;
  15. border-radius: 15px;
  16. margin: auto;
  17. border: 2px solid white;
  18. }
  19. label{
  20. margin: auto;
  21. padding: 15px;
  22.  
  23. }
  24. input{
  25. width: 100%;
  26. }
  27. h1{
  28. text-align: center;
  29. padding-top:15px;
  30. }
  31. .insert{
  32. width: 100px;
  33. height: 40px;
  34. text-align: center;
  35. margin: auto;
  36. }
  37. .listar{
  38. width: 100px;
  39. height: 40px;
  40. text-align: center;
  41. margin: auto;
  42. }
  43. .alterar{
  44. width: 100px;
  45. height: 40px;
  46. text-align: center;
  47. margin: auto;
  48. }
  49. .apagar{
  50. width: 100px;
  51. height: 40px;
  52. text-align: center;
  53. margin: auto;
  54. }
  55. .consultar{
  56. width: 49%;
  57. }
  58. form{
  59. margin: auto;
  60. padding: 15px;
  61. }
  62. .alert{
  63. width: 40%;
  64. height: 100px;
  65. margin: auto;
  66. text-align: center;
  67. padding: 15px;
  68. background-color: orange;
  69. color: white;
  70. }
  71. td{
  72. border: 2px solid white;
  73. border-collapse: collapse;
  74. }
  75. .nome{
  76. width: 49%;
  77. }
  78.  
  79. </style>
  80. <body>
  81.  
  82. <?php
  83.  
  84. $turnbd = new mysqli('localhost','root','','exemplo da');
  85. $tnumero=$tnome=$temail=$ttelefone='';
  86.  
  87. if ($turnbd->connect_error) {
  88. die('Erro na ligação : ('. $turnbd->connect_errno .') '. $turnbd->connect_error);
  89.  
  90. }
  91.  
  92. //Inserçao
  93.  
  94. if(isset($_POST['inserir'])){
  95.  
  96. $query = "INSERT INTO alunos(numero,nome,email,telefone) VALUES(?,?,?,?)";
  97.  
  98.  
  99. if ($_POST['numero']=='' || $_POST['nome']=='' || $_POST['email']=='' || $_POST['telefone']=='' ) {
  100. echo "<div class='alert'><h1>Existem campos que nao foram preenchidos!</h1></div>";
  101. }
  102.  
  103. else{
  104.  
  105.  
  106. $statement = $turnbd->prepare($query);
  107.  
  108. $statement->bind_param('issi', $_POST["numero"],
  109. $_POST["nome"],
  110. $_POST["email"],
  111. $_POST["telefone"]);
  112. $statement->execute();
  113.  
  114. if ($statement->affected_rows>0){
  115. echo "<div class='alert'><h1>Foi inserido/atualizado um registo!</h1></div>";
  116. }
  117. else{
  118. die('Erro: ('. $turnbd->errno .') '. $turnbd->error);
  119. }
  120. $tnumero=$tnome=$temail=$ttelefone='';
  121. $statement->close();
  122.  
  123. }
  124.  
  125. $turnbd->close();
  126. }
  127.  
  128. //Listagem
  129.  
  130. if(isset($_POST['listagem'])){
  131. $query = "SELECT numero,nome,email,telefone FROM alunos ORDER BY numero";
  132. $statement = $turnbd->prepare($query);
  133. $statement->execute();
  134. $statement->bind_result($tnumero,$tnome,$temail,$ttelefone);
  135.  
  136.  
  137. echo '<table style=" color: white; border: 2px solid white; margin: auto; border-collapse: collapse;">';
  138. echo "<tr>";
  139. echo "<td>Numero</td>";
  140. echo "<td>Nome</td>";
  141. echo "<td>E-mail</td>";
  142. echo "<td>Telefone</td>";
  143. echo "</tr>";
  144. while ($statement->fetch()) {
  145. echo "<tr>";
  146. echo "<td>$tnumero</td>";
  147. echo "<td>$tnome</td>";
  148. echo "<td>$temail</td>";
  149. echo "<td>$ttelefone</td>";
  150. echo "</tr>";
  151. }
  152. echo "</table><br>";
  153. $tnumero=$tnome=$temail=$ttelefone='';
  154. $statement->close();
  155. $turnbd->close();
  156. }
  157.  
  158. //Consulta
  159.  
  160. if(isset($_POST['consulta'])){
  161. $query = "SELECT * FROM alunos WHERE numero=?";
  162. $statement = $turnbd->prepare($query);
  163. $statement->bind_param('i',$_POST["numero"]);
  164. $statement->execute();
  165. $statement->bind_result($tnumero,$tnome,$temail,$ttelefone);
  166.  
  167. if ($statement->fetch()) {
  168. echo "<div class='alert'><h1>Foi encontrado o registo!!</h1></div>";
  169. }
  170.  
  171. else{
  172. echo "<div class='alert'><h1>Não foi encontrado o registo!!</h1></div>";
  173. }
  174.  
  175.  
  176. }
  177.  
  178. //Alterar
  179.  
  180. if(isset($_POST['alterar'])){
  181. $query = "SELECT COUNT(*) FROM alunos WHERE numero=?";
  182. $statement = $turnbd->prepare($query);
  183. $statement->bind_param('i',$_POST["numero"]);
  184. $statement->execute();
  185. $statement->bind_result($nrows);
  186. $statement->fetch();
  187. $statement->close();
  188.  
  189. if ($nrows>0) {
  190. $query = "UPDATE alunos SET nome=?,email=?,telefone=? WHERE numero=?";
  191. $statement = $turnbd->prepare($query);
  192. $statement->bind_param('ssii', $_POST["nome"],
  193. $_POST["email"],
  194. $_POST["telefone"],
  195. $_POST["numero"]);
  196. $statement->execute();
  197. echo "<div class='alert'><h1>Foi feita uma alteraçao!</h1></div>";
  198. }
  199.  
  200. else if ($_POST['numero']=='' || $_POST['nome']=='' || $_POST['email']=='' || $_POST['telefone']=='' ) {
  201. echo "<div class='alert'><h1>Existem campos que nao foram preenchidos!</h1></div>";
  202. }
  203.  
  204. else{
  205. echo "<div class='alert'><h1>Não foi encontrado o registo!!</h1></div>";
  206. }
  207. }
  208.  
  209. //Apagar
  210.  
  211. if(isset($_POST['apagar'])){
  212. $query = "DELETE FROM alunos WHERE numero=?";
  213. $statement = $turnbd->prepare($query);
  214. $statement->bind_param('i',$_POST["numero"]);
  215.  
  216. if ($statement->execute() && $statement->affected_rows>0) {
  217. echo "<div class='alert'><h1>Registo eliminado!</h1></div>";
  218. }
  219. else{
  220. echo "<div class='alert'><h1>Não foi encontrado o registo!!</h1></div>";
  221. }
  222. }
  223.  
  224.  
  225. /*if(isset($_POST['pesc'])){
  226. if($_POST['numero']==''){
  227. echo "<div class='alert'><h1>Existem campos que nao foram preenchidos!</h1></div>";
  228. }
  229. else{
  230. $qr = "select * from alunos where numero=?";
  231. $ordem = $turnbd->prepare($qr);
  232. $ordem->bind_param('i', $_POST['numero']);
  233. $ordem->execute();
  234. $ordem->bind_result($tnumero, $tnome, $temail, $ttelemovel);
  235. if($ordem->fetch()){
  236. echo "<div class='alert'><h1>Foi encontrado o registo!!</h1></div>";
  237. }
  238. else{
  239. echo "<div class='alert'><h1>Nao foi encontrado o registo!!</h1></div>";
  240. }
  241. $ordem->close();
  242. }
  243. }*/
  244.  
  245.  
  246.  
  247. /*$statement="select * from alunos order by numero";
  248. $listart = $turnbd->query($statement);
  249.  
  250. while($row1 = $listart->fetch_array()) {
  251. echo '<option value="'.$row1["numero"].'">'.$row1["nome"].'</option>';
  252.  
  253. }
  254. $statement->free();*/
  255. ?>
  256.  
  257. <div>
  258. <h1>Gestão de Contactos</h1>
  259. <form name="form1" action="" method="post">
  260. <input type="hidden" name="pesc" value="1">
  261. Pesquisa por nome:<select id="opçoes" name="opcoes" onChange="document.form1.submit();>
  262. <option value="Selecione um nome">Selecione um nome</option>
  263. </select>
  264. </form>
  265. <form action="" method="post">
  266. <label>Número:</label><br><input class="nome" type="text" name="numero" value="<?php echo $tnumero; ?>"><input class="consultar" type="submit" name="consulta" value="Consulta"><br>
  267. <label>Nome:</label><br><input type="text" name="nome" value="<?php echo $tnome; ?>"><br>
  268. <label>E-mail:</label><br><input type="email" name="email" value="<?php echo $temail; ?>"><br>
  269. <label>Telefone:</label><br><input type="text" name="telefone" value="<?php echo $ttelefone; ?>"><br><br>
  270. <input class="insert" type="submit" name="inserir" value="Inserir">
  271. <input class="listar" type="submit" name="listagem" value="Listar Todos">
  272. <input class="alterar" type="submit" name="alterar" value="Alterar">
  273. <input class="apagar" type="submit" name="apagar" value="Apagar">
  274. </form>
  275. </div>
  276.  
  277. </body>
  278. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement