• I have this php script that generate an sql query. However, it needs a semicolon inside the sql statement and because of that, the sql query doesn’t work.
    The problem is that, the value bm_shopping_cart.php;bm_categories.php doesn’t stored in the database that suppose to be inserted by that query. When I removed;bm_categories.php the value bm_shopping_cart.php is inserted on the database. So the problem resides on having the semicolon. I also tried using \ but the problem still there. I really need the semicolon included on the value. Any ideas?

    VALUES ('Installed Modules', 'MODULE_BOXES_INSTALLED', 'bm_shopping_cart.php;bm_categories.php', 'This is automatically updated. No need to edit.', '6', '0', now())

    It is included in this function.

    function insert_configuration6_table($table_name9, $type) {
         global $wpdb;
    
         if (!empty ($wpdb->charset))
         $charset_collate = "DEFAULT CHARACTER SET {$wpdb->charset}";
         if (!empty ($wpdb->collate))
         $charset_collate .= " COLLATE {$wpdb->collate}";
    
        $sql = "INSERT INTO {$table_name9} (configuration_title, configuration_key, configuration_value, configuration_description, configuration_group_id, sort_order, date_added) VALUES ('Installed Modules', 'MODULE_BOXES_INSTALLED', 'bm_shopping_cart.php;bm_categories.php', 'This is automatically updated. No need to edit.', '6', '0', now()),('Installed Template Block Groups', 'TEMPLATE_BLOCK_GROUPS', 'boxes', 'This is automatically updated. No need to edit.', '6', '0', now());";
    
         require_once(ABSPATH . 'wp-admin/includes/upgrade.php');
         dbDelta($sql);

    I have searched a lot for this however, I can’t find a solution that work.
    How can I make an sql query that includes a semicolon in the values? or do you have any solution to this or a dbdelta alternative??

    [Moderator note: Please wrap code in the backtick character or use the code button.]

Viewing 2 replies - 1 through 2 (of 2 total)
  • There’s two things about this.

    First, semi-colans are fine in text as long as they are part of the text (inside single-quotes) like your is, so that’s not going to be where the problem is. I’d do something like this with the SQL query to see if it tells me a more specific line number:

    $sql = "INSERT INTO {$table_name9} (
          configuration_title,
          configuration_key,
          configuration_value,
          configuration_description,
          configuration_group_id,
          sort_order,
          date_added
     ) VALUES (
           'Installed Modules',
          'MODULE_BOXES_INSTALLED',
          'bm_shopping_cart.php;bm_categories.php',
          'This is automatically updated. No need to edit.',
          '6',
          '0',
          now()
     ),
     (
          'Installed Template Block Groups',
          'TEMPLATE_BLOCK_GROUPS',
         'boxes',
         'This is automatically updated. No need to edit.',
         '6',
         '0',
         now()
    );";

    Second, and more likely most importantly, dbdelta() is not meant to be used for inserting data. It’s for defining and building tables, and should really only be used for that.

    You could use either a standard $wpdb->query($query) call, but I’d recommend looking at the $wpdb->insert() function as that will do all of the escaping and security that you’ll need.

    Try something like this:

    global $wpdb;
    $mod_val='bm_shopping_cart.php;bm_categories.php';
    
    $wpdb->insert($table_name9, array('field1' => $field1,'fieldwhatever' => $mod_val), array('%s','%s'));

    Hope this helps, all the best!

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘a sql query with a semicolon problem’ is closed to new replies.