Creating a database in my plugin not working

Good day everyone, I've been trying to add a table in my database for a whole day now, and I can't seem to find the error, I looked into multiple similar questions, but I had my syntax similar to all of them, and I followed the Codex when adding dbDelta().

<?php
if( !function_exists('transaction_draft_save') ){
//1, 234, donation, monday june 5 2018, give monthly, 10, usd, incomplete
function transaction_draft_save($user_id, $full_name, $email, 
$invoiceNumber, $plan_type, $date, $membership_type, $plan_price, $currency, 
$status){

    global $taylormemorial_options;
    global $wpdb;
    $dbprefix = '';
    $dbprefix = $wpdb->prefix;
    $table_name = $wpdb->prefix ."user_transactions";

    require_once( ABSPATH . 'wp-admin/includes/upgrade.php' );

    $sql = "CREATE TABLE IF NOT EXISTS " . $table_name . " (
        'id' INT(6) NOT NULL AUTO_INCREMENT ,
        'user_id' TEXT NOT NULL,
        'full_name' TEXT NOT NULL,
        'email' TEXT NOT NULL,
        'invoiceNumber' TEXT NOT NULL,
        'plan_type' TEXT NOT NULL,
        'date' TEXT NOT NULL,
        'membership_type' TEXT NOT NULL,
        'plan_price' INT NOT NULL,
        'currency' TEXT NOT NULL,
        'payment_method' TEXT NOT NULL,
        'status' TEXT NOT NULL,
        PRIMARY KEY  (id)
        )";

        dbDelta( $sql );


 }
}

?>

Another notice, is that Im calling this function in another page to pass the variables. If i return $sql variable and var_dump the function, it shows the query just fine, so scope is not the problem. I checked once and twice and three times, but can't seem to find the root of the problem, if anyone can provide help, would really appreciate it and thank you.

Answers 1

  • You have syntax error. You don't need single quote for column name.

    You can use phpmyadmin to test MySQL queries.

    $sql = "CREATE TABLE IF NOT EXISTS $table_name (
        id INT(6) NOT NULL AUTO_INCREMENT ,
        user_id TEXT NOT NULL,
        full_name TEXT NOT NULL,
        email TEXT NOT NULL,
        invoiceNumber TEXT NOT NULL,
        plan_type TEXT NOT NULL,
        date TEXT NOT NULL,
        membership_type TEXT NOT NULL,
        plan_price INT NOT NULL,
        currency TEXT NOT NULL,
        payment_method TEXT NOT NULL,
        status TEXT NOT NULL,
        PRIMARY KEY  (id)
        )";
    

Related Questions