简体   繁体   中英

insert into tables from another tables

Well, I trying insert into tables to another but I get this error;

WordPress database error: [You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near ''lcl_user_agent' (user_agent, hitcount, click_timestap) SELECT visite_useragent,' at line 1] [You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near ''lcl_user_agent' (user_agent, hitcount, click_timestap) SELECT visite_useragent,' at line 1]

This is my code;

$addagent = "INSERT INTO $wpdb->prefix.'lcl_user_agent' (user_agent, hitcount, click_timestap) SELECT visite_useragent, visite_hitcount, click_timestap FROM $wpdb->prefix.'lcl_visite_agent' .agent_id IN (' . $agent_id . ')";

$wpdb->query($addagent);

I want insert into lcl_user_agent the visites from visite_useragent then I get the visite for block with $agent_id from the form

It looks like you're messing up at the very end. It should be this form:

INSERT INTO table2 (column1, column2, column3, ...)
SELECT column1, column2, column3, ...
FROM table1
WHERE condition;

So maybe this is what you're going for?

$addagent = '
 INSERT INTO 
        '.$wpdb->prefix.'lcl_user_agent
        (user_agent, hitcount, click_timestap) 
    SELECT 
        visite_useragent, visite_hitcount, click_timestap 
    FROM 
        '.$wpdb->prefix.'lcl_visite_agent
    WHERE
        agent_id IN (' . $agent_id . ')
 ';

Give this a shot:

$addagent = "INSERT INTO ". $wpdb->prefix. 'lcl_user_agent'. " (user_agent, hitcount, click_timestap) SELECT visite_useragent, visite_hitcount, click_timestap FROM ". $wpdb->prefix.'lcl_visite_agent'. " where agent_id IN ('. $agent_id. ')";

$wpdb->query($addagent);

I've never had much luck embedding the -> within a double-quoted string. Just makes it more clear to concatenate the strings together. Also, in this case, unless you have multiple $agent_id values, you might consider where agent_id = $agent_id instead of using IN.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM