Problems using INSERT in PHP (mysqli)

Asked

Viewed 299 times

0

i (beginner in the field of php development) am having problems inserting data using the code below:

    <?php

$conn = new mysqli("zz", "zz", "zz", "zz"); 
// Alterei a string de conexão por questão de segurança

if ($conn->connect_error) {
  echo "Error: " .$conn->connect_error;
}

$login = 'user';
$senha = '12345';
$stmt = $conn->prepare("INSERT INTO tbl_usuario (login,senha) VALUES (?, ?)");
$stmt->execute();
?>

It runs normally without error, but when I check my database, no data was actually included :/. Someone could help me?

Follows print of the bank: inserir a descrição da imagem aqui

1 answer

1

After creating the Prepared Statement need to do the bind of values:

$stmt->bind_param("ss", $login, $senha);

Otherwise nothing is executed:

<?php
    $conn = new mysqli("zz", "zz", "zz", "zz"); 
    // Alterei a string de conexão por questão de segurança

    if ($conn->connect_error) {
      echo "Error: " .$conn->connect_error;
    }

    $login = "user";
    $senha = "12345";
    $stmt = $conn->prepare("INSERT INTO tbl_usuario (login,senha) VALUES (?, ?)");
    $stmt->bind_param("ss", $login, $senha);
    $stmt->execute();
?>
  • I had omitted a part of my code unintentionally (actually, I left it as a comment, and it was just the bind param: $stmt->bind_param('ss', $login, $pass); Precisely with such a command that does not present me any error and even so does not include :/

  • But does it solve your problem? Or does it remain?

  • Continues with the problem. There is another way to include data without using the param bind?

  • Try placing the values of $login and $senha quotation marks instead of quotes. Answer edited with this correction.

  • I was able to solve the problem :), when trying to save with '?' via that stored normally, soon I realized that it was a problem in the variable. I checked the code again and saw that the problem was actually in passing the variable name in the bind_param parameter (I passed as $pass and the variable was $password). I fixed it and now it’s storing normally :D. Thanks for your attention and help! Hugs

Browser other questions tagged

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