Skip to content

Commit

Permalink
Merge pull request #8355 from Sesquipedalian/2.1/insert_ignore_returning
Browse files Browse the repository at this point in the history
Fixes a bug when trying to return an ID from an INSERT IGNORE in MySQL
  • Loading branch information
Sesquipedalian authored Dec 6, 2024
2 parents 6952673 + 7aea4ea commit b9e0009
Showing 1 changed file with 15 additions and 11 deletions.
26 changes: 15 additions & 11 deletions Sources/Subs-Db-mysql.php
Original file line number Diff line number Diff line change
Expand Up @@ -840,26 +840,30 @@ function smf_db_insert($method, $table, $columns, $data, $keys, $returnmode = 0,
// the inserted value already exists we need to find the pk
else
{
$where_string = '';
$count2 = count($keys);
for ($x = 0; $x < $count2; $x++)
$where_string = [];

foreach ($columns as $column_name => $type)
{
$keyPos = array_search($keys[$x], array_keys($columns));
$where_string .= $keys[$x] . ' = ' . $data[$i][$keyPos];
if (($x + 1) < $count2)
$where_string .= ' AND ';
if (str_contains($type, 'string-'))
$where_string[] = $column_name . ' = ' . sprintf('SUBSTRING({string:%1$s}, 1, ' . substr($type, 7) . ')', $column_name);
else
$where_string[] = $column_name . ' = ' . sprintf('{%1$s:%2$s}', $type, $column_name);
}

$where_string = implode(' AND ', $where_string);

$request = $smcFunc['db_query']('', '
SELECT `' . $keys[0] . '` FROM ' . $table . '
WHERE ' . $where_string . ' LIMIT 1',
array()
SELECT ' . $keys[0] . '
FROM ' . $table . '
WHERE ' . $where_string . '
LIMIT 1',
array_combine($indexed_columns, $data[$i])
);

if ($request !== false && $smcFunc['db_num_rows']($request) == 1)
{
$row = $smcFunc['db_fetch_assoc']($request);
$ai = $row[$keys[0]];
$ai = (int) $row[$keys[0]];
}
}

Expand Down

0 comments on commit b9e0009

Please sign in to comment.