Error, php does not insert data into database

Asked

Viewed 38 times

2

People php is returning that is entering the data but in the database it does not insert:

// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
} 
echo "Connected successfully";


  $stmt = $conn->prepare("INSERT INTO perguntas (nome, pergunta, resposta1,resposta2,resposta3,respostaCorreta)
  VALUES (?,?,?,?,?,?)");

  $stmt->bind_param("ssssss",$nomePergunta,$pergunta,$reposta1,$reposta2,$resposta3,$respostaCorreta);

  //Pegando dados via post 
  $nomePergunta  = $_POST["nome"];
  $pergunta  = $_POST["pergunta"];
   $resposta1  = $_POST["resposta1"];
    $resposta2  = $_POST["resposta2"];
     $resposta3  = $_POST["resposta3"];
      $respostaCorreta = $_POST["resposta"];

  $stmt->execute();


  echo "dados inseridos com sucesso";
  • Tries to change the order $startName = $_POST["name"]; ................. $stmt = $Conn->prepare("INSERT INTO questions (name, question, answer1,answer2,answer3,answerCorrects) VALUES (?,? ,? ,? ,? ,? )"); $stmt->bind_param("ssssss",$nameQuest,$question,$reply1,$reply2,$reply3,$replyCorrects);

  • 1

    kkk, the order certainly influences the Insert, but if you don’t put an if no execute(), even if for another reason you do not enter will return "successfully entered data";

  • It is not missing to do a query to send to the database?

  • If you take the instruction and run it straight into the database, what error occurs ?

1 answer

0

If you try to do so:

// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
} 
echo "Connected successfully";

//Pegando dados via post 
  $nomePergunta  = $_POST["nome"];
  $pergunta  = $_POST["pergunta"];
  $resposta1  = $_POST["resposta1"];
  $resposta2  = $_POST["resposta2"];
  $resposta3  = $_POST["resposta3"];
  $respostaCorreta = $_POST["resposta"];


$sql = ""INSERT INTO perguntas (nome, pergunta, resposta1,resposta2,resposta3,respostaCorreta)
  VALUES ("ssssss",'$nomePergunta','$pergunta','$reposta1','$reposta2','$resposta3','$respostaCorreta')"; //Coloquei aspas porque acredito que é string seus campos


if (mysqli_query($conn, $sql)) {
      echo "Pergunta adicionada com sucesso!";
} else {
      echo "Error: " . $sql . "<br>" . mysqli_error($conn);
}
mysqli_close($conn);

Browser other questions tagged

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