• Resolved Jonas

    (@ropaeh)


    Hey,

    i can’t figure out why my code to create 2 tables in the Database isn’t working.

    Here’s the code:

    <?php
    
        /* create tables for jQuery Maximage */
    
        function jqmi_create_tables() {
    
            global $wpdb;
    	$charset_collate = $wpdb->get_charset_collate();
    	require_once( ABSPATH.'wp-admin/includes/upgrade.php' );
    
            $jqmi_image_paths = $wpdb->prefix.'jqmi_image_paths';
            $jqmi_options = $wpdb->prefix.'jqmi_options';
    
            // wp_jqmi_image_paths
    
            $jqmi_sql_one = "CREATE TABLE ".$jqmi_image_paths." (
                id int(11) NULL AUTO_INCREMENT,
                path text NOT NULL,
                UNIQUE KEY id (id)
            ) $charset_collate;";
    
            dbDelta( $jqmi_sql_one );
    
            // wp_jqmi_options
    
            $jqmi_sql_two = "CREATE TABLE ".$jqmi_options." (
                option text NOT NULL,
                value text NOT NULL,
                UNIQUE KEY option (id)
            ) $charset_collate;";
    
            dbDelta( $jqmi_sql_two );
    
        }
    
        register_activation_hook( __FILE__, 'jqmi_create_tables' );
    
        /* initial information for wp_jqmi_options */
    
        global $wpdb;
        $wpdb->insert(
            $wpdb->prefix.'jqmi_options',
            array(
                'effect' => 'fade'
            )
        );
    
    ?>

Viewing 4 replies - 1 through 4 (of 4 total)
  • I’m not 100%, but I think you need to have your columns in the ticks ”.
    And also you must put your .$jqmi_image_paths. in ‘.$jqmi_image_paths.’ not the double ones. So your table would then be:

    $jqmi_sql_one = "CREATE TABLE '.$jqmi_image_paths.' (
                'id' int(11) NULL AUTO_INCREMENT,
                'path' text NOT NULL,
                UNIQUE KEY id (id)
            ) $charset_collate;";

    But the ‘ are more like the ones here used for the code.

    Dion

    (@diondesigns)

    If your code actually looks like what you posted, you are attempting to insert data into a table which has yet to be created.

    The code in the post above mine will generate a PHP parse error. If anything, the query should look like this:

    $jqmi_sql_one = "CREATE TABLE $jqmi_image_paths (
    	id int(11) NULL AUTO_INCREMENT,
    	path text NOT NULL,
    	UNIQUE KEY id (id)
    ) $charset_collate";

    Thread Starter Jonas

    (@ropaeh)

    Thanks for your replies, but unfortunately this didn’t work.

    Both tables aren’t created and i still can’t figure out why…

    Thread Starter Jonas

    (@ropaeh)

    Ok so now i’ve got it working.
    The php error log wasn’t helpful but it seems that there was a problem with the terms “option” and “value”.

    Nethertheless, thank you guys!

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘WP Plugin won't create tables in Database’ is closed to new replies.