How to register and consult data in the bank of Wordpress?

2

On my site, there is a section called "My Ideal Board."

It is a form where the user will enter his data and there will be a return message with the ideal board type for the person.

First step, make form: Done;

Now I need to connect and register the data in the database. I created a table in the Wordpress database called "minhap". However, I am not able to register the data in it: confused:

The connection and registration code looks like this:

global $wpdb;

  $nome = "";
  $email = "";
  $estilo = "";
  $experiencia = "";
  $altura = "";
  $peso = "";

  //VÁRIÁVEIS 
  if(!empty($_POST)){     
     $nome = $_POST['nome'];
     $email = $_POST['email'];
     $estilo = $_POST['estilo'];
     $experiencia = $_POST['experiencia'];
     $altura = $_POST['altura'];
     $peso = $_POST['peso'];


     cadastrar($nome,$email,$estilo,$experiencia,$altura,$peso);
     calcularIMC($estilo,$experiencia,$altura,$peso);

  }


  function cadastrar($nome,$email,$estilo,$experiencia,$altura,$peso){          //INSERE OS DADOS NO BANCO
      try{

          $wpdb->prepare("INSERT INTO aa_minhaprancha("
                  . "nome, email, estilo, experiencia, altura, peso) VALUES("
                  . ":nome, :email, :estilo, :experiencia, :altura, :peso)");
          $wpdb->bindValue(":nome", $nome);
          $wpdb->bindValue(":email", $email);
          $wpdb->bindValue(":estilo", $estilo);
          $wpdb->bindValue(":experiencia", $experiencia);
          $wpdb->bindValue(":altura", $altura);
          $wpdb->bindValue(":peso", $peso);
          $wpdb->execute();

          if($wpdb->rowCount() > 0)
              return true;
          else
              return false;

      }catch(PDOException $e){
         echo "Erro ao incluir na tabela categoria ".$e->getMessage();
      }
  }

At first, how can I connect and register data in the database?

    
asked by anonymous 08.06.2016 / 20:29

2 answers

1

Boss, try this, get stuck, comment, agent moves on ...

<?php
//Suas informações
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "bd_";

// Conexão
$conn = new mysqli($servername, $username, $password,$dbname);

// Verifica sua conexão depois se tiver ok vc customiza
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
} 
echo "Conecto sussu";

// Aqui se tá selecionando o banco de novo, qq tira 
mysqli_select_db($conn,"bd_");

//insere os dados recebidos pelo formulario
$nome_cad = $_POST['nome_cad'];//o mesmo nome que tiver nos parâmetros do seu form lá onde tem o "name="
$email_cad = $_POST['email_cad'];//idem

//Aqui é a tal da query(consulta em inglês)
$query = "INSERT INTO sua_tabela (nome_cad, email_cad)
VALUES ('$nome_cad', '$email_cad')";

if ($conn->query($query) === TRUE) {
    echo "cadastro realizado com sucesso";
} else {
    echo "Error: " . $query . "<br>" . $conn->error;
}

$conn->close();
?>

See if funfa, qq thing comments ... GL;) More correct even is the prepare, mai in a mto mto, with this example you can compare and make prepare tmb if you give a fuck ... That's just the connection itself ...

    
09.06.2016 / 04:23
1

I did it! I made the direct form:

<?php
/*CONEXAO COM O BANCO DE DADOS*/
define('DB_NAME', 'meubanco');
define('DB_USER', 'meuusuario');
define('DB_PASSWORD', 'minhasenha');
define('DB_HOST', 'localhost');
define('DB_CHARSET', 'utf8mb4');
define('DB_COLLATE', '');


function conectar(){

    $dsn = "mysql:host=".DB_HOST.";dbname=".DB_NAME."";

    try{
        $conectar = new PDO($dsn, DB_USER, DB_PASSWORD);
        $conectar->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    }catch(PDOException $e)
    {
       echo "Erro ao conectar ao banco". $e->getMessage();
    }
    return $conectar;
}

and the registration code was exactly the same, I just removed '$ wpdb' for any variable.

    
09.06.2016 / 17:13