Error sending form data

1

I am learning mysql and php, but it is giving an error in the form, where even having been filled it still asks to fill in the fields and does not register anything in the database table

<?php
$db = "progdesenv";
@mysql_connect("localhost", "root", "") or trigger_error(mysql_error(),E_USER_ERROR);
mysql_select_db ($db); 
 ?>
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Untitled Document</title>
<script src="ckeditor/ckeditor.js"></script>

</head>

<body>
<form  method="post" enctype="multipart/form-data">
   Nome:<input type="text" name="nome"><br>
   Idade:<input type="text" name="idade"><br>
   Telefone:<input type="text" name="telefone"><br>
   Mensagem<textarea class="ckeditor" name="editor1" cols="30" rows="10" ></textarea>
   <input type="hidden" name="acao" value="enviado">
   <input type="submit" value="Enviar Informações">
</form>
<?php
if(isset($_POST['acao']) && $_POST['acao'] == 'enviado'){

        $nome = $_POST['nome'];
        $idade = $_POST['idade'];
        $telefone = $_POST['telefone'];
        $editor1 = $_POST['editor1'];

        if(empty($nome) || ($idade) || ($telefone) || ($editor1)){
                echo "Preencha os campos corretamente";
            }else{
                $insereDados = msql_query("INSERT INTO formulario (nome, idade, telefone, editor1) VALUES ('$nome', '$idade', '$telefone', '$editor1' )");  
                echo "Enviado com sucesso!!";
            }
    }
?>

    
asked by anonymous 23.04.2015 / 00:12

1 answer

3

Change this line:

if(empty($nome) || ($idade) || ($telefone) || ($editor1)){

To:

if(empty($nome) || empty($idade) || empty($telefone) || empty($editor1)){

In this way each variable will be checked by empty. The previous if only checked $nome was empty and if $idade , $telefone or $editor1 were different from empty it would go into if all logic tests are connected through the OR ( || ) or a value being true the condition is satisfied.

As you are still learning, do not use the mysql_ * functions because they are already obsolete, prefer the PDO or mysqli, recommended reading:

Why should not we use functions of type mysql_ *?

MySQL vs PDO - Which is the most recommended to use?

How to prevent SQL injection in my PHP code

    
23.04.2015 / 00:23