【问题标题】:Php insert with select in value syntax在值语法中使用选择的 PHP 插入
【发布时间】:2017-03-20 18:57:50
【问题描述】:

这个 php 插入代码是我的 php 脚本的一部分,用于从 csv 文件导入值。我想通过 csv 文件中设置的其他值插入 fld_id。 csv内容示例:123456789,title,description where:

data[0] is 123456789
data[1] is title
data[2] is description

如果insert运行在mysql客户端,值是正确insert,但是从php页面返回错误:fld_idcat不能为null。

这是部分php代码:

/*other code*/
do {
    if ($data[0]) {
        mysql_query("
                INSERT INTO table_imports 
                (
                    fld_username,
                    fld_idcat,
                    fld_title,
                    fld_desc,
                    fld_userimport
                )
                VALUES
                (
                    '$Username',
                    (select id_cat from table_category where id_cat_random = '".addslashes($data[0])."'),
                    '".addslashes($data[1])."',
                    '".addslashes($data[2])."'
                    'userImport'                        
                )
        ");
    }
} while ($data = fgetcsv($handle,1000,",","'"));
header('Refresh: 6; URL=finishPage.php'); 
die(mysql_error());

/*other code*/

如何更正此 php 插入以在 select 内的变量 $data[0] 处传递值 123456789?

我希望能解释一下我的问题

谢谢

【问题讨论】:

标签: php mysql csv


【解决方案1】:

为了所有人。我已经解决了我的问题。脚本工作正常,但是变量 data[0] 中的值没有通过,因为 csv 文件格式不是 UTF8。转换成csv文件格式后,脚本运行正常。

【讨论】:

    猜你喜欢
    • 2018-11-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-06-20
    • 1970-01-01
    相关资源
    最近更新 更多