mickburkejnr
mickburkejnr

Reputation: 3690

How to store multiple emails to a database in one form submission?

I'm building a system that allows an organisations members to refer newsletters they receive to their friends or family. What I thought about doing was to give these members a page that would allow them to add that persons name and email address in to a form. This form would be limited to 5 email names and email addresses, so you would essentially have five rows with two HTML form inputs per row.

My problem is that I don't know how I would reliably add this to a database? Would I need 5 individual forms that would then be submitted by one Form submit button?

EDIT
What I have at the moment is two tables. A member table and a referral table. When the member starts to the referral process their data is added to the member table, and then their ID is taken.

The member table consists of the following fields:

ID
Firstname
Surname
Email
Referrer

When the member add's their own details, I then take their ID and store it for use later.

Then, I want to have a form that looks like this:

Referral 1:  their email address   -  their name
Referral 2:  their email address   -  their name
Referral 3:  their email address   -  their name
Referral 4:  their email address   -  their name
Referral 5:  their email address   -  their name

This is so that I can add their information to the referral table, which would look like this will all the data added:

ID   |   Firstname   |   Surname   |   Email            |   Referrer
==========================================================================
1    |   Joe         |   Bloggs    | [email protected]     |    1
2    |   John        |   Bloggs    | [email protected]    |    1
3    |   James       |   Bloggs    | [email protected]   |    1
4    |   Jordan      |   Bloggs    | [email protected]  |    1
5    |   Jack        |   Bloggs    | [email protected]    |    1

This is what the end product should be.

Upvotes: 0

Views: 1058

Answers (2)

SeanWM
SeanWM

Reputation: 16989

you just need one form and then the user id of the person who is logged in.

<?php
if (!empty($_POST))
{
    for($i = 1; $i < 6; $i++)
    {
        mysql_query("INSERT INTO tbl (name, email, referrer) VALUES ('" . $_POST[name . $i] . "', '" . $_POST[email . $i] . "', '" . $_SESSION[user_id] . "')");
    }
}
?>
<html>
<head></head>
    <body>
        <form action="<?= $_SERVER['REQUEST_URI'] ?>" method="post">
            <?php
                for($i = 1; $i < 6; $i++)
                {
                    echo '<p>';
                    echo 'Name ' . $i;
                    echo '<input type="text" name="name' . $i . '" id="name' . $i . '">';
                    echo 'Email ' . $i;
                    echo '<input type="text" name="email' . $i . '" id="email' . $i . '">';
                    echo '</p>';
                }
            ?>
            <input type="submit" value="save">
        </form>
    </body>
</html>

Upvotes: 1

ragebunny
ragebunny

Reputation: 1760

You could have the extra email addresses in the signup for all with different names, for example, guest_email_1, guest_email_2 and so on.

Then on the database side you could either add some new fields to that users record to add these email addresses to or you could make a new table that is for storing these guest emails. In this table you could have the ID of the person that signed them up so you have a reference of where that email address came from if you should need it.

Is that what you're looking for?

Edit I think you might be looking for a way to get the ID of the user that was just added. So lets say a user summits the form and they're added to the database table but now we have to add their ID as well as the email addresses to the other table. If this is right and you are using mysqli then there is a function mysqli_insert_id Here is the manual for it:

[mysqli_inser_id - manual][1]

This function will get the last ID added to the database then you can use it to store the other email addresses with the ID of the original users.

That it?

Upvotes: 0

Related Questions