Database does not work

0

I've never made a form, so please forgive me if you're absurdly wrong.

Here is the bank with the table:

CREATE DATABASE db_formacao;
USE db_formacao;

CREATE TABLE  'db_formacao'.'formacoes' (
'ID' INT( 5 ) NOT NULL AUTO_INCREMENT ,
'NOME' VARCHAR( 255 ) NOT NULL ,
'OBJETIVO' TEXT NOT NULL ,
'CARGA' INT( 5 ) NOT NULL ,
'CONTEUDO' TEXT NOT NULL ,
PRIMARY KEY (  'ID' )
) ENGINE = MYISAM ;

Already here, on the page itself that the form is being built I try to make the connection:

<?php
    $conn = new mysqli ("localhost", "root", "", "db_formacoes");

    $nome = $_POST['nome'];
    $objetivo = $_POST['objetivo'];
    $conteudo = $_POST['conteudo'];
    $carga = $_POST['carga'];

    $squery = "INSERT INTO formacoes (nome, objetivo, conteudo, carga) VALUES('$nome','$objetivo','$conteudo', '$carga' )";
    $resultado = mysqli_query($conn,$squery);


    if(!mysqli_query($conn, $squery)){  
        echo 'Opa! Não conseguimos nos conectar ao banco de dados. '. mysqli_error($conn);
    }else{
        echo 'Operação realizada com sucesso';
    }

    mysqli_close($conn);
?>

When I click on the save form button nothing happens to indicate that the information has been saved and I'm checking it by doing a select in phpMyAdmin. Here is the result:

    
asked by anonymous 17.07.2017 / 15:31

1 answer

2

To connect the bank using mysqli_query , according to the PHP documentation, there are two ways:

  • Procedural: mysql_query
  • Object Oriented: mysqli_query
  

Note: Only procedural style uses the feature returned by mysqli_connect () or < mysqli_init ()

To connect procedural style:

$con = mysql_connect('host', 'usuario', 'senha')
or die('Erro ao conectar: ' . mysql_error());
mysql_select_db('banco') or die('Banco não encontrado');

/* Realiza um *SELECT* no banco */
$sql = "SELECT * FROM 'usuario' LIMIT 5";
$result = mysql_query($sql) or die('Erro no SQL: ' . mysql_error());

//Exibe os dados
echo "<table>\n";
while ($line = mysql_fetch_array($result, MYSQL_ASSOC)) {
    echo "\t<tr>\n";
    foreach ($line as $col_value) {
        echo "\t\t<td>$col_value</td>\n";
    }
    echo "\t</tr>\n";
}
echo "</table>\n";

// Libera um resultado da memória
mysql_free_result($result);

// Fecha conexão
mysql_close($result);

To connect the database using object-oriented style:

$con = new mysqli('host', 'usuario', 'senha', 'banco');

if (mysqli_connect_errno()) {
    printf("Falha na conexão ao banco: %s\n", mysqli_connect_error());
    exit();
}

$sql = "SELECT * FROM 'usuario' LIMIT 5";
$query = mysqli_query($con, $sql);

if ($query) {
    // Pode fazer assim
    while ($usuario = mysqli_fetch_assoc($query)) {
        // Exibe um link com a notícia
        echo $usuario['nome'] . ' - ' . $usuario['email'];
        echo '<br/>';
    } // fim while

    // Ou assim
    while ($usuario = mysqli_fetch_object($query)) {
        // Exibe um link com a notícia
        echo $usuario->nome . ' - ' . $usuario->email;
        echo '<br/>';
    }
} 

echo 'Total de notícias: ' . mysqli_num_rows($query);

Here there is a tutorial on how to perform queries using either the procedural form, and object oriented. Any questions just comment on the answer.

    
17.07.2017 / 16:05