Advertisement
jpvieiraa

PHP VIEIRA 3

Feb 19th, 2019
147
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.27 KB | None | 0 0
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <title>Exemplo</title>
  5. <style type="text/css">
  6. #ger1{
  7. margin: auto;
  8. background-color: #993333;
  9. color: white;
  10. width: 500px;
  11. height: auto;
  12. border:2px solid black;
  13. border-radius: 10px;
  14. }
  15. .ger2{
  16. margin: auto;
  17. background-color: #993333;
  18. color: white;
  19. width: 500px;
  20. height: auto;
  21. border:2px solid black;
  22. border-radius: 10px;
  23. }
  24. p{
  25. text-align: center;
  26. }
  27.  
  28. .alerta{
  29. border-radius: 10px;
  30. text-align: center;
  31. margin: auto;
  32. background-color: red;
  33. margin-right: 20px;
  34. margin-left: 20px;
  35. }
  36. .done{
  37. border-radius: 10px;
  38. text-align: center;
  39. margin: auto;
  40. background-color: green;
  41. margin-right: 20px;
  42. margin-left: 20px;
  43. }
  44. .tabelanova{
  45. margin: auto;
  46. }
  47. </style>
  48. </head>
  49. <body>
  50. <?php
  51. $bd_host="localhost";
  52. $bd_user="root";
  53. $bd_password="";
  54. $bd_database="13787_3";
  55. $msg= "";
  56. $existe="";
  57. $numero=$idade=$email=$local="";
  58. $mq = new mysqli($bd_host,$bd_user,$bd_password,$bd_database);
  59.  
  60.  
  61.  
  62. if(isset($_POST["submit"])){
  63. //if($_POST["Idade"]!='' && ...)
  64. $qr = "INSERT INTO tbl_13787(Numero,Idade,Email,Local) VALUES(?,?,?,?)";
  65. $st = $mq->prepare($qr);
  66. $st->bind_param('iiss', $_POST["Numero"],
  67. $_POST["Idade"],
  68. $_POST["Email"],
  69. $_POST["Local"]);
  70.  
  71. if ($st->execute() && $st->affected_rows>0){
  72. $msg= "<p class='done'>Foi inserido/atualizado um registo!</p>";
  73.  
  74. }
  75. else{
  76. $msg='<p class="alerta"> Erro: ('. $mq->errno .') '. $mq->error .'</p>';
  77. $erro=1;
  78. }
  79.  
  80. $st->close();
  81.  
  82.  
  83.  
  84. }
  85. if(isset($_POST["Consultar"])){
  86. $qr="SELECT * FROM tbl_13787 ORDER BY Numero";
  87.  
  88. $results = $mq->query($qr);
  89. echo "<table class=tabelanova border='1'>";
  90. echo "<td>Numero</td>";
  91. echo "<td>Idade</td>";
  92. echo "<td>Email</td>";
  93. echo "<td>Local</td>";
  94. while ($row = $results->fetch_assoc()) {
  95. echo "<tr>";
  96. echo "<td>".$row["Numero"]."</td>";
  97. echo "<td>".$row["Idade"]."</td>";
  98. echo "<td>".$row["Email"]."</td>";
  99. echo "<td>".$row["Local"]."</td>";
  100. echo "</tr>";
  101. }
  102. echo "</table>";
  103. }
  104. else{
  105.  
  106. if(isset($_POST["pesquisar"])){
  107.  
  108. $qr="SELECT * FROM tbl_13787 where Numero=?";
  109. $st=$mq->prepare($qr);
  110. $st->bind_param('i', $_POST["Numero"]);
  111. $st->execute();
  112. $st->bind_result($numero,$idade,$email,$local);
  113. if($st->fetch()){
  114. $existe=true;
  115. $msg='<p class="done" > Registo Encontrado</p>';
  116.  
  117. }
  118. else{
  119. $existe=false;
  120. $msg='<p class="alerta">Registo nao Encontrado</p>';
  121. }
  122. $st->close();
  123. }
  124. else{
  125. if(isset($_POST["Alterar"])){
  126. $qr="UPDATE tbl_13787 SET Idade=?, Email=?, Local=? WHERE Numero=?";
  127. $st=$mq->prepare($qr);
  128. $st->bind_param('issi',$_POST["Idade"],$_POST["Email"],$_POST["Local"],$_POST["Numero"]);
  129. $st->execute();
  130. $st->close();
  131. }
  132. else{
  133. if(isset($_POST["Apagar"])){
  134. $qr="DELETE FROM tbl_13787 WHERE Numero= ?";
  135. $st=$mq->prepare($qr);
  136. $st->bind_param('i',$_POST["Numero"]);
  137. $st->execute();
  138. $st->close();
  139. }
  140. }
  141. }
  142. }
  143.  
  144.  
  145. ?>
  146. <br>
  147. <form method="post" action="" id="tbl" class=ger2 name="tbl">
  148. <input type="hidden" name="pesquisar" value="1"/>
  149. <p>Seleciona: <select name="Numero" id="Numero" onChange="document.tbl.submit();">
  150. <option value="-1">
  151. Selecione um registo
  152. </option>
  153. <?php
  154. $qrs="SELECT * FROM tbl_13787";
  155. $st = $mq->query($qrs);
  156. $option = '';
  157. while($row = $st->fetch_assoc())
  158. {
  159. echo '<option value = "'.$row['Numero'].'">'.$row['Numero'].'</option>';
  160. }
  161. $st->close();
  162. ?>
  163. </select>
  164. </form>
  165. <form method="post" action="" id="ger1" class="ger2">
  166. <p>Numero:<input type="number" name="Numero" value="<?php echo $numero ?>"><input type="submit" name="pesquisar" value="Pesquisar registo"></p>
  167. <p>Idade:<input type="number" name="Idade" value="<?php echo $idade ?>"></p>
  168. <p>Email:<input type="text" name="Email" value="<?php echo $email ?>"></p>
  169. <p>Local:<input type="text" name="Local" value="<?php echo $local ?>"></p>
  170. <p><input type="submit" name="submit" value="Inserir registo" id="botao1"></p>
  171. <p><input type="submit" name="Consultar" value="Consultar registo" id="botao2"></p>
  172. <?php if ($existe==true) {?>
  173. <p><input type="submit" name="Alterar" value="Alterar registo" /></p>
  174. <p><input type="submit" name="Apagar" value="Eliminar registo" /></p>
  175. <?php }?>
  176. <p>
  177. <?php
  178. echo $msg;
  179. $mq->close();
  180. ?>
  181. </p>
  182. </form>
  183. </body>
  184. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement