Creating a table via dbDelta
Creating a table via dbDelta
Creating a table via dbDelta
The dbDelta function is super nitpicky. Everything has to be exactly correct. In this case, the lack of space between the table name and the parentheses following it will cause your call to fail, because it can’t figure out the table name without that space. The relevant code in dbDelta() is this: if (preg_match(“|CREATE TABLE …
require_once(ABSPATH . ‘wp-admin/includes/upgrade.php’); $drzava = “your sql”; dbDelta($drzava);
Glancing at your code I see a few potential problems. According to this Codex page dbDelta is “picky”; You must put each field on its own line in your SQL statement. You must have two spaces between the words PRIMARY KEY and the definition of your primary key. You must use the key word KEY …
function jal_install() { global $jal_db_version; $sql = “CREATE TABLE IF NOT EXISTS WP_AWESOME_TABLES ( id mediumint(9) NOT NULL AUTO_INCREMENT, PRIMARY KEY id (id) );”; require_once( ABSPATH . ‘wp-admin/includes/upgrade.php’ ); dbDelta( $sql ); add_option( “jal_db_version”, $jal_db_version ); } register_activation_hook( _____FILE_____, ‘jal_install’ ); You have to hook the function. After activate the plugin table will created. http://codex.wordpress.org/Creating_Tables_with_Plugins
You appear to have answered your own question in a comment, but yes, the SQL is wrong. KEYs must come at the end of the statement, after the column definitions.
dbdelta demands that: You must put each field on its own line in your SQL statement. You must have two spaces between the words PRIMARY KEY and the definition of your primary key. You must use the key word KEY rather than its synonym INDEX and you must include at least one KEY. You must …
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, …
You are missing comma end of phone varchar(10) NOT NULL line, add comma end of line after that table will be created. I have tested https://prnt.sc/qgqmog function lapizzeria_database() { global $wpdb; global $lapizzeria_db_version; $lapizzeria_db_version = “1.0”; $table = $wpdb->prefix . ‘reservation’; $charset_collate = $wpdb->get_charset_collate(); //SQL Statement $sql = “CREATE TABLE $table ( id mediumint(9) NOT …
I would strongly suggest using AJAX for this purpose. Using “init” for inserting some data to your database is not a good solution. Here’s a good guide on how to do it with AJAX: https://webprogramo.com/how-to-create-an-ajax-form-in-wordpress/1156/