dimanche 1 mai 2016

Wordpress Fatal Error - SQL - Do I suppress?

Been building a custom plugin for a client and I have been working on creating a table and inserting data to Wordpress. I finally have the tables created and the data does indeed insert (although 3 times??).

So I have it working where it sets up everything but I still get the "Plugin could not be activated because it triggered a fatal error." No debug log gets created, nothing in the console, tables are created and data is inserted. Then it breaks.

Can anyone shed light on the statements to see where the error is. My eyes cannot find it and a second site would be helpful.

This is the create table function:

    function db1_install() {
    global $wpdb;
    global $db_version1;
    global $table_name;
    $table_name = $wpdb->prefix . 'myCategories';   
    $charset_collate = $wpdb->get_charset_collate();    
    $sql = "CREATE TABLE $table_name (
        id mediumint(9) NOT NULL,
        item varchar(20) NOT NULL,
        item_cat varchar(20) NOT NULL
        ) $charset_collate;";
    require_once( ABSPATH . 'wp-admin/includes/upgrade.php' );
    dbDelta( $sql );
    add_option( 'db_version1', $db_version1 );    
    } 
    register_activation_hook( __FILE__, 'db1_install' );

And this is the Data insert function:

function add_categories(){

    global $wpdb;
    $table_name = $wpdb->prefix . 'myCategories';
    $wpdb->query("INSERT INTO $table_name (id,item,item_cat)
    values (1,'item1','cat1'), (2,'item2','cat1'), (2,'item3','cat4')");
    die();
    return true;
    }
    register_activation_hook( __FILE__, 'add_categories');

It does not tell me the error and it inserts the records 3 times!!

I am not using meta or options for a reason and really just nned to figure this out so I can complete my custom pluging.

by the way I am a newbie/novice PHP coder so be gentle. Anyone?



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire