Error sending the form by POST

1

I'm having a strange error. It is not quite a bug because the form submits the form but it changes one of the fields to 0 or 1. Here is the code.

<form method="POST" enctype="multipart/form-data">
   <div class="form-group col-lg-3" style="padding-left:0;">
      <label for="predifinicao">Nome predefinido</label>
      <input class="form-control" type="text" value="<?=$nome; ?>" name="titulo" id="predifinicao">
   </div>
   <div class="form-group col-lg-3" style="padding-left:0;">
      <label for="horario">Texto do Horário</label>
      <input class="form-control" type="text" value="<?=$horario; ?>" name="horario" id="horario">
   </div>
   <div class="form-group col-lg-6" style="padding-left:0;">
      <label for="capa">Imagem do programa</label>
      <div class="input-group">
         <input class="form-control" type="file" name="imagem" id="capa">
         <span class="input-group-addon label-primary" style="cursor:pointer;border:0;" data-target="#modal_preview_radio" data-toggle="modal"><i class="fa fa-eye"></i></span>
         <span class="input-group-addon label-danger" style="cursor:pointer;border:0;" title="Irá apagar a imagem atual do programa!" data-toggle="tooltip"><i class="fa fa-trash"></i></span>
      </div>
   </div>
   <div class="form-group">
      <input type="submit" class="btn btn-primary" name="submit" value="Alterar definições" style="width:200px;" />
   </div>
</form>

And now PHP

if(isset($_POST['submit'])){
    $nome=$_POST['titulo'];
    echo $nome;
    $horario=$_POST['horario'];
    $update=mysqli_query($db, "UPDATE radio_predefinicao SET titulo = '$nome' AND horario = '$horario'") or die("Erro.");
    move_uploaded_file($_FILES['imagem']['tmp_name'], "../radio/programas/default/capa.jpg");
    echo "<script>window.location.href='index.php?pg=12';</script>";
}

The field $nome returns well in echo made there but it puts the case are letters and 1 case are numbers. What do I have wrong?

    
asked by anonymous 18.05.2018 / 11:46

1 answer

1

As I have reported

<?php
        include_once($_SERVER['DOCUMENT_ROOT']."/cobranca/plano/funcoes.php");
        include_once($_SERVER['DOCUMENT_ROOT']."/cobranca/utils/global_funcoes.php");

        $dadosValido = true;
        $arrayMensagemErros = array();

        $plano = (isset($_GET['id'])) ? carregarDadosPlanoBanco($_GET['id']) : carregarDadosPlanoSessao();

        if ($_SERVER["REQUEST_METHOD"] == "POST") {


            if(!validarCamposPreenchidosPlano($arrayMensagemErros)) {

                if (isset($_POST['id'])) {
                    if (!is_numeric($_POST['id'])) {
                        die("O código é inválido");
                    }
                    $plano['id'] = $_POST['id'];
                }

                $plano['tipo_cobranca'] = limpaCampo($_POST['tipo_cobranca']);
                $plano['valor'] = limpaCampo($_POST['valor']);
                $plano['nome'] = limpaCampo($_POST['nome']);

                empty($plano['id']) ? inserirPlano($plano) : editarPlano($plano);
                header("Location: list.php");
            }
    }

?>
<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <title>Cadastro de planos de cobrança</title>
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <link rel="stylesheet" type="text/css" media="screen" href="../css/style.css" />
    <script src="main.js"></script>
</head>
<body>
    <form action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]); ?>" method="POST">
        <div style="width: 600px;" class="elementoCentralizado">
            <h1 class="elementoCentralizado">Cadastro dos planos de cobrança</h1>
            <table border="1" style="margin-left: auto;margin-right: auto">
                <tr>
                    <td>
                        <label for="nome">Nome:</label>
                    </td>
                    <td colspan="2">
                        <input type="text" name="nome" value="<?php echo preencheValue($plano['nome']);?>"/>
                        <span><?php  echo retornaChave($arrayMensagemErros, 'nome'); ?></span>
                    </td>
                </tr>
                <tr>
                    <td>
                        <label for="tipo_cobranca">Tipo de cobrança:</label>
                    </td>
                    <td colspan="2">
                        <select name="tipo_cobranca" id="tipo_cobranca" value="<?php echo preencheValue($plano['nome']);?>">
                            <option value="M" <?php echo optionSelector($plano['tipo_cobranca'], "M"); ?>>Mensal</option>
                            <option value="A" <?php echo optionSelector($plano['tipo_cobranca'], "A"); ?>>Anual</option>
                        </select>
                        <span><?php echo retornaChave($arrayMensagemErros, 'tipo_cobranca'); ?></span>
                    </td>
                </tr>
                <tr>
                    <td>
                        <label for="valor">Valor:</label>
                    </td>
                    <td colspan="2">
                        <input type="text" name="valor" value="<?php echo preencheValue($plano['valor']);?>">
                        <span><?php echo retornaChave($arrayMensagemErros, 'valor'); ?></span>
                    </td>
                </tr>
                <tr>
                    <td class="elementoCentralizado">
                        <input type="reset" value="Limpar">
                    </td>
                    <td class="elementoCentralizado">
                        <div class="botao">
                            <a href="list.php">Cancelar</a>
                        </div>
                    </td>
                    <td class="elementoCentralizado">
                        <?php if (!empty($plano['id'])) :
                                echo "<input type='hidden' name='id' value='{$plano['id']}'>";
                              endif; ?>
                        <input type="submit" value="Salvar">
                    </td>
                </tr>
            </table>
        </div>
    </form>
</body>
</html>

Functions to search the database and if there is any validation problem, keep the data in session

function carregarDadosPlanoBanco($codigo) {

    $conexao = conectarAoBanco();
    $sql = "SELECT * FROM planos WHERE id = " . $codigo . ";";
    $result = mysqli_query($conexao, $sql);
    return mysqli_fetch_assoc($result);
}

function carregarDadosPlanoSessao() {
    $plano['id'] = isset($_POST['id']) ? $_POST['id'] : "";
    $plano['nome'] = isset($_POST['nome']) ? $_POST['nome'] : "";
    $plano['tipo_cobranca'] = isset($_POST['tipo_cobranca']) ? $_POST['tipo_cobranca'] : "";
    $plano['valor'] = isset($_POST['valor']) ? $_POST['valor'] : "";
    return $plano;
}

Method to validate the data, detail, step the vector by reference so as not to lose it and keep the values.

function validarCamposPreenchidosPlano(&$arrayMensagemErros) {

    $dadoInvalido = false;

    if (empty($_POST['nome'])) {
        $arrayMensagemErros['nome'] = "Preencha o campo Nome";
        $dadoInvalido = true;
    }

    if (empty($_POST['tipo_cobranca'])) {
        $arrayMensagemErros['tipo_cobranca'] = "Preencha o campo Tipo de cobrança";
        $dadoInvalido = true;
    }

    if (empty($_POST['valor'])) {
        $arrayMensagemErros['valor'] = "Preencha o campo Valor";
        $dadoInvalido = true;
    }

    return $dadoInvalido;
}

And finally, if the insertion or editing is correct, I use the locate method

header("Location: list.php");
    
18.05.2018 / 12:01