FORM HTML5 and PHP all on the same page [closed]

-1

How to process these tasks before the form is written to the database?

CASE 01: How do you prevent visitor from being repeated in visitor ?

CASE 02: How to determine winner or draw?

CASE 03: Given the CASE 02 , how to write it in the hidden field?

CASE 04: If you have a winner, how do you record it in the hidden field result ?

CASE 05: How to display the FORM successfully saved message, only if all requests are met?

<!DOCTYPE html>
<html>
<?php
include("bd.php"); //Conectando com o MySQL
?>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

    <title>Registro de resultado dos jogos</title>

    <link type="text/css" rel="stylesheet" media="screen" href="estilos.css" />
</head>
<body>
    <div id="site"> 
        <form method="post" id="insere_jgfeito" align="center" enctype="multipart/form-data"> <!-- ÍNICIO DO USUÁRIO PREENCHENDO FORMULÁRIO -->

            <td>
            <tr>
                <span><strong>Inserir Resultados<strong></span>
            </br>
            </tr>

            <tr>
            <td>
<select required name="campeonato"/>
    <option value="" disabled="disabled" selected="selected">Campeonato</option>
    <?php
        $cst_campeonato = mysql_query("SELECT 'id', 'campeonato' FROM 'tbl_campeonatos' WHERE 'status' = 1 ORDER BY 'id' ASC");
            while($campeonatos = mysql_fetch_array($cst_campeonato)){ ?>
    <option value="<?php echo $campeonatos["id"];?>"><tr><?php echo $campeonatos["campeonato"];?></tr></option>
    <?php } ?>
<select>            </td></br>
            <td>
            <label for="data-atual">Realizado em...</label>
            <input type="date" name="data-atual" id="data-atual" value="<?=date('d/m/Y')?>" required>
            </td>
            </tr>
            </td></br>
            <tr>
            <td>
            <select required name="mandante"/>
                <option value="" disabled="disabled" selected="selected">Mandante</option>
                <?php
                    $cst_competidor = mysql_query("SELECT 'tbl_jogadores'.'tecnico' AS tecnico, 'tbl_competidor'.'nome' AS nome FROM 'tbl_competidor' INNER JOIN 'tbl_jogadores' ON 'tbl_competidor'.'id_competidor' = 'tbl_jogadores'.'tecnico' WHERE 'tbl_competidor'.'status' = 1 GROUP BY 'tbl_jogadores'.'tecnico' ORDER BY 'tbl_competidor'.'nome' ASC");
                    while($tecnicos = mysql_fetch_array($cst_competidor)){ ?>
                <option value="<?php echo $tecnicos["tecnico"];?>"><tr><?php echo $tecnicos["nome"];?></tr></option>
                <?php } ?>
            <select>
            <input type="number" min="0" max="9" name="mscore" required="required">
            </td>
            <td>
            <input type="number" min="0" max="9" name="vscore" required="required">
            <select required name="visitante"/>
                <option value="" disabled="disabled" selected="selected">Visitante</option>
                <?php
                    $cst_competidor = mysql_query("SELECT 'tbl_jogadores'.'tecnico' AS tecnico, 'tbl_competidor'.'nome' AS nome FROM 'tbl_competidor' INNER JOIN 'tbl_jogadores' ON 'tbl_competidor'.'id_competidor' = 'tbl_jogadores'.'tecnico' WHERE 'tbl_competidor'.'status' = 1 GROUP BY 'tbl_jogadores'.'tecnico' ORDER BY 'tbl_competidor'.'nome' ASC");
                    while($tecnicos = mysql_fetch_array($cst_competidor)){ ?>
                <option value="<?php echo $tecnicos["tecnico"];?>"><tr><?php echo $tecnicos["nome"];?></tr></option>
                <?php } ?>
            <select>
            </td>
            </tr>   
            <input type="hidden" value="?" name="tipo"/>
            <input type="hidden" value="?" name="resultado"/>
            <input type="hidden" value="1" name="status"/>
            <input type="hidden" name="acao" value="enviado" />
            <button type="submit">Gravar</button>
        </form> <!-- FIM DO FORMULÁRIO -->
    </div>
