diff --git a/Sources/Subs-Db-mysql.php b/Sources/Subs-Db-mysql.php index eaf9041e4d..34254851d4 100644 --- a/Sources/Subs-Db-mysql.php +++ b/Sources/Subs-Db-mysql.php @@ -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]]; } }