0

On my website, there is a section called "My Ideal plank."

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

First step, make the form: Done;

Now do I need to connect and register the data in the database. I created a table in the Wordpress database called "minhaprancha". But I am not able to record the data on it: confused:

The connection code and registration is as follows:

<?php
require_once('../../../wp-load.php');
require_once('../../../wp-config.php');
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->insert( "INSERT INTO aa_minhaprancha("."nome, email, estilo, experiencia, altura, peso) VALUES (". ":nome', ':email', ':estilo', ':experiencia', ':altura', ':peso')");

          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 the data in the database?

  • Have you considered using a custom post type and wp_insert_post instead? Then you don't have to write any queries, you can use WP_Query to fetch things and gain caching speed ups, and you get a free user interface in the admin area and REST API endpoints! – Tom J Nowell Jul 3 '18 at 11:08
  • Also, don't try to insert standalone files for form processing or AJAX requests, it's dangerous, easy to break, and causes problems – Tom J Nowell Jul 3 '18 at 11:09
1

Two things, at least: It looks like you need to declare the global $wpdb and you're using $wpdb->insert() incorrectly. The insert() method doesn't take your SQL query but a list of variables. It then create the SQL for you. See here

You can execute SQL directly but it's a different process.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.