0

I have a PHP function that adds a users id number and an event id number to a table via a form.

function:

public function register_user($activity_id, $user_id) 
        {
            try
            {
                $stmt = $this->db->prepare("INSERT INTO attendancelist (user_id, activity_id) 
                VALUES(:user_id, :activity_id)");

                $stmt->bindparam    (":user_id"     , $user_id);  
                $stmt->bindparam    (":activity_id" , $activity_id);
                $stmt->execute(); 
                return true;
            }
            catch(PDOException $e)
            {
                $this->error = 'Database error ' . $e->getMessage();
                return false;
            }    
        }

and the form:

<?php
            $stmt = $DB_con->prepare("SELECT * FROM activities");
            $stmt->execute([]);
            $rows = $stmt->fetchAll(PDO::FETCH_ASSOC);

            //Displays all of the currently available activities
            foreach($rows as $activity)
            {
            echo '<form action="" method="post">';
            echo '<input type="hidden"  name="activity_id"  value="',   $activity   ['activity_id'],    '">';
            echo '<input type="hidden"  name="user_id"      value="',   $userRow    ['user_id'],        '">';
            echo '<h2>', $activity['name'], '</h2>';
            echo '<p>', $activity['description'], '</p>';
            echo '<p>', $activity['start'], ' until ',  $activity['end'], '</p>';
            echo '<input type="submit" value="Register for Activity" name="register_button" />';
            echo '</form>';
            echo '<br />';
            }?>

When I try to run this I get this error: Undefined variable: activity_id on line 12, can anyone please tell me where I've gone wrong? thank you

Aaranihlus
  • 143
  • 2
  • 13

0 Answers0