Guest User

Untitled

a guest
Dec 12th, 2018
120
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.96 KB | None | 0 0
  1. <html>
  2. <head>
  3. <meta charset="UTF-8">
  4.  
  5. <link rel="stylesheet" type="text/css" href="mystyle_inserir.css">
  6. <link rel="stylesheet" type="text/css" href="w3schoolsExample.css">
  7.  
  8. <title>Inserir utilizador</title>
  9. <style>
  10. form label{
  11. display: inline-block;
  12. width: 100px;
  13. font-weight: bold;
  14. }
  15. </style>
  16. </head>
  17. <body>
  18.  
  19. <?php
  20.  
  21. $servername = "localhost";
  22. $username = "root";
  23. $password = "";
  24. $dbname = "rhumanos";
  25.  
  26. // Create connection
  27. $conn = mysqli_connect($servername, $username, $password, $dbname);
  28.  
  29. // Check connection
  30. if (!$conn) {
  31. die("Conecção falhou: " . mysqli_connect_error() );
  32. }
  33. echo "Conectado";
  34.  
  35. function addToDB(){
  36.  
  37. $nome = $_POST['nome'];
  38. $cargo = $_POST['cargo'];
  39. $email = $_POST['email'];
  40. $localidade = $_POST['localidade'];
  41. $setor = $_POST['setor'];
  42. $telefone = $_POST['telefone'];
  43. $salario = $_POST['salario'];
  44. $horario = $_POST['horario'];
  45.  
  46. $sql = "INSERT INTO trabalhador (nome, cargo, email, localidade, setor, telefone, salario, horario)
  47. VALUES ($nome, $cargo, $email, $localidade, $setor, $telefone, $salario, $horario)";
  48.  
  49. if (mysqli_query($conn, $sql)) {
  50. echo "Adicionado com sucesso";
  51. } else {
  52. echo "Erro: " . $sql . "<br>" . mysqli_error($conn);
  53. }
  54. mysqli_close($conn);
  55. }
  56. ?>
  57.  
  58. <h2>Exemplo de inserção numa MySQL database ao usar PHP</h2>
  59.  
  60. <br><br>
  61.  
  62. <form action="addToDB()" method="post">
  63. <label for="nome">Nome: </label> <input type="text" name="nome" />
  64. <br><br>
  65. <label for="cargo">Cargo: </label> <input type="text" name="cargo" />
  66. <br><br>
  67. <label for="email">E-Mail: </label> <input type="text" name="email" />
  68. <br><br>
  69. <label for="localidade">Localidade: </label> <input type="text" name="localidade" />
  70. <br><br>
  71. <label for="setor">Setor de Trabalho: </label> <input type="text" name="setor" />
  72. <br><br>
  73. <label for="telefone">Telefone: </label> <input type="text" name="telefone" />
  74. <br><br>
  75. <label for="salario">Salario (/h): </label> <input type="text" name="salario" />
  76. <br><br>
  77. <label for="horario">Horário (QT horas): </label> <input type="text" name="horario" />
  78. <br><br>
  79. <input type="submit" value="submit">
  80. </form>
  81.  
  82. </body>
  83.  
  84. </html>
Add Comment
Please, Sign In to add comment