Error when registering form in database

Asked

Viewed 90 times

0

I have the following resolution, I am not able to register in the bank. I already changed some things but always appears these errors:

Warning: mysqli_set_charset() expects parameter 1 to be mysqli, resource given in C:\xampp\htdocs\envio\conecta_mysql.inc on line 5

Notice: Trying to get property of non-object in C:\xampp\htdocs\envio\conecta_mysql.inc on line 7

Fatal error: Call to a member function query() on resource in C:\xampp\htdocs\envio\insere.inc on line 17

This is the conecta_mysql.inc:

<?php

$conexao = mysql_connect('localhost', 'root', '', 'formulario');

mysqli_set_charset($conexao, 'utf8');

if ($conexao->connect_error) {
    die("Falha ao realizar a conexão: " . $conexao->connect_error);
}
?>

This is the inserts.inc:

<?php

include 'conecta_mysql.inc';

$n_1             = $_POST['n_1'];
$n_2             = $_POST['n_2'];
$n_3             = $_POST['n_3'];
$consultor01     = $_POST['consultor01'];
$consultor02     = $_POST['consultor02'];
$consultor03     = $_POST['consultor03'];
$total_acordos   = $_POST['total_acordos'];
$total_parcelas  = $_POST['total_parcelas'];

$sql = "INSERT INTO n_acordos VALUES";
$sql .= "('$n_1','$n_2','$n_3','$consultor01','$consultor02','$consultor03','$total_acordos','$total_parcelas')";

if ($conexao->query($sql) === TRUE) {
    echo "Informações atualizadas com sucesso!";
    } else {
    echo "erro: " . $sql . "<br>" . $conexao->error;
    }
    $conexao->close();

?>

2 answers

0

The connection should be "mysqli_connect()", then it should be an object of type mysqli.

  • I was able to fix the connection only if it shows error in the other file--> inserts.inc line 17 --> if ($connectionwith->query($sql) === TRUE) { echo "Updated information successfully!";

  • What is the error message?

  • Notice: Undefined variable: connection in C: xampp htdocs send inserts.inc on line 17 Fatal error: Call to a Member Function query() on null in C: xampp htdocs send inserts.inc on line 17

  • A doubt the file extensions are ". inc"?

0

your problem is precisely in connection

$conexao = mysql_connect('localhost', 'root', '', 'formulario');

mysqli_set_charset($conexao, 'utf8');

Note that you initiate connection by mysql and uses commands from mysqli. Doesn’t work

follows an ex of how to use:

$host = 'localhost';
$user = 'usuario';
$pass = 'senha';
$db   = 'nome_do_banco';

// conexão e seleção do banco de dados
$con = mysqlI_connect($host, $user, $pass, $db);

// executa a consulta
$sql = "SELECT * FROM funcionarios ORDER BY nome";
$res = mysqli_query($con, $sql);

// conta o número de registros
$total = mysqli_num_rows($res);

echo "<p>Total de Resultados: " . $total . "</p>";

// loop pelos registros
while ($f = mysqli_fetch_array($res))
{
    echo "<p>" . $f['nome'] . " | " . 
         $f['email'] . " | " . 
         $f['salario'] . " | " . 
         date('d/m/Y', strtotime($f['nascimento'])) . "</p>";
}

// fecha a conexão
mysqli_close($con);
  • i still have error file inserts.inc line 17 --> if ($connected->query($sql) === TRUE) { echo "Information updated successfully!";

Browser other questions tagged

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