I'm unfortunately getting an empty query warning and i cant seem to find the issue I hope you'll help me.

mysqli->query( ) --> $result = $conn->query($sql); is where it tells me i'm doing something wrong.


global $wpdb;

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

$servername = "localhost";
$username = "root";
$password = "****";
$dbname = "***";

// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);

$user = $current_user->ID;
$time = new DateTime('now');

$reg_id = $wpdb->escape(trim($_POST['reg_id']));
$dato = $wpdb->escape(trim($_POST['$time']));
$fiske_vægt = $wpdb->escape(trim($_POST['fiske_vægt']));
$fiske_længde = $wpdb->escape(trim($_POST['fiske_længde']));
$reg_user_id = $wpdb->escape(trim($_POST['$user']));

$sql = $wpdb->insert( 'wp_registreringer',
        'dato' => apply_filters('pre_register_dato', $dato),
        'fiske_vægt' => apply_filters('pre_register_fiske_vægt', $fiske_vægt),
        'fiske_længde' => apply_filters('pre_register_fiske_længde', $fiske_længde),
        'reg_user_id' => apply_filters('pre_register_reg_user_id', $user)

$result = $conn->query($sql);   

My form

<form method="post">
    <h3>Don't have an account?<br /> Create one now.</h3>
    <p><input type="number" value="" name="fiske_længde" id="fiske_længde" />cm</p>
    <p><input type="number" value="" name="fiske_vægt" id="fiske_vægt" />kg</p>
    <p><input type="datetime-local" name="dato" value="<?php echo date("Y-m-d\TH:i:s",time()); ?>"/></p>
    <button type="submit" name="btnregister" class="button" >Submit</button>
    <input type="hidden" name="submit" value="submit" />


There's a lot wrong with your code:

  1. In your query you're setting reg_user_id to $user, when it looks like you meant to use $reg_user_id, which is probably a problem because you're setting $user to $current_user->ID even though $current_user doesn't exist.
  2. $_POST['reg_id'] doesn't exist in your form, and I have no idea what you're trying to do with $_POST['$user'] and $_POST['$time'].
  3. You shouldn't escape values when using $wpdb->insert().
  4. You're inserting with $wpdb, but trying to run a query from $conn by passing it $sql even though $wpdb->insert() does not return SQL.

Here is code that performs the same function without those issues:

function wpse_283721_handle_form() {
    global $wpdb;

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

    if ( ! is_user_logged_in() ) {

    $current_user = wp_get_current_user();

    $fiske_vægt = trim( $_POST['fiske_vægt'] );
    $fiske_længde =  trim( $_POST['fiske_længde'] );

        $wpdb->prefix . 'registreringer',
            'dato'         => new DateTime( 'now' ),
            'fiske_vægt'   => apply_filters( 'pre_register_fiske_vægt', $fiske_vægt ),
            'fiske_længde' => apply_filters( 'pre_register_fiske_længde', $fiske_længde ),
            'reg_user_id'  => $current_user->ID,
add_action( 'init', 'wpse_283721_handle_form' );

Take note of the following:

  1. I put the function for handling the form in a hook, not in a template. It's not clear from your code how you've done it, but this would be the correct way.
  2. Since you use the current user's ID in the code, I do not proceed with processing the form if a user is not logged in.
  3. I make sure to get the current user with wp_get_current_user().
  4. I do not use $wpdb->escape(), for two reasons: Firstly, it has been deprecated and shouldn't be used, and secondly, $wpdb->insert() will do the escaping for you.
  5. I didn't use the date submitted with the form since the server can just set the date when inserting. If you change your table to set the default value of the dato column to the current time you don't even need to do that.
  6. I used $wpdb->prefix to ensure the correct table name. This is also something to do when the table is created if you're doing that in code.

But there's still things you'll probably want to look into:

  1. Use something more secure or unique than $_POST['submit'] to see if your form is submitted. Possibly even a nonce.
  2. More validation or sanitisation. Like making sure all fields are present, and ensuring they are the expected types of values eg. numbers are numbers.
  • Thank you very much for your detailed answer I'm going to look into it now and how im supposed to use functions and how to connect it to my form. You've help me a great deal to use Wordpress custom tables and what i've been doing was wrong and contradictory to each other. Now I'll read some more to figure this out :) QUICK QUESTION: How am I supposed to use the function in my registrering.php page ?
    – Mg10
    Oct 30 '17 at 13:38

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.