简体   繁体   English

MYSQL,PHP:将记录从一个数据库插入另一个数据库

[英]MYSQL, PHP: Insert records from one database to another

I have a necessity to insert some record from one table1 in database1 to another table2 in database2. 我有必要将一些记录从database1的一个table1插入到database2的另一个table2。 So far I have this.. 到目前为止,我有这个。

    $records_r = mysqli_fetch_assoc(mysqli_query($conn_r, "SELECT * FROM `export` WHERE ID < 100"));        

    $columns_r = implode(",",array_keys($records_r));
    $values_r = implode(",",array_values($records_r));          

    $import = mysqli_query($conn_i,"INSERT INTO NOTimport ($columns_r) values ($values_r)");
    if (!$import) {
    printf("Error: %s\n", mysqli_error($conn_i));
    exit();}

It gives me the error: 它给了我错误:

Error: You have an error in your SQL syntax; 错误:您的SQL语法有错误;

This is how the syntax looks: 语法如下所示:

INSERT INTO `NOTimport` ('xx,xx,xx,xx,xx,xx,xx,xx') values ('11,'11,E,2079,1931,xx,xx,x')

I am 99% sure that single quotes are causing the error, but why are there? 我99%确信单引号引起了错误,但是为什么会出现错误?

As per your original post https://stackoverflow.com/revisions/31116693/1 and completely overwriting your original post without marking it as an edit: 按照您的原始帖子https://stackoverflow.com/revisions/31116693/1并完全覆盖您的原始帖子,而不将其标记为编辑:

You're using the MySQL import reserved word 您正在使用MySQL import保留字
https://dev.mysql.com/doc/refman/5.5/en/keywords.html https://dev.mysql.com/doc/refman/5.5/zh-CN/keywords.html

It needs to be wrapped in ticks 它需要包裹在tick中

INSERT INTO `import` ($columns_r) values ($values_r)

or rename that table to something other than a reserved word. 或将该表重命名为保留字以外的名称。

Plus, $values_r may require to be quoted and depending on what's being passed through $columns_r , you may need to use ticks around that. 另外,可能需要在$values_r加上引号,并且取决于通过$columns_r传递的内容,您可能需要在其周围使用对号。

Ie: 即:

INSERT INTO `import` (`$columns_r`) values ('".$values_r."')

Even then, that is open to SQL injection. 即使这样,它仍然可以进行SQL注入。

So, as per your edit with these values values ('11,'11,E,2079,1931,xx,xx,x') , just quote the values since you have some strings in there. 因此,根据您使用这些值values ('11,'11,E,2079,1931,xx,xx,x') ,因为其中包含一些字符串,所以只需引用这些值即可。 MySQL will differentiate between those values. MySQL将区分这些值。

Escape your values: 逃避您的价值观:

$values_r = implode(",",array_values($records_r));
$values_r = mysqli_real_escape_string($conn_r, $values_r);

or $conn_i I'm getting confused as to which variable is which here. $conn_i我对这里的哪个变量感到困惑。 Be consistent if you're using the same db. 如果使用相同的数据库,请保持一致。

Edit: 编辑:

As stated in comments by chris85, use prepared statements and be done with it. 如chris85的评论所述,使用准备好的陈述并加以处理。

import is a reserved word in MYSQL. import是MYSQL中的保留字。 So, you need to use backticks (``) around it in your query. 因此,您需要在查询中使用反引号(``)。

So rewrite as follows: 所以重写如下:

 $import = mysqli_query($conn_i,"INSERT INTO `import` ($columns_r) values ($values_r)");

Without Using PHP you can use MySql Query Which Will Perform Insert Operation As:- 如果不使用PHP,则可以使用MySql查询,该查询将按以下方式执行插入操作:-

$columns_r='`name`,`class`';

mysqli_query($conn_i,"INSERT INTO `import` ({$columns_r}) select {$columns_r} from `export`");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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