PHP - Populating Display Input Data with MySQL Data from Table

I am trying to populate results with a description from my MySQL database table interest_type

My private function is

    private function Get_Interests_Types_From_DB()
    {
        $sql = "SELECT 
                    InterestID,
                    InterestDescription
                FROM
                    interest_type";

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

        while($row = mysqli_fetch_array($result))
        {
            $arrayResult[] = $row;
        }

        return ($arrayResult);
    }

The area of code that I am trying to use this function in is within a public function within the same class. The values from the form are numeric. I am trying to tie the $interests variable with the InterestID from the table and then print the InterestDescription, instead of the value of $interests.

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 />");

            $interestDescription = $this->Get_Interests_Types_From_DB();

            foreach($interests as $likes)
            {
                if($likes == $interestDescription['InterestID'])
                    echo $$interestDescription['InterestDescription'] . "<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);
        }

I am getting: Notice: Undefined index: InterestID in C:\xampp-portable\htdocs\A340\Assign5\Assign5_Club_Membership_Class.php on line 287

How can I print the InterestDescription instead of the value of $interests?

Answers


there:

while($row = mysqli_fetch_array($result))
{
   $arrayResult[] = $row;
}

now the $arrayResult, which is actually your $interestDescription, contains something like:

$interestDescription[0]["InterestID"] 
$interestDescription[1]["InterestID"]
...

and not $interestDescription[InterestID"]

UPDATE:

perhaps you need something like this (assuming that $interests holds an array of IDs)

        $interestDescription = $this->Get_Interests_Types_From_DB();
        foreach($interests as $likes) {
           foreach($interestDescription as $desc) {
              if($likes == $desc['InterestID']) {
                 echo $desc['InterestDescription'] . "<br />";
              }
           }
        }

Need Your Help

Errors with jQuery Mobile using a Single, Dynamic .html page which links to n JQM “pages”

jquery ajax dynamic jquery-mobile dynamic-content

I have a mobile site, which consists of a single HTML page (Mobile.html). The content is loaded dynamically into JQM formatted divs of data-role="page". The only static content on the page is the b...

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.