如何准备将字符串作为值插入到MySQL表中?

时间:2017-07-24 作者:Russell Eubanks

我试图将一些包含连字符和撇号的字符串作为值插入MySQL表中。我指定字符串值:

$venue_name = addslashes(get_the_title($id));
当我回显该值时,我得到:

4B’s Brewery
以及

Ace Hardware - Bailey
我的SQL语句如下所示:

    INSERT INTO wp_w2bw2c_venue (id, market_id, venue_status, venue_name,
 website, address_1, address_2, city, state, postal_code, phone, fax, email,
 logo_attachment_id, featured_image_attachment_id, tagline, description,
 doors_open, capacity, venue_type_id, salesperson_id, landing_page_id) VALUES
 (817, 7, \'Active\', \'4B’s Brewery\', \'http://4bsbrewery.com/\', \'215 W Main St\',
 \'\', \'Cedaredge\', \'Colorado\', \'81413\', \'970-856-7762\', \'\', \'\', 0, 0, \'\', \'\', \'\',
 0, \'168\', 89, 0)

    INSERT INTO wp_w2bw2c_venue (id, market_id, venue_status, venue_name,
 website, address_1, address_2, city, state, postal_code, phone, fax, email,
 logo_attachment_id, featured_image_attachment_id, tagline, description, 
doors_open, capacity, venue_type_id, salesperson_id, landing_page_id) VALUES 
(915, 7, \'Active\', \'Ace Hardware – Bailey\', \'\', \'Moore Lumber & Hardware\', \'\',
 \'Bailey\', \'Colorado\', \'\', \'720-924-3999\', \'\', \'\', 0, 0, \'\', \'\', \'\', 0, \'168\',
 89, 0)
我正在使用

dbDelta($sql);
更新数据库。

但我遇到如下数据库错误:

    WordPress database error: [You have an error in your SQL syntax; check
 the manual that corresponds to your MySQL server version for the right syntax
 to use near \'\'4B&#8217\' at line 4]

    INSERT INTO wp_w2bw2c_venue (id, market_id, venue_status, venue_name, 
website, address_1, address_2, city, state, postal_code, phone, fax, email,
 logo_attachment_id, featured_image_attachment_id, tagline, description, 
doors_open, capacity, venue_type_id, salesperson_id, landing_page_id) VALUES 
(817, 7, \'Active\', \'4B&#8217



    WordPress database error: [You have an error in your SQL syntax; check 
the manual that corresponds to your MySQL server version for the right syntax to
 use near \'\'Ace Hardware &#8211\' at line 4]

    INSERT INTO wp_w2bw2c_venue (id, market_id, venue_status, venue_name, 
website, address_1, address_2, city, state, postal_code, phone, fax, email, 
logo_attachment_id, featured_image_attachment_id, tagline, description, 
doors_open, capacity, venue_type_id, salesperson_id, landing_page_id) VALUES 
(915, 7, \'Active\', \'Ace Hardware &#8211
特殊字符的和-终止字符串。我尝试了addslashes()、str\\u replace(“-”、“-”、$venue\\u name)和mysql\\u real\\u escape\\u string(),但没有成功。如何准备字符串以接受为mysql VARCHAR表列值?

1 个回复
SO网友:Jacob Peattie

dbDelta 实际上是为了创建数据库或表,而不是插入记录。

我建议使用$wpdb->insert(), 这将为您避开以下值:

$wpdb->insert( 
    $wpdb->prefix . \'w2bw2c_venue\', 
    array(
        \'market_id\'  => 7,
        \'venue_name\' => get_the_title( $id ),
    ),
    array(
        \'%d\',
        \'%s\',
    )
);
在我的示例中,我刚刚使用了您遇到问题的名称字段,显然您需要填写其余字段。我包括market\\u id以显示第二个的正确用法$format 阵列,确保market_id 被解析为整数。

结束

相关推荐

什么是phpBB?它是不是类似于我可以在WordPress中使用的插件?

我对phpBB做了一些研究。它说这是一个论坛插件,但没有提到这个插件可以在Wordpress中使用。然后我看到this forum 比较phpBB和Wordpress插件的地方(看起来两者仍然不同)。我在Wordpress开发StackExchange中看到了几个关于phpBB的问题,但没有一个回答我的问题。所以,如果我买this theme, 那么我可以在Wordpress中使用它吗?如果没有,那么哪个平台可以用来编辑主题?代码是PHP,还是可以使用HTML和CSS?对不起,问题太多了。