6
I have the following code, 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, recording the variables in the database. Since I put this code alone in a file, all these variables are in external files. The structure looks something like this:
index.php
-> form where you take the values of the variables
script.php
-> where variables are declared, and some mathematical operations are performed. p.ex:
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 called by index.php
in action
of form
, in which is called the script.php
(in the head
):
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 archive prepare.php
, which includes variables in the bank (already with modifications of script.php
).
So following a few tutorials, and a few questions from here, I tried so:
<?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 doesn’t record in the bank, and it doesn’t return any errors... What I’m doing wrong?
The autocommit is connected?
– Guilherme Nascimento
It was my impression that the
prepare
?– Edilson
Ah @Edilson... my bad... already put the
prepare
, but I’m still getting some mistakes, but I think that was the main thing... I’ll just check here and return. Thanks.– gustavox
@That’s what Edilson was... I was using
$sql
instead of$stmt = $mysqli->prepare
... thanks, put a response there.– gustavox
Going
Editar
the answer below, to prevent the page from extending too much, and I’m glad I helped.– Edilson
Okay @Edilson, thanks so much! Hugs.
– gustavox
Ready, I thought better edit, and enjoy the wave, because sometimes the
commit
causes the information not to be saved permanently in the database/table. Just choose the answer to close the question.– Edilson
@Edilson did not understand, the commit "sometimes". In fact, the commit is used when the autocommit is off so that it is possible to use rollbacks, that is, if you run multiple querys and one fails, you can rollback all previous ones and prevent updates and partial inserts from entering the bank. The commit only works when autocommit is off, because if you use it on then it’s just redundancy :)
– Guilherme Nascimento