Jump to content
DMBoss

Envio de Campos Vazios para BD via Form

Recommended Posts

DMBoss

Form (adesão.php)

<?php include_once "../inc/error.inc"; ?>

<!DOCTYPE html>
<html lang="pt">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>M.E.R.D.A. - Adesão</title>
    <link rel="stylesheet" href="../css/bootstrap.css">
    <script src="https://code.jquery.com/jquery-3.3.1.slim.min.js" integrity="sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo" crossorigin="anonymous"></script>
</head>
<body class="bg-secondary">
    <div class="container-fluid text-white">
        <!--Menu-->
        <?php include_once "../inc/menu_paginas.inc"; ?>

        <!--Header-->
        <?php include_once "../inc/header_paginas.inc"; ?>

        <!--Main Frame-->
        <div class="container border border-dark">
            <div class="row justify-content-center">
                <div class="col-6">
                    <!--Formlário-->
                    <form action="confirmacao.php" method="POST">
                        <!--Dados Pessoais-->
                        <div class="row">
                            <div class="col">Dados Pessoais</div>
                        </div>
                        <div class="row">
                            <div class="col">
                                <div class="form-group">
                                    <label for="txtnome">Nome *</label>
                                    <input type="text" class="form-control" id="txtnome" name="txtnome" aria-describedby="namelHelp" placeholder="Nome" required>
                                    <small id="nameHelp" class="form-text text-dark">Ex. Manuel</small>
                                </div>
                            </div>
                            <div class="col">
                                <div class="form-group">
                                    <label for="txtapelido">Apelido *</label>
                                    <input type="text" class="form-control" id="txtapelido" name="txtapelido" aria-describedby="nameHelp" placeholder="Apelido" required>
                                    <small id="nameHelp" class="form-text text-dark">Ex. Nicolau</small>
                                </div>
                            </div>
                        </div>
                        <div class="row">
                            <div class="col">
                                <div class="form-group">
                                    <label for="txttelemovel">Telemóvel</label>
                                    <input type="text" class="form-control" id="txttelemovel" name="txttelemovel" aria-describedby="tellHelp" placeholder="Telefone">
                                    <small id="telHelp" class="form-text text-dark">Ex. 96X XXX XXX</small>
                                </div>
                            </div>
                            <div class="col">
                                <div class="form-group">
                                    <label for="txtdata">Data Nascimento</label>
                                    <input type="date" class="form-control" id="txtdata" name="txtdata" aria-describedby="datelHelp" placeholder="DD/MM/AAAA">
                                    <small id="dateHelp" class="form-text text-dark">Ex. 07/11/1989</small>
                                </div>
                            </div>
                        </div>
                        <div class="form-group">
                            <label for="txtemail">Email *</label>
                            <input type="email" class="form-control" id="txtemail" name="txtemail" aria-describedby="emailHelp" placeholder="Email" required>
                            <small id="emailHelp" class="form-text text-dark">Ex. email@dominio.com</small>
                        </div>
                        <hr>
                        <!--Dados Adesão-->
                        <div class="row">
                            <div class="col">Dados Adesão</div>
                        </div>
                        <div class="row">
                            <div class="col">
                                <div class="form-group">
                                    <label for="txtnick">Nick *</label>
                                    <input type="text" class="form-control" id="txtnick" name="txtnick" aria-describedby="nameHelp" placeholder="Nick" required>
                                    <small id="nameHelp" class="form-text text-dark">Ex. DMBoss</small>
                                </div>
                            </div>
                            <div class="col">
                                <div class="form-group">
                                    <label for="txtafiliacao">Padrinho / Madrinha</label>
                                    <input type="text" class="form-control" id="txtafiliacao" name="txtafiliacao" aria-describedby="nameHelp" placeholder="Padrinho / Madrinha">
                                    <small id="nameHelp" class="form-text text-dark">Ex. DMBoss<br>Quem te convidou para o M.E.R.D.A.</small>
                                </div>
                            </div>
                        </div>
                        <div class="row">
                            <div class="col">
                                <div class="form-group">
                                    <label for="txtcargo">Cargo</label>
                                    <input type="text" class="form-control" id="txtcargo" name="txtcargo" aria-describedby="tellHelp" placeholder="Cargo">
                                    <small id="nameHelp" class="form-text text-dark">Cargo ocupado.<br>Por defeito será "Membro".</small>
                                </div>
                            </div>
                            <div class="col">&nbsp;</div>
                        </div>
                        <div class="form-group">
                            <label for="txtmotivo">Motivo da Adesão *</label>
                            <textarea class="form-control" id="txtmotivo" name="txtmotivo" aria-describedby="nameHelp" rows="3" required></textarea>
                            <small id="nameHelp" class="form-text text-dark">Porque motivo desejas juntar-te a nós?</small>
                        </div>
                        <hr> * Canmpos Obrigatórios
                        <br>
                        <br>
                        <button type="submit" class="btn btn-dark">Enviar</button>
                        <br>
                    </form>
                </div>
            </div>
            <div class="row">
                <p>&nbsp;</p>
            </div>
        </div>
        <!--Footer-->
        <?php include_once "../inc/footer.inc"; ?>
        <div class="row">
            <p>&nbsp;</p>
        </div>
    </div>
</body>
</html>

<!--Script's-->
<?php include_once "../inc/javascript.inc"; ?>

Página de Confirmação que chama a Função Adicionar

<?php include_once "../inc/error.inc"; ?>

<!DOCTYPE html>
<html lang="pt">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>M.E.R.D.A. - Home</title>
    <link rel="stylesheet" href="../css/bootstrap.css">
    <script src="https://code.jquery.com/jquery-3.3.1.slim.min.js" integrity="sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo" crossorigin="anonymous"></script>
</head>
<body class="bg-secondary">
    <div class="container-fluid text-white">
        <!--Menu-->
        <?php include_once "../inc/menu_paginas.inc"; ?>

        <!--Header-->
        <?php include_once "../inc/header_paginas.inc"; ?>

        <!--Main Frame-->
        <div class="container border border-dark">
            <?php include_once "../inc/adesao.inc";

                echo '<script>console.log("Teste");</script>';

                $membros = new Membros();
                $membros->add();
            ?>
            <p>Sê muito bem vindo ao nosso convívio e família.</p>
        </div>
        <!--Footer-->
        <?php include_once "../inc/footer.inc"; ?>
    </div>
</body>
</html>

<!--Script's-->
<?php include_once "../inc/javascript.inc"; ?>

Função para Adicionar na BD

<?php 
    define ('MYSQL_HOST', '127.0.0.1');
    define ('MYSQL_USER', 'root');
    define ('MYSQL_PASSWORD', '');
    define ('MYSQL_DB_NAME', 'merda_db');

    class Membros{
        //Adicionar Novo Membro
        public function add(){
            try {
                $PDO = new PDO('mysql:host=' . MYSQL_HOST . ';dbname=' . MYSQL_DB_NAME, MYSQL_USER, MYSQL_PASSWORD);
                $PDO->exec("set names utf8");

                $nome = $_POST['txtnome'];
                $apelido = $_POST['txtapelido'];
                $telemovel = $_POST['txttelemovel'];
                $data_nascimento = $_POST['txtdata'];
                $email = $_POST['txtemail'];
                $nick = $_POST['txtnick'];
                $afiliacao = $_POST['txtafiliacao'];
                $cargo = $_POST['txtcargo'];
                $motivo = $_POST['txtmotivo'];

                $stmt = $PDO->prepare("INSERT INTO membros (nome, apelido, telemovel, data_nascimento, email, nick, afiliacao, cargo, motivo) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)");
                $stmt->execute([$nome, $apelido, $telemovel, $data_nascimento, $email, $nick, $afiliacao, $cargo, $motivo]);

                $stmt = null;
            }
            catch (Exception $e) {
                echo 'Erro ao conectar com o MySQL: ' . $e->getMessage();
            }
        }
    }
?>

 

O que eu quero é, que no caso de o campo no form não ser obrigatório ele permita inserir igual na base de dados.

Edited by DMBoss

DMBoss aka KingOfShadows

Share this post


Link to post
Share on other sites
M6

Por "inserir igual na base de dados" queres dizer utilizar o valor padrão que o campo tem na BD?

Se sim, então basta no comando insert omitires o campo e, obviamente, o valor.


10 REM Generation 48K!
20 INPUT "URL:", A$
30 IF A$(1 TO 4) = "HTTP" THEN PRINT "400 Bad Request": GOTO 50
40 PRINT "404 Not Found"
50 PRINT "./M6 @ Portugal a Programar."

 

Share this post


Link to post
Share on other sites
DMBoss

Obrigado pela dica M6.

Consegui resolver de outra maneira.

 

Agora tenho outro problema, estou a alojar o site no servidor 000webhost.com, não me dá nenhum erro mas simplesmente não adiciona os dados inseridos na base de dados.

Alguem já teve o mesmo problema ou sabe com resolver o mesmo?

Edited by DMBoss

DMBoss aka KingOfShadows

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×
×
  • Create New...

Important Information

By using this site you accept our Terms of Use and Privacy Policy. We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.