Hello the same issue has troubled me in the past.
I have a autocomplete script that the user chooses people to send them something.
I use to store multiple values in one row. That is the id of the selected people that the user chooses.
Thats not the proper thing to do and i changed it.
I have created another table that stores each value with the same id.

Here is an example of what i am trying to store.

* a_id = 1       to_user_id = 103
* a_id = 1       to_user_id = 107
* a_id = 1       to_user_id = 115

i mean that the a_id stays the same but the to_user_id will change upon the INSERT statement.

How do i do that?

here is the code

html
<input type="hidden" class="selected_ids" value="selected_ids" name="selected_ids[]" multiple="yes" id="selected_ids" />

php

    $s = array($_POST['selected_ids']);
    foreach($s as $sa)
    {
        $toUser = $sa;
    }

name="name[]"
helps with multiple selects or multiple fields with the same name

in your case i'd use a comma separated list and parse the posted value

I dont have a problem retrieving the data, i dont know to put them in the second table.

the second table has two columns
a_id
to_user_id

Whats the mysql syntax to store the same id but different to_user_id

It depends upon the table definition. If a_id is unique, then you need to alter the table structure so that a_id is no longer unique and the primary key is composed of a_id and to_user_id. Then, you can have multiple rows to the same a_id, but each a_id can only have one copy of to_user_id. SQL does not deal well with multiple values like you want unless you do this, or create a secondary table that is linked (joined) to the first one via a_id, and the first table has no to_user_id. Unless your table has other data associated with a_id (such as name, address, whatever), then the first suggestion I made would be optimal. If it does have such info, then my second suggestion would be appropriate.

Yes i did already did your second suggestion. What would be the script to insert the values?

Insert_Ads($title,$desc,$uid,$toUser)
This is the script to put data on table one
"INSERT INTO table_one(a_title,a_desc,user_id)VALUES('$title','$desc','$uid')"

how should the script will be to insert to table two the AUTO_INCREMENT a_id from table_one and the multiple values of $toUser?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.