PHP函数未定义变量

时间:2016-04-05 18:17:14

标签: php

我有一个PHP函数,可以通过表单将用户ID号和事件ID号添加到表中。

功能:

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;
            }    
        }

和表格:

<?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 />';
            }?>

当我尝试运行此操作时,我收到此错误:第12行的未定义变量:activity_id,任何人都可以告诉我哪里出错了?谢谢

0 个答案:

没有答案