Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Multiple entries in SQL?

Tags:

arrays

php

mysql

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?

like image 206
Milos Miskone Sretin Avatar asked Nov 18 '25 10:11

Milos Miskone Sretin


2 Answers

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))
like image 146
Kermit Avatar answered Nov 19 '25 22:11

Kermit


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

like image 22
Dipesh Parmar Avatar answered Nov 20 '25 00:11

Dipesh Parmar



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!