<?php 
/* RECEBENDO OS DADOS PREENCHIDOS DO FORMULÁRIO!
OBS: Para fins didáticos, todas as variavés utilizarão o prefixo "rcb_" referente a "recebe valor de" */

$rcb_campeonato = $_POST ["campeonato"];    // Captura o (select) com nome: "campeonato" referente ao "id_campeonato"
$rcb_data_atual = $_POST ["data-atual"];    // Traz (input date) com nome: "data-atual" 
$rcb_mandante   = $_POST ["mandante"];      // Captura o participante de nome: "mandante"
$rcb_mscore = $_POST ["mscore"];            // Recebe número entre 0 e 9 de: "mscore"
$rcb_vscore = $_POST ["vscore"];            // Recebe número entre 0 e 9 de: "vscore"
$rcb_visitante  = $_POST ["visitante"];     // Captura o participante de nome: "mandante"
$rcb_tipo   = $_POST ["tipo"];              // Campo oculto de valores 0=Empate, 1=Vitória do mandante ou 2=Visitante
$rcb_resultado = $_POST ["resultado"];      // Deve gravar o ID vencedor (mandante ou visitante) 
$rcb_status = $_POST ["status"];            // Se tudo estiver OK, grava sempre o campo com valor 1.


//Como gravar esses dados no banco de dados?


//Como referenciar a tabela do banco de dados?


$query = "INSERT INTO 'tbl_jogos' ( 'Null' , 'id_campeonato', 'dt_partida' , 'mandante' , 'visitante' , 'mscore' , 'vscore' , 'tipo' , 'resultado', 'status') 
VALUES ('$rcb_campeonato', '$rcb_data_atual', '$rcb_mandante', '$rcb_visitante', '$rcb_mscore', '$rcb_vscore', '$rcb_tipo', '$rcb_resultado', '$rcb_status')";

echo "Seu resultado foi gravado com sucesso!";
?>
</body>
</html>
    
asked by anonymous 21.08.2018 / 03:59

1 answer

0

CASE 01 : just create a conditional and trigger an error if it happens ...

if($rcb_visitante == $rcb_mandante) {
    echo "O mandante não pode ser igual ao visitante.";
}

CASE 02 : Just add two variables: $vencedor ( string ) and $empate ( boolean ) from one more conditional. Ps: For the following cases, I created more variables to assist in printing: $tipo and $resultado .

$vencedor = $empate = false;
$tipo = $resultado = "?";
if($rcb_mscore == $rcb_vscore) {
   $empate = true;
   $tipo = "0";
} elseif($rcb_mscore > $rcb_vscore) {
   $vencedor = $resultado = $rcb_mandante;
   $tipo = "1";
} else {
   $vencedor = $resultado = $rcb_visitante;
   $tipo = "2";
}

CASE 03 : In order to perform this operation, this validation process must come BEFORE from the form view, so that in the snippet where we type the tipo we can populate it dynamically, for example:

<input type="hidden" value="<?=$tipo?>" name="tipo"/>

CASE 04 : same as the previous step ...

<input type="hidden" value="<?=$resultado?>" name="resultado"/>

CASE 05: At the end of all processes, you must have a variable that indicates that the form has passed the validation process. In this case, I will take the opportunity to put the whole code of how this process would look. Note: I was based solely on the structure you were following in this exercise, but this structure and work method is not a good practice. The ideal is to separate what is server-side from what is client-side, that is, your form response could be an AJAX request. However, by the level of the thing I believe it is not yet time to talk about it, hehe. I recommend learning: Javascript > AJAX > jQuery > jQuery.AJAX + PHP. It will change your view a little on the subject. Anyway, let's code it:

link

However, however, the whole structure of your HTML is full of errors ... Have a little more patience in the studies, I think it is going too fast ... Just a piece of advice As I said, I followed what you were trying to do (with server-side validations), but these validations will only occur when the user submits the form (the page will be reloaded). If you intend to prevent this, your validation must be client-side (Javascript). I can elaborate a response doing the same things with jQuery (to explain it faster, because there is time ... haha).

    
21.08.2018 / 13:58