gpt4 book ai didi

php - 每四个值分隔数组并使 MySql 插入

转载 作者:行者123 更新时间:2023-11-29 13:20:17 24 4
gpt4 key购买 nike

抱歉,我是新手。我有一个像这样的数组:

array(Jason,bp 9/27,07AAC,Mike,T/L KGL 6,07AAB);  //(could be more or less

我需要每四个值将其分开。我想让 MySql 像这样插入:

INSERT INTO `table` (`name` ,`address` ,`number`)
VALUES
('Jason', 'bp 9/27', '07AAC'),
('Mike', 'T/L KGL 6', '07AAB')
..... (could be more or less)

我已经编写了一个代码,但它一直显示错误,我不知道哪一部分是错误的。

下面是从 ajax 接收数据的代码。

if ($_POST['action'] == "checktransfertmp") {

$arrCheckTrans = $_POST['arrCheckTrans'];

$trans = explode(",", $arrCheckTrans);
$output = array_chunk( $trans, 4 );
//print_r($trans);
$addContact = addCheckCustomertemp($output);
if ($addContact != false) {
echo "ok.";
}

}

之后它将以array_chunk的形式将数据发送到mysql。

function addCheckCustomertemp($output){

$connection = MySQLConnection();
$conf = new BBSupervisorConf();

$flag =true;

$query = 'INSERT INTO bb_customer(customername, address, callername) VALUES ';

foreach ($output as $chunk) {

$query .= '(' . implode( ',', $chunk ) . '),';
}

$query = substr($query, 0, -1);


try {
if (!mysql_query($query)){
die (mysql_error());
$flag = false;
}else{
}
}catch(Exception $e){
//some log here
}
closeDB($connection);
return $flag;
}

我一直在努力,我得到的唯一结果是:*您的 SQL 语法有错误;请检查与您的 MySQL 服务器版本相对应的手册,了解在“9/27,07AAC”附近使用的正确语法,(Mike, T/L KGL 6' at line 1 。

如果这是一个愚蠢的问题或者我的解释不好,请让我先道歉。任何帮助或评论将不胜感激。

最佳答案

尝试替换这个:

$query .= '(' . implode( ',', $chunk ) . '),';

这样:

$query .= "('" . implode( "','", $chunk ) . "'),";

关于php - 每四个值分隔数组并使 MySql 插入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20934855/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com