Error sending form data

Asked

Viewed 115 times

1

I am learning mysql and php, but is giving an error in the form, where even having been filled in it keeps asking to fill in the fields and does not record 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!!";
            }
    }
?>

  • 1

    NAY use the family functions mysql_*. Use the ones of the family mysqli_* or PDO. This function family has been discontinued and can be removed in the near future. And you should escape values before they go to the database. Read this answer to another question: http://answall.com/questions/579/por-que-n%C3%a3o-devo-usar-fun%C3%A7%C3%b5es-do tipo-mysql? lq=1

1 answer

3

Change that line:

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

To:

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

This way each variable will be checked by Empty. The previous if just checked $nome was empty and if $idade, $telefone or $editor1 is different from empty that would enter if because all logical tests are connected via OR(||) or is a valid being true the condition is satisfied.

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

Why should we not use mysql type functions_*?

Mysqli vs PDO - which is the most recommended to use?

How to prevent SQL code injection into my PHP code

  • I put your code and now this error: Fatal error: Call to Undefined Function msql_query() in E: sites htdocs progdesenv2 admin index.php on line 34 in this code snippet: $inserts("INSERT INTO formulary (name, age, phone, editor1) VALUES ('$name', '$age', '$phone', '$editor1' )");

  • @Estenioribeironobrega, is error syntax, the correct is mysql_query and not msql_query ta missing a y in function name :P

  • aeee worked, just one more question, if I want to play this data in html for the client to see the entered data I do as?

  • @Estenioribeironobrega, can give an echo in the variables $nome, $telefone etc or take the id inserted with mysql_insert_id and make a select with the database data.

  • @Estenioribeironobrega, this one question shows how to convert amysql_* code to mysqli

Browser other questions tagged

You are not signed in. Login or sign up in order to post.