Skip to content

Commit b9e0009

Browse files
Merge pull request #8355 from Sesquipedalian/2.1/insert_ignore_returning
Fixes a bug when trying to return an ID from an INSERT IGNORE in MySQL
2 parents 6952673 + 7aea4ea commit b9e0009

File tree

1 file changed

+15
-11
lines changed

1 file changed

+15
-11
lines changed

Sources/Subs-Db-mysql.php

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -840,26 +840,30 @@ function smf_db_insert($method, $table, $columns, $data, $keys, $returnmode = 0,
840840
// the inserted value already exists we need to find the pk
841841
else
842842
{
843-
$where_string = '';
844-
$count2 = count($keys);
845-
for ($x = 0; $x < $count2; $x++)
843+
$where_string = [];
844+
845+
foreach ($columns as $column_name => $type)
846846
{
847-
$keyPos = array_search($keys[$x], array_keys($columns));
848-
$where_string .= $keys[$x] . ' = ' . $data[$i][$keyPos];
849-
if (($x + 1) < $count2)
850-
$where_string .= ' AND ';
847+
if (str_contains($type, 'string-'))
848+
$where_string[] = $column_name . ' = ' . sprintf('SUBSTRING({string:%1$s}, 1, ' . substr($type, 7) . ')', $column_name);
849+
else
850+
$where_string[] = $column_name . ' = ' . sprintf('{%1$s:%2$s}', $type, $column_name);
851851
}
852852

853+
$where_string = implode(' AND ', $where_string);
854+
853855
$request = $smcFunc['db_query']('', '
854-
SELECT `' . $keys[0] . '` FROM ' . $table . '
855-
WHERE ' . $where_string . ' LIMIT 1',
856-
array()
856+
SELECT ' . $keys[0] . '
857+
FROM ' . $table . '
858+
WHERE ' . $where_string . '
859+
LIMIT 1',
860+
array_combine($indexed_columns, $data[$i])
857861
);
858862

859863
if ($request !== false && $smcFunc['db_num_rows']($request) == 1)
860864
{
861865
$row = $smcFunc['db_fetch_assoc']($request);
862-
$ai = $row[$keys[0]];
866+
$ai = (int) $row[$keys[0]];
863867
}
864868
}
865869

0 commit comments

Comments
 (0)