Concatenating a String and Primary Key ID during Insertion
Inserting data into a MySQL table can be straightforward, as demonstrated in the provided code. However, if you wish to create usernames as 'user' concatenated with an auto-incremental primary key ID (user1, user2, user3, etc.), a direct approach requires some consideration.
Concatenation limitations:
Alternative approach:
To address this limitation, you can perform the following steps:
$query = 'UPDATE `users` SET `username` = CONCAT("user", ?) WHERE `id` = ?'; $stmt = $mysqli->prepare($query); $stmt->bind_param("ii", $id, $id); $stmt->execute();
Considerations for non-AUTO_INCREMENT IDs:
If the user_id is not an AUTO_INCREMENT value, you can directly concatenate the 'user' string and the specified ID in your PHP code before passing it as a parameter in the insert statement.
Additional notes:
Das obige ist der detaillierte Inhalt vonWie verkette ich eine Zeichenfolge und eine Primärschlüssel-ID beim Einfügen in MySQL?. Für weitere Informationen folgen Sie bitte anderen verwandten Artikeln auf der PHP chinesischen Website!