Mends.One

Multiple entries in SQL?

Mysql, Arrays, Php

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?

3
M
Milos Miskone Sretin
Jump to: Answer 1 Answer 2 Answer 3

Answers (3)

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))
3
K
Kermit

Comments:

Milos Miskone Sretin said:
Any help on how can I do that?
Kermit said:
@Miskone I would recommend you read this question. While it talks about SQL Server, it still applies here.

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

2
D
Dipesh Parmar

Comments:

markasoftware said:
why not just serialize() instead of json_encode? serialize is native to PHP, json is from javascript
Kermit said:
How would you go about searching for a phone number?
Dipesh Parmar said:
@Markasoftware i have also edited answer and mentioned serialize too.
Dipesh Parmar said:
@AarolamaBluenk for that read stackoverflow.com/questions/4116419/…
Kermit said:
@DipeshParmar It clearly states that you shouldn't serialize it and store it in a normalized fashion.

User serialize(), like you said. However, for a working add another button you need JavaScript. Here is a form:

<form action="action.php" method="post">
    <div id="phoneNumbers">
        <input type="text" value="home phone">: <input type="text" name="0-value" value="(xxx)xxx-xxxx">
    </div>
    <button onclick="addAnother();">Add Another Phone Number</button>
    <input type="submit>
</form>

Here is the javascript (put in head tag of your page):

<script type="text/javascript">
    var nums=0;
    function addAnother(){

        document.getElementById('phoneNumbers').innerHTML+='<input type="text" name="'+++nums+'-name">: <input type="text" name="'+nums+'-value">';

    }
</script>

Here is action.php:

<?php
$arrayOfNums=array();
foreach($_POST as $curVal){
    array_push($arrayOfNums,$curVal);
}
$serializedArray=serialize($arrayOfNums);
#now do whatever code you have to add serializedArray to your database. It is a string, so this is easy.
?>

Now you have a serialized array in your database. Just unserialize() it and you have an array like this, alternating between name and value: 'home tel','(324)444-4356','work tel','(444)546-5678' etc. This is untested, tell me if it fails.

1
M
markasoftware

Comments:

Milos Miskone Sretin said:
Any suggestions on how to do that in php if a have more things on that page except phone. So how can I set controller on if($this->request->is('post') to do that array serialize only if the phone values are saving?

Related Questions