Mysqli INSERT INTO Does not work

Asked

Viewed 3,379 times

-1

I have a code that inserts records into the database, it is not displaying any error, it is simply not sending the data to the database

the code worked previously only that as mysql_* became obsolete I had to update, and then it stopped working

here is the code

<?php
include "../../lib/inc_con.php";
session_start();
$mesa = $_POST['mesa'];
$tamanho = $_POST['tamanho'];
$quantidade = $_POST['qtd'];
$adicional = implode(',', $_POST['adicional']);
$hiddentotal = $_POST['hiddentotal'];
date_default_timezone_set('America/Sao_Paulo'); 
$mysqldata = new DateTime(); 
$data = $mysqldata->format(DateTime::ISO8601);
$produto_id1 = utf8_encode($_POST['produto_id1']);
$atendente_id = $_SESSION['id'];
$observacao = $_POST['observacao'];
$produzido = '0';
$valortotal = $quantidade * $hiddentotal;
$asplo = $_POST['asplo'];


$inserir = $conexao->query("INSERT INTO pedidos (mesa, tamanho, qtd, adicional, valortotal, data, produto_id1, atendente_id, produzido, observacao, asplo) 
values ('$mesa', '$tamanho', '$quantidade', '$adicional', '$valortotal', '$data', '$produto_id1', '$atendente_id', '$produzido', '$observacao', '$asplo'") or die (mysqli_error()); 
if($inserir){
    echo "Inserido";
} else {
    echo $inserir->error;
}

?>

inc_cong.php file

<?php

error_reporting(0);
ini_set(“display_errors”, 0 );

$hostname = "localhost";
$username = "root";
$password = "";
$dbdatabase = "moclient";

$conexao = new mysqli($hostname, $username, $password, $dbdatabase);
if($conexao->connect_error){
    echo "Conexao:";?><span class="ls-tag-danger">Erro!</span>
<?php
}else{ 
    echo "Conexao:";?><span class="ls-tag-success">OK!</span>
<?php }

?>
  • If you echo var $insert or copy the query and echo the query and directly test it performs the Insert?

  • Post the contents of inc_con.php.

  • <?php error_reporting(0); ini_set("display_errors", 0 ); $hostname = "localhost"; $username = "root"; $password = ""; $dbdatabase = "moclient"; $connected = new mysqli($hostname, $username, $password, $dbdatabase); if($connect_error){ echo "Connected:";? ><span class="ls-tag-Danger">Error! </span> <? php }Else{ echo "Connected:";? ><span class="ls-tag-Success">OK! </span> <? php } ?>

  • Although it is not returning errors, it even returns some message ?

  • Return Connection: OK!

  • I’m talking about this condition here if($inserir), returns inserted ? Or returns the error ?

  • Returns nothing

  • 2

    Alfredo, it would be nice if we had a crystal ball to guess what’s going on. but we don’t.. That’s why there’s such a "feedback".. vc just returns vague things... If you want help, help us help you. but be aware that this is not a support and help center. Your job is your responsibility.

  • @Alfredolima made the changes as I suggested in my reply http://answall.com/a/95832/3635 ?

Show 4 more comments

2 answers

2

When you’re in development use it like this:

<?php
error_reporting(E_ALL|E_STRICT); //Irá mostrar qualquer erro

$hostname = "localhost";
$username = "root";
$password = "";
$dbdatabase = "moclient";

$conexao = new mysqli($hostname, $username, $password, $dbdatabase);
if($conexao->connect_error){
    echo "Conexao:";?><span class="ls-tag-danger">Erro!</span>
<?php
}else{ 
    echo "Conexao:";?><span class="ls-tag-success">OK!</span>
<?php }

?>

In case you said you return:

Return Connection: OK!

But this is just the connection, you should see the error of this point, you are using two error checks:

$inserir = $conexao->query("INSERT INTO pedidos (mesa, tamanho, qtd, adicional, valortotal, data, produto_id1, atendente_id, produzido, observacao, asplo) 
values ('$mesa', '$tamanho', '$quantidade', '$adicional', '$valortotal', '$data', '$produto_id1', '$atendente_id', '$produzido', '$observacao', '$asplo'") or die (mysqli_error()); //Erro aqui, abaixo não é executado
if($inserir){
    echo "Inserido";
} else {
    echo '', $inserir->error;//Este erro
}

You used the or no need, and another detail you’re using the object-oriented format, so maybe mysqli_error is coming empty.

Use like this:

$inserir = $conexao->query("INSERT INTO pedidos (mesa, tamanho, qtd, adicional, valortotal, data, produto_id1, atendente_id, produzido, observacao, asplo) 
values ('$mesa', '$tamanho', '$quantidade', '$adicional', '$valortotal', '$data', '$produto_id1', '$atendente_id', '$produzido', '$observacao', '$asplo'");

if($inserir){
    $inserir->close(); //Fecha a execução da query query
    echo "Inserido";
} else {
    echo 'Erro: ', $inserir->error;//Exibe o erro
}

I recommend using exactly how the documentation describes:

(I recommend the documentation in English because some pages in Portuguese have wrong information - /a/67950/3635)

When you send the script to production (pro site) then you can use it like this:

<?php
error_reporting(E_ALL|E_STRICT); //Irá mostrar qualquer erro

Additional

The session_start(); must come before any echo, print or text, then do so:

<?php
session_start();
include "../../lib/inc_con.php";

-1

Commit is set to true?

If not, possibly this is the solution to your problem. And in this case, use $mysqli->query('SET AUTOCOMMIT = 1');

  • What is an AUTOCOMMIT?

  • Without autocommit you must commit the execution of the script to make it persist in the database. After running Insert you should put : $mysqli->commit(); if not configured to perform autocommit.

  • It would actually be $mysqli->autocommit(true/false) for your case, the method/declaration commit makes permanent any changes made to the database during a transaction, which is where they are usually most useful. And they’re usually used in queries where we don’t want the data to be changed permanently once we input it, using the rollback we can undo the changes, depending on the state of the commit.

Browser other questions tagged

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