Error in insert MSQLI [closed]

1

I am changing from mysql_ to mysqli, everything works (update and delete) more in the insert does not save .... where is the error?

insert file:

include("config/con_mysqli.php");


$sigla                  = $_REQUEST['sigla'];
$nome                   = $_REQUEST['nome'];    


mysqli_set_charset($con,"utf8");

$sql = "insert into setores(sigla,nome) values('$sigla','$nome')";



@mysqli_query($con,$sql);



echo json_encode(array(
    'idsetor' => mysqli_insert_id($con),
    'sigla' => $sigla,
    'nome' => $nome
));
    
asked by anonymous 19.07.2017 / 14:22

1 answer

0

First let's go to concepts. You are exiting the procedural mode from mysqli_query to object-oriented. I commented on this case briefly in this question .

The problem is that you are still messing with procedural and object-oriented when working with mysqli_query .

How would it be to work with 'object-oriented mysqli_query:

Connection (Not that yours is wrong)

// Conecta ao banco de dados
$mysqli = new mysqli('127.0.0.1', 'usuario', 'senha', 'banco');

Valuing the connection

// Verifica se ocorreu algum erro
if (mysqli_connect_errno()) {
    die('Não foi possível conectar-se ao banco de dados: ' . 
         mysqli_connect_error());
    exit();
}

Try using the Prepared Statements . Make your life easier

/* Prepared statement, Primeiro parte prepare o sql.
Verifique se não ocorreu erro */
if (!($stmt = $mysqli->prepare("INSERT INTO test(id) VALUES (?)"))) {
    echo "Falha no Prepare: (" . $mysqli->errno . ") " . $mysqli->error;
}

// Segunda parte passe os parâmetros e execute a query
$id = 1;
if (!$stmt->bind_param("i", $id)) {
    echo "Falha no bind_param: (" . $stmt->errno . ") " . $stmt->error;
}

if (!$stmt->execute()) {
    echo "Falha ao executar a query: (" . $stmt->errno . ") " . $stmt->error;
}

Another example . Otherwise, avoid using @ in database queries or anywhere in your code. Omitting errors and warnings only hinders you from producing good code.

I hope that helps you.

    
19.07.2017 / 15:29