0

我有一个脚本可以循环 2000 条客户记录并将它们复制到另一个数据库中。有时它运行完美,有时它返回一个 mysql 错误,并带有与 PHP 中发送的查询不对应的消息。这是错误消息(PA K'2012-12-04 11:05:09')O DU LIC TE EY UPDA):

    [0] => 数组
        (`在此输入代码`
        [级别] => 错误
        [代码] => 1064
        [消息] => 您的 SQL 语法有错误;检查与您的 MySQL 服务器版本相对应的手册,以了解在 'PA K'2012-12-04 11:05:09' 附近使用的正确语法
        )
            O DU LIC TE EY UPDA' 在第 45 行
        )

发生错误时在 PHP 中打印的查询:

    插入
                `客户`
            (
                `klantId`,
                `naam`,
                `地址`,
                `邮政编码`,
                `普拉斯`,
                `电话`,
                `移动`,
                `传真`,
                `电子邮件`,
                `网站`,
                `post_adres`,
                `post_postcode`,
                `post_plaats`,
                `kvknummer`,
                `btwnummer`,
                `rekeningnr`,
                `postbanknr`,
                `邮寄`,
                `创建`,
                `修改`
            )价值观
            (
                1134,
                '纳姆',
                '地址 9L',
                '1234 SG',
                '普拉茨',
                无效的,
                无效的,
                无效的,
                无效的,
                'www.test.nl',
                无效的,
                无效的,
                无效的,
                无效的,
                无效的,
                无效的,
                无效的,
                1、
                '2012-12-04 11:05:09',
                '2012-12-04 11:05:09'
            )
            重复密钥更新
                `naam`=值(`naam`),
                `adres`=VALUES(`adres`),
                `邮政编码`=VALUES(`邮政编码`),
                `plaats`=VALUES(`plaats`),
                `telefoon`=VALUES(`telefoon`),
                `mobiel`=值(`mobiel`),
                `传真`=值(`传真`),
                `电子邮件`=值(`电子邮件`),
                `网站`=值(`网站`),
                `post_adres`=VALUES(`post_adres`),
                `post_postcode`=VALUES(`post_postcode`),
                `post_plaats`=VALUES(`post_plaats`),
                `kvknummer`=值(`kvknummer`),
                `btwnummer`=值(`btwnummer`),
                `rekeningnr`=值(`rekeningnr`),
                `postbanknr`=值(`postbanknr`),
                `邮寄`=值(`邮寄`),
                `created`=VALUES(`created`),
                `修改`=值(`修改`)
                ;

我的脚本在公司实习,数据库在数据中心运行 extern。脚本运行 50% 正确 50% 错误...

提前致谢!!

添加了带循环的脚本:

`
    $dbcon = getDbConnection();
    $aClients = getClients(); // 返回对象
    foreach ($aClients AS $oClient) {
    $sQuery = ' 插入
        `客户`
    (
        `klantId`,
        `naam`,
        `地址`,
        `邮政编码`,
        `普拉斯`,
        `电话`,
        `移动`,
        `传真`,
        `电子邮件`,
        `网站`,
        `post_adres`,
        `post_postcode`,
        `post_plaats`,
        `kvknummer`,
        `btwnummer`,
        `rekeningnr`,
        `postbanknr`,
        `邮寄`,
        `创建`,
        `修改`
    )价值观
    (
        ' . $dbcon->real_escape_string($oClient->id) 。',
        ' . $dbcon->real_escape_string($oClient->naam) 。',
        ' . $dbcon->real_escape_string($oClient->adres) 。',
        ' . $dbcon->real_escape_string($oClient->postcode) 。',
        ' . $dbcon->real_escape_string($oClient->plaats) 。',
        ' . $dbcon->real_escape_string($oClient->telefoonnummer) 。',
        ' . $dbcon->real_escape_string($oClient->mobielnummer) 。',
        ' . $dbcon->real_escape_string($oClient->faxnummer) 。',
        ' . $dbcon->real_escape_string($oClient->emailadres) 。',
        ' . $dbcon->real_escape_string($oClient->website) 。',
        ' . $dbcon->real_escape_string($oClient->afwijkend_adres) 。',
        ' . $dbcon->real_escape_string($oClient->afwijkend_postcode) 。',
        ' . $dbcon->real_escape_string($oClient->afwijkend_plaats) 。',
        ' . $dbcon->real_escape_string($oClient->kvknummer) 。',
        ' . $dbcon->real_escape_string($oClient->btwnummer) 。',
        ' . $dbcon->real_escape_string($oClient->bankrekening) 。',
        ' . $dbcon->real_escape_string($oClient->postbank) 。',
        ' . $dbcon->real_escape_string($oClient->mailing == 'ja' ? 1 : 0) 。',
        ' . $dbcon->real_escape_string($oClient->created) 。',
        ' . $dbcon->real_escape_string($oClient->modified) 。'
    )
    重复密钥更新
        `naam`=值(`naam`),
        `adres`=VALUES(`adres`),
        `邮政编码`=VALUES(`邮政编码`),
        `plaats`=VALUES(`plaats`),
        `telefoon`=VALUES(`telefoon`),
        `mobiel`=值(`mobiel`),
        `传真`=值(`传真`),
        `电子邮件`=值(`电子邮件`),
        `网站`=值(`网站`),
        `post_adres`=VALUES(`post_adres`),
        `post_postcode`=VALUES(`post_postcode`),
        `post_plaats`=VALUES(`post_plaats`),
        `kvknummer`=值(`kvknummer`),
        `btwnummer`=值(`btwnummer`),
        `rekeningnr`=值(`rekeningnr`),
        `postbanknr`=值(`postbanknr`),
        `邮寄`=值(`邮寄`),
        `created`=VALUES(`created`),
        `修改`=值(`修改`)
    ;';
    $dbcon->query($sQuery);
}`

上次编辑:在与数据库服务器相同的服务器上尝试了该脚本,但没有发生。它发生在某个地方...

设置为:脚本在服务器 A 上运行,数据库在服务器 B 上运行。网络不同,因此使用 Internet 进行数据库连接。某处数据丢失...

4

1 回答 1

0

You don't santize the input well. If in PHP you have to make all values valid with mysql_real_escape_string()

'P A K'2012-12-04 11:05:09' looks like you have a ' to many, wich causes the error. Escape this by \'

Querys work, or they don't. No 50-50 change with good coding; so if the query works, there's a problem with some input.

于 2012-12-05T14:25:14.677 回答