Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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.

share|improve this question
1  
Don't use spaces. $this -> HostName should be always written as $this->hostname –  bad_boy Apr 29 '13 at 4:48
    
    
Why are you creating a new Club object for every row retrieved from the database? Seems to me that you should be creating a Member object with the retrieved row and from there have methods for getting interests, etc. The way you have it now is very confusing. –  jraede Apr 29 '13 at 5:21
    
I didn't think the spaces mattered like other languages. I was doing it to keep it cleaned up. And I am not even sure what you mean jraede, like I said, I am kind of new to MySQL. I was trying to use the same model from a previous project. I probably do have it wrong. But getting it right is what I need guidance for. –  CharlWillia6 Apr 29 '13 at 12:40
    
Test the components separately. Put a filled insert statement at the SQL command window for starters. Figure out what the error is that's not getting reported properly –  hd1 Apr 30 '13 at 15:46

1 Answer 1

up vote 0 down vote accepted

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);
        }    
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.