Advertisement
nelsonchalid

form com validação e gravação no bd

May 20th, 2022
1,199
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.49 KB | None | 0 0
  1. <?php
  2.  
  3.     if (count($_POST) > 0) {
  4.         include('conexao2.php');
  5.  
  6.         $erro = false;
  7.         $nome = $_POST['nome'];
  8.         $url = $_POST['url'];
  9.         $palavras_chave = $_POST['palavras_chave'];
  10.  
  11.         if(empty($nome)) {
  12.             $erro = "Preencha o nome";
  13.         }
  14.         if(empty($url)) {
  15.             $erro = "Preencha a URL";
  16.         }
  17.         if(empty($palavras_chave)) {
  18.             $erro = "Preencha as palavras-chave ";
  19.         }    
  20.         if($erro) {
  21.             echo "<p><b>ERRO: $erro</b></p>";        
  22.         } else {
  23.             $sql_code = "INSERT INTO url (nome, url, palavras_chave, data) VALUES ('$nome', '$url', '$palavras_chave', NOW())";
  24.             $deu_certo = $mysqli->query($sql_code) or die($mysqli->error);
  25.             if($deu_certo){
  26.                 echo "<br><br><center><h3><b>Registro cadastrado com sucesso.</h3></p></center>";                
  27.                 unset($_POST);
  28.             }  
  29.         }    
  30.     }
  31.  
  32. ?>
  33.  
  34. <html>
  35.     <head>
  36.     <meta name="viewport" content="width=device-width, initial-scale=1.0">
  37.     <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css">
  38.         <title>Cadastro</title>
  39.     </head>
  40.     <body>
  41.     <div class="container">
  42.         <br><br><a href="index.php">Listar</a><br>
  43.         <br>
  44.         <h5>Cadastro</h5>
  45.         <br>        
  46.             <form action="" method="POST">      
  47.                 <div class="form-group">                
  48.                     <label>Nome da página ou site</label>
  49.                     <input type="text" value="<?php if(isset($_POST['nome'])) echo $_POST['nome']; ?>" name="nome" class="form-control" size="50">                    
  50.                 </div>
  51.                 <div class="form-group">                
  52.                      <label>URL</label>
  53.                     <input type="text" value="<?php if(isset($_POST['url'])) echo $_POST['url']; ?>" name="url" class="form-control" size="99">                    
  54.                 </div>
  55.                 <div class="form-group">                
  56.                      <label>Palavras-chave (separadas por vírgula)</label>
  57.                     <input type="text" value="<?php if(isset($_POST['palavras_chave'])) echo $_POST['palavras_chave']; ?>" name="palavras_chave" class="form-control" size="99">                    
  58.                 </div>
  59.  
  60.                 <button type="submit" class="btn btn-success">Cadastrar</button>
  61.             </form>
  62.     </div>
  63.     </body>
  64. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement