Charles Williams
Charles Williams

Reputation: 573

PHP Form to MySQL - Array[] into MySQL Issue

I am working on a website that is supposed to allow Club Member registration. The information from a form is supposed to be stored into a MySQL database. I have already accomplished this with storing and reading from a XML style text file, but now I must convert it to MySQL. The following is code from part of the index.php:

    <div id="rightcol">
    <?php
        include_once("Membership_Class.php");

        $myClub = new Club("Localhost", "AMemberUser", "Pass123Word", "info_club");

        if(isset($_GET['BodyContent']))
        {
            if($_GET['BodyContent'] == "about")
            {
                $myClub -> DisplayAbout();
            }
            else if($_GET['BodyContent'] == "register")
            {
                $myClub -> DisplayRegistrationForm();
            }
            else if($_GET['BodyContent'] == "processregistration")
            {
                $myClub -> ProcessRegistrationForm();
            }
            else if($_GET['BodyContent'] == "members")
            {
                $myClub -> DisplayMembers();
            }
        }
    ?>
</div>

The next part is from my Membership_Class.php file:

function DisplayRegistrationForm()
    {
        echo("<h2>Become a Club Member</h2>

        <form name='register' method='post' action='Assign5_Solution.php?BodyContent=processregistration'>
            <table>
                <tbody>
                    <tr>
                        <td width='80px'>
                            <label>First Name: </label>
                        </td>
                        <td width='300'>
                            <input id='firstname' type='text' name='firstname' value='' required/>
                        </td>
                    </tr>
                    <tr>
                        <td>
                            <label>Last Name: </label>
                        </td>
                        <td>
                            <input id='lastname' type='text' name='lastname' value='' required/>
                        </td>
                    </tr>
                    <tr>
                        <td>
                            <label>Your Email: </label>
                        </td>
                        <td>
                            <input id='email' type='text' name='email' value='' required/>
                        </td>
                    </tr>
                    <tr>
                        <td>
                            <label>Gender: </label>
                        </td>
                        <td>
                            <input id='gender' type='radio' name='gender' value='male'>Male<br />
                            <input id='gender' type='radio' name='gender' value='female'>Female
                        </td>
                    </tr>
                    <tr>
                        <td>
                            <label>Interested in: </label>
                        </td>
                        <td id='check'>
                            <span style='font-weight: bold;'>Check All that Apply:</span><br />
                            <input id='interests' type='checkbox' name='interests[]' value='1'>Pizza Party<br />
                            <input id='interests' type='checkbox' name='interests[]' value='2'>Joining Study Groups<br />
                            <input id='interests' type='checkbox' name='interests[]' value='3'>Visiting Employer Sites<br />
                            <input id='interests' type='checkbox' name='interests[]' value='4'>Participating in Programming Competitions<br />
                            <input id='interests' type='checkbox' name='interests[]' value='5'>Building Games<br />
                            <input id='interests' type='checkbox' name='interests[]' value='6'>Becoming an Officer of the Club
                        </td>
                    </tr>
                    <tr>
                        <td colspan='2' style='text-align: center;'>
                            <input id='submit' type='submit' name='submit' value='Sign Up'/>
                        </td>
                    </tr>
                </tbody>
            </table>    
        </form>");
    }

    function ProcessRegistrationForm()
    {   
        $fname = $_POST['firstname'];
        $lname = $_POST['lastname'];
        $email = $_POST['email'];
        $gender = $_POST['gender'];
        $interests = $_POST['interests'];

        if(!isset($_POST['firstname']) || !isset($_POST['lastname']) || !isset($_POST['email']) ||
            ($_POST['firstname']) == '' || ($_POST['lastname']) == '' || ($_POST['email']) == '')
        {
            echo("Please enter your first / last name and email.");
        }   
        else
        {
            echo("<h2>Results</h2>");
            echo("<div id='results'>");
            echo $fname; 
            echo("<br />");
            echo $lname; 
            echo("<br />");
            echo $email;
            echo("<br />");
            echo $gender;
            echo("<br />");
            foreach($interests as $likes)
            {
                echo $likes . "<br />";
            }
            echo("<p style='font-weight: bold;'>Your data has been saved! We will contact you soon!</p>");
            echo("</div>");
        }

        $myClub = new Club("localhost","A340User","Pass123Word","info_club");

        $date = date("Y/m/d");

        $sql="INSERT INTO member
                    (`FirstName`,`LastName`,`Gender`,`Email`,`MemberSince`)
                VALUES
                    ('$fname','$lname','$gender','$email','$date');";

        $result = mysqli_query($this->Con,$sql);
        if($result == true) 
        {
            echo "Successful Insert<br />";
        }
        else
        {
            echo "Error Inserting class" . mysqli_error($this->Con) ." <br />";
        }

        for($i = 0; $i < sizeof($interests); $i++)
        {
            $interest = $interests[$i];

            $sql="INSERT INTO member_interests
                        (`Email`,`InterestID`)
                    VALUES
                        ('$email',$interest);";
        }

        $result = mysqli_query($this->Con,$sql);
        if($result == true) 
        {
            echo "Successful Insert<br />";
        }
        else
        {
            echo "Error Inserting class" . mysqli_error($this->Con) ." <br />";
        }

Right now I have gotten this to post to my database, but when I check multiple interests in the form data when I am testing it, it only posts one of the interests I check to the member_interests table. Obviously I have the interests[] array wrong somewhere, or the loop.

My database is called info_club with three tables: interest_type, member, and member_interests. The user email is the key id. In the interest_type table there is two columns, InterestID, and InterestDescription. In the member table there is Email, FirstName, LastName, Gender, and MemberSince. In my member_interests table there is an Email and InterestID columns.

I need to find out how to get multiple interests into the member_interests table.

Upvotes: 0

Views: 686

Answers (1)

Charles Williams
Charles Williams

Reputation: 573

Ok, I switched the $result = mysqli_query($this->Con,$sql); line into the for loop and it works well inserting into the database. So simple. So the correct code above is:

for($i = 0; $i < sizeof($interests); $i++)
        {
            $interest = $interests[$i];

            $sql="INSERT INTO member_interests
                        (`Email`,`InterestID`)
                    VALUES
                        ('$email',$interest);";

            $result = mysqli_query($this->Con,$sql);
        }    

Upvotes: 1

Related Questions