Reputation: 902
I don't know the best way to title this, but basically what I want to do is something like this:
The user will be presented with a page that has a form. They'll have the ability to input their name, a bio, and then links to anything. I want to be able to allow the users to input as many links as they want. (Ex: "add another")
This would create another <input type="text>
tag that they can put the link into.
How would I handle that in PHP/MySQL to submit the query?
As of now I do something like:
update_info($name,$bio,$link1,$link2,$link3,$link4){...}
But, as you can see that's not efficient and the users are limited to the amount of links they can add.
Upvotes: 0
Views: 179
Reputation: 22862
You have many ways to deal with that, but probably the best and scalable one is a many-to-many relationship between your users table and a links to table, if more than one user can share de same link, of course. If that's not the case you just do a many-to-one relationship between the user table and the links table, so a user has many links and a link has one user.
Why a many-to-many or many-to-one?
Well this way a user can have has many links he wants to, it's easy to query and later, if you want to, you can make some metrics, like how any users share same link? What's the average number of links a user owns? And so on. You might not need this now, but is always good have some scalability.
Remember to have a many-to-many relationship you must have a pivot table.
You can obtain more info and see the differences between relationships here
Upvotes: 1
Reputation: 6081
In your HTML use an array to gather the links like:
<input type="text" name="link[]">
and in your php use for each to get all links:
foreach($_POST['link'] as $link)
or you can even convert array to string
$links = implode(",",$_POST['link']); //will give you all the links seperated by commas
Upvotes: 3
Reputation: 1076
noob's answer is closer to what you asked for.
I would create a <textarea>
and have the user add a line break to add a new item. PHP would then explode()
that value and make an array.
Upvotes: 0