Mysqli INSERT error

Asked

Viewed 245 times

-1

I am not able to identify the error. I have always set up structures like this, but now this error appears.

Parse error: syntax error, Unexpected '' (T_ENCAPSED_AND_WHITESPACE), expecting '-' or Identifier (T_STRING) or variable (T_VARIABLE) or number (T_NUM_STRING)

$inserir = $conexao->query("INSERT INTO cadastro (nome, sobrenome, email, senha, contrata, trabalha, status) VALUE (
          $_POST['nome'],
          $_POST['sobrenome'],
          $_POST['email'],
          password_hash($_POST['senha'], PASSWORD_DEFAULT)."\n",
          $_POST['contrata'],
          $_POST['trabalha'],
          'N'
    )");

I AM USING THESE VERSIONS:

PHP = 7.1.6

Mysql = 5.7.11

  • @Inkeliz Different subjects, different mistakes.

  • Trade this piece PASSWORD_DEFAULT)."\n", for PASSWORD_DEFAULT),

  • @rray Error remains.

  • Log this one of yours insert, copy and run in PHP Myadmin or Mysql console itself to more easily identify the error that is returning...

  • @wBB How to mount the log?

  • You save your entire SQL sentence in a variable and have it printed on the same screen, with echo: $sql = "INSERT INTO cadastro (nome, sobrenome, email, senha, contrata, trabalha, status) VALUE (
 $_POST['nome'],
 $_POST['sobrenome'],
 $_POST['email'],
 password_hash($_POST['senha'], PASSWORD_DEFAULT)."\n",
 $_POST['contrata'],
 $_POST['trabalha'],
 'N'
 )" and executes echo $sql; then. Then you will see exactly what is sending to the database.

  • 1

    can also print on console: echo '<script>console.log('.$sql.')</script>';

  • Try to echo the mounted sql to see the result

  • I think your query is wrong, no? is VALUES plural

  • And the concatenation of the arrays inside the double quotes looks like this: {$_POST['name']}

  • Please do not duplicate questions, this is totally wrong to do in the community. Thank you for understanding.

Show 6 more comments

1 answer

2


Concatenation of arrays inside double quotes is incorrect. PHP does not understand functions and arrays that are inside double quotes as variables. The correct way is to concatenate with keys: {$_POST['chave']} or so: "string".$_POST['chave']."string";

Another observation, your query is as VALUE and the correct one is VALUES in the plural.

Upshot:

$senha = password_hash($_POST['senha'], PASSWORD_DEFAULT);
$sql = "INSERT INTO cadastro (nome, sobrenome, email, senha, contrata, trabalha, status) VALUES (
       {$_POST['nome']},
       {$_POST['sobrenome']},
       {$_POST['email']},
       $senha,
       {$_POST['contrata']},
       {$_POST['trabalha']},
       'N'
      )";

    echo $sql;

Follows Here a link to the Sandbox

Browser other questions tagged

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