How can I store multiple entries in SQL in one field? For example in field phone I want to let users enter more phones like private, home, business etc.
Any hellp on how can I create trat form and how to store data in sql?
I guess I can store them using serialize() function in php but how to make a form and collect that information?
Form:
Home Phone: 321321333
Other: 333213213
Add Another
Also I would like to make that they can edit name field so if users want to put Home Tel instead of Home Phone or Pager instead Other.
ANY HELP? PLEASE?
You do not store multiple values in a single field. You need a normalized structure that will allow for multiple phone numbers. A 1:n relationship would be appropriate here.
user (userId PK) userPhone (userId FK, phoneType, phoneNumber (userId, phoneType PK))
You can use json_encode() and json_decode() for saving and accessing data.
For saving.
$phone_number = array("Home Phone" => "321321333", "Other" => "333213213");
$encoded = json_encode($phone_number);
For accessing.
$result = mysqli_fetch_assoc($sql);
$decoded = json_decode( $result['phone_number_mysql_fields'] );
You can use the PHP serialize function to store arrays and objects in MySQL
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With