1

I am trying to insert data into Wordpress database by creating a table "wp_ecoles_details". I have created the table but when I try to insert the data from form, it does not insert the data. I have checked that database connection works, but the insertion does not happen. Can someone help me on this? Here is my code:-

<?php
/**
 * Template Name: cours_page
 *
 * Creates a page with posts, akin to the default index.php. Using this template you can create as many pages of posts as you want.
 *
 * @package thim
 */
get_header();



if ( is_user_logged_in() ) {

    global $wpdb;
    global $current_user;   

    $user = wp_get_current_user();
    //echo ($user);
    $post_id = $wpdb->get_results("SELECT DISTINCT user_id FROM $wpdb->pmpro_membership_orders");

    if ( isset( $_POST['submit'] ) ){

    $type = $_POST["type"];
    $nom_ecole = $_POST["nom_ecole"];
    $adresse = $_POST["adresse"];     
    $postale = $_POST["postale"];
    $ville = $_POST["ville"];
    $telephone = $_POST["telephone"];
    $classes = $_POST["classes"];
    $total_eleve = $_POST["total_eleve"];
    $n_scolaire = $_POST["n_scolaire"];

        global $wpdb;
        $wpdb->insert('wp_ecoles_details', array(
                'type' =>   $type ,
                'nom_ecole' => $nom_ecole,
                'adresse' => $adresse,     
                'postale' => $postale,
                'ville' => $ville,
                'telephone' => $telephone,
                'classes' => $classes,
                'total_eleve' => $total_eleve,
                'n_scolaire' => $n_scolaire
            ),
            array( '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s' ));

    }

?>  
        <div class="ecole_div" id="ecole_div">
            <form  method="post">
              <fieldset>
                <legend>Information sur l’école:</legend>

                 <input type="radio" name="type" value="privee"> Privée
              <input type="radio" name="type" value="publique"> Publique
                <br> <br>
                Nom de l’école:<br>
                <input type="text" name="nom_ecole" value="">
                <br>
                Adresse:<br>
                <textarea rows="4" cols="50" name="adresse"  value="">
                    </textarea>
                <br> 

                Code postal:<br>
                <input type="number" name="postal" value="">
                <br>
                Ville:<br>
                <input type="text" name="ville" value="">
                <br><br>
                Téléphone école:<br>
                <input type="text" name="telephone" value="">
                <br>
                Nombre de classes:<br>
                <input type="number" name="classes" value="">
                <br><br>
                Nombre total d’élèves:<br>
                <input type="number" name="total_eleve" value="">
                <br>
                Niveau scolaire
                 <input type="checkbox" name="n_scolaire" value="maternelle"> Maternelle
              <input type="checkbox" name="n_scolaire" value="primaire"> Primaire
              <input type="checkbox" name="n_scolaire" value="elementaire"> Elémentaire
              <br><br>
                <input type="submit" value="Submit">
              </fieldset>
            </form>
        </div>
14
  • 1
    You very much need to debug your insert query ... $wpdb->last_error; $wpdb->last_query; Commented Sep 14, 2018 at 16:57
  • how i can use?? Commented Sep 14, 2018 at 16:59
  • Insert the following right after your $wpdb->get_results line: $wpdb->last_error; $wpdb->last_query; and post the results ... Commented Sep 14, 2018 at 17:03
  • Also after your insert statement $wpdb->insert(...) Commented Sep 14, 2018 at 17:05
  • Does the data needs to be in a special table? Can you provide more details about what you want to accomplish I am inferring you are trying to store some kind of address. Commented Sep 14, 2018 at 17:07

1 Answer 1

1

You have a field named postal in your form, but in your code you are trying to get value from postale.

Replace

<input type="number" name="postal" value="">

with th

<input type="number" name="postale" value="">

UPDATE:

add name attribute to the the submit button:

<input type="submit" name="submit" value="Submit">
Sign up to request clarification or add additional context in comments.

7 Comments

i change it and and the same thing
i don't know why i don't get anythning no error and no result
Have you tried to debug as suggested by @Jamie_D ? In the config file add define( 'WP_DEBUG_LOG', true ); after define( 'WP_DEBUG', true );
yes the problem is i don't get any thing in post like i reload my page with nothing in post
Make sure you have define( 'WP_DEBUG_LOG', true ); in your config file. After that check for debug.log file in the wp-content folder
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.