sql creates two tables using $wpdb->prefix

Viewed 47
$tablename = $wpdb->prefix . "name";

$sql = "CREATE TABLE IF NOT EXISTS $tablename(
    time_id INT(11) NOT NULL AUTO_INCREMENT,
    job VARCHAR(120) NOT NULL,
    employee VARCHAR(100) NOT NULL,
    date_entry DATETIME NOT NULL,
    duration INT(11) NOT NULL,
    PRIMARY KEY(time_id)
);";

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

dbDelta($sql);

creates two tables: 'wp_name' and 'name'. If I define $tablename = 'wp_name' instead it only creates one table. Also '{$wpdb->prefix}name' doesn't change the problem.

What's the problem here? - thanks!

1 Answers

Get rd of the brackets

$sql = "CREATE TABLE IF NOT EXISTS $tablename (
    ...
);";

They are only used to replace internal wp variables

Update:

your case can't create two tables by itself, there is no way, you have somewhere the same code twice.

Without the prefix, the second code checks that there is already a table named nameand does nothing, but when you add the prefix, it tries to add the tbale name which azt this time doesn't exist, because it is call wp_name

Related