gpt4 book ai didi

php - 使用 array_keys() 进行插入

转载 作者:行者123 更新时间:2023-11-29 11:34:25 25 4
gpt4 key购买 nike

使用此解决方案将数组插入数据库 Insert array into MySQL database with PHP产生 MySql 错误。

我的代码:

// Create arrays and values
$columns = "`".implode("`, `",array_keys($value))."`";
$escaped_values = array_map('mysql_real_escape_string', array_values($value));
$values = implode("', '", $escaped_values);
// Print arrays and values
echo "<br><h4>Columns:</h4> <br>";
echo $columns;
echo "<br><h4>Values:</h4><br>";
echo $values;
echo "<br><h4>";
// Insert or update
$insert = mysql_query("INSERT INTO stocklink2 ($columns) VALUES ('$values') ON DUPLICATE KEY UPDATE ($columns) = ('$values'); ");
if($insert === FALSE) {
die(mysql_errno($link).mysql_error()); // TODO: better error handling
}
echo "</h4>";

代码输出:

Columns:

`RecNo`, `Style`, `Upper`, `Split`, `Stocktype`, `Lineno`, `Upper2`,
`Upper3`, `Upper4`, `Upper5`, `Bottom`, `Price`, `Comments`, `Comments2`,
`Stocka`, `Stockb`, `Stockc`, `Stockd`, `Stocke`, `Stockf`, `Stockg`,
`Stockh`, `Stocki`, `Stockj`, `Stockk`, `Stockl`, `Stockm`, `Stockn`,
`Stocko`, `Resa`, `Resb`, `Resc`, `Resd`, `Rese`, `Resf`, `Resg`, `Resh`,
`Resi`, `Resj`, `Resk`, `Resl`, `Resm`, `Resn`, `Reso`, `Ordera`, `Orderb`,
`Orderc`, `Orderd`, `Ordere`, `Orderf`, `Orderg`, `Orderh`, `Orderi`,
`Orderj`, `Orderk`, `Orderl`, `Orderm`, `Ordern`, `Ordero`, `Availa`,
`Availb`, `Availc`, `Availd`, `Availe`, `Availf`, `Availg`, `Availh`,
`Availi`, `Availj`, `Availk`, `Availl`, `Availm`, `Availn`, `Availo`,
`Currsales`, `Sixmonth`, `Size25s`, `Size30s`, `Size35s`, `Size40s`,
`Size45s`, `Size50s`, `Size55s`, `Size60s`, `Size65s`, `Size70s`, `Size75s`,
`Size80s`, `Size85s`, `Size90s`, `Size95s`, `Size100s`, `Size105s`,
`Size110s`, `Size115s`, `Size120s`, `Size125s`, `Size130s`, `Size135s`,
`Size140s`, `Size145s`, `Size150s`, `Size20c`, `Size25c`, `Size015c`,
`Size10c`, `Size30c`, `Size35c`, `Size40c`, `Size45c`, `Size50c`, `Size55c`,
`Size60c`, `Size65c`, `Size70c`, `Size75c`, `Size80c`, `Size85c`, `Size90c`,
`Size95c`, `Size100c`, `Size105c`, `Size110c`, `Size115c`, `Size120c`,
`Size125c`, `Size130c`, `Size135c`, `Size140c`, `Size145c`, `Size150c`
Values:


2', 'BNO5839 ', 'CCA ', '8', ' ', '0', ' ', ' ', ' ', ' ', 'BO ', '120', '
', ' ', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0',
'0', '0', '159', '2', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0',
'0', '0', '-328', '-2', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0',
'0', '0', '0', '-418', '-4', '0', '0', '0', '0', '0', '0', '0', '0', '0',
'0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0',
'0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0',
'0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0',
'0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0',
'0 1064You have an error in your SQL syntax; check the manual that
corresponds to your MariaDB server version for the right syntax to use near
'(`RecNo`, `Style`, `Upper`, `Split`, `Stocktype`, `Lineno`, `Upper2`,
`Upper3`, ' at line 1

“Upper”列是保留字,因此我必须转义列名称。当我更新单个字段时,这有效,但不适用于字段数组。

最佳答案

这是错误的:

ON DUPLICATE KEY UPDATE ($columns) = ('$values'); ");
^^^^^^^^^^^^^^^^^^^^^

你正在构建

on duplicate key update (field, field, field, etc..) = (value, value value, ...)

可以像这样并行更新/分配值。你必须做个人

on duplicate key update field1=value1, field2=value2, etc...

关于php - 使用 array_keys() 进行插入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36849284/

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