I have the following code, in which I want to use prepared statments
:
prepare.php
:
<?php
include "../conex.php"; // conecta
mysqli_set_charset($mysqli,"utf8"); // Transforma em UTF8 pra gravar acentos no servidor
// Inserção de variáveis do formulário no banco de dados
$sql = "INSERT INTO tabela (userid, username, meses, percdev)
VALUES ('$user_id', '$user_name', '$meses', '$percdev')";
if ($mysqli->query($sql) === TRUE) {
echo "New record created successfully";
} else {
echo "Error: " . $sql . "<br>" . $mysqli->error;
}
$mysqli->close();
This code works correctly by writing the variables to the database. Since I put this code alone in a file, all of these variables are in external files. The structure looks something like this:
index.php
- > form where you get the values of the variables
script.php
- > where variables are declared, and some mathematical operations are performed. e.g.
setlocale(LC_ALL, 'pt_BR', 'pt_BR.utf-8', 'pt_BR.utf-8', 'portuguese');
date_default_timezone_set('America/Sao_Paulo');
include "datas/tempo.php";
$percDevIns = empty($_POST['Tpercentdev']) ? NULL : $_POST['Tpercentdev'];
$valorIns = (788.00 * $percDevIns) / 100;
saida.php
- > The file named by index.php
in action
of form
, in which script.php
(in head
) is called:
include "../../models/scripts/script.php";
// todo o HTML e PHP da página de saída
And at the end of this file is the call to the file prepare.php
, which includes the variables in the database (already with script.php
modifications).
So following some tutorials, and some questions from here, I tried like this:
<?php
include "../conex.php";
// Transforma em UTF8 pra gravar acentos no servidor
mysqli_set_charset($mysqli,"utf8");
// Inserção de variáveis do formulário no banco de dados
$sql = "INSERT INTO tabela (userid, username, meses, percdev)
VALUES (?, ?, ?, ?)";
$stmt->bind_param('isii', $user_id, $user_name, $meses, $percdev);
$stmt->execute();
printf("Error: %s.\n", $stmt->error);
$stmt->close();
$mysqli->close();
But it does not write to the bank, and it does not return any errors to me ... What am I doing wrong?