Reputation: 109
I have a database which consists of these two tables.
Clubs
-id* primary key
-name
Course -id
-clubid* foreign key
-name
======================================HTML=======================================
On my webpage I have a form which allows a user to register a club which by default starts with one course and allows them to click 'add course' this works by cloning the input field and changing the name.
So on submit, their maybe one, two or more courses that need to be added to my database
======================================PHP=========================================
So far I have been just allowing one course per club but there needs to be a one to many relationship.
if($_POST){
if(isset($_POST['register'])){
$clubid = $clubs->addClub($_POST['golfclub'], $_POST['email'],$_POST['password1'], $_POST['password1'],date('day',time()));
$clubs->addClubCourse($clubid, $_POST['course1']);
header('Location: ./index.php');
}
}
The addClubCourse
uses the most recently added id in the club table to enter the course at this point. How can I do an iteration to add say,
$_POST['course1'], $_POST['course2']) .... etc without knowing how many courses their will be.
====SQL===== Simply inserts the new course with the club id relevant to the registration.
Thanks for the help
Upvotes: 0
Views: 96
Reputation: 540
Or iterate through $_POST but validate also:
foreach($_POST as $key => $value){
if(preg_match('/^course\d+$/', $key)){
$courses[$key] = $value;
}
}
The array input is a more elegant solution though.
Upvotes: 0
Reputation: 64657
Name your html field so it becomes an array:
<input name="course[]">
etc.
Then, you can do:
foreach($_POST['course'] as $course) {
$clubs->addClubCourse($clubid, $course);
}
Upvotes: 2