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中文網其他相關文章!