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:
以上がMySQL への挿入時に文字列と主キー ID を連結するにはどうすればよいですか?の詳細内容です。詳細については、PHP 中国語 Web サイトの他の関連記事を参照してください。