无法重新分配自动全局变量_POST

时间:2014-03-17 16:13:54

标签: php

我在尝试访问class-insert.php页面(或我的大多数其他页面)时收到以下错误:Fatal error: Cannot re-assign auto-global variable _POST in C:\Program Files (x86)\Zend\Apache2\htdocs\includes\class-insert.php on line 47。 此代码来自以下视频:https://www.youtube.com/watch?v=Wgbmgf84ZOo,可在此处找到:http://www.johnmorrisonline.com/lesson/how-to-create-a-social-network-using-php/

类insert.php

<?php
        require_once('class-db.php');

        if ( !class_exists('INSERT') ) {
            class INSERT {
                public function update_user($user_id, $postdata) {
                    global $db;

                    $table = 's_users';

                    $query = "
                                    UPDATE $table
                                    SET user_email='$postdata[user_email]', user_pass='$postdata[user_pass]', user_nicename='$postdata[user_nicename]'
                                    WHERE ID=$user_id
                                ";

                    return $db->update($query);
                }

                public function add_friend($user_id, $friend_id) {
                    global $db;

                    $table = 's_friends';

                    $query = "
                                    INSERT INTO $table (user_id, friend_id)
                                    VALUES ('$user_id', '$friend_id')
                                ";

                    return $db->insert($query);
                }

                public function remove_friend($user_id, $friend_id) {
                    global $db;

                    $table = 's_friends';

                    $query = "
                                    DELETE FROM $table 
                                    WHERE user_id = '$user_id'
                                    AND friend_id = '$friend_id'
                                ";

                    return $db->insert($query);
                }

                public function add_status($user_id, $_POST) {
                    global $db;

                    $table = 's_status';

                    $query = "
                                    INSERT INTO $table (user_id, status_time, status_content)
                                    VALUES ($user_id, '$_POST[status_time]', '$_POST[status_content]')
                                ";

                    return $db->insert($query);
                }

                public function send_message($_POST) {
                    global $db;

                    $table = 's_messages';

                    $query = "
                                    INSERT INTO $table (message_time, message_sender_id, message_recipient_id, message_subject, message_content)
                                    VALUES ('$_POST[message_time]', '$_POST[message_sender_id]', '$_POST[message_recipient_id]', '$_POST[message_subject]', '$_POST[message_content]')
                                ";

                    return $db->insert($query);
                }
            }
        }

        $insert = new INSERT;
    ?>

谢谢!

2 个答案:

答案 0 :(得分:3)

您不能使用superglobal作为函数参数的变量名。如果使用备用名称,您仍然可以将超全局传递给函数,或者您可以从参数列表中删除超全局,并且只需访问它,因为它随处可用。

List of superglobals

选项1:

function print_post( $post_data ){
    print_r($post_data);
}
print_post($_POST);

选项2 :(不良形式)

function print_post(){
    print_r($_POST);
}
print_post();

答案 1 :(得分:1)

我所做的就是删除函数arg中的$ _POST并解决问题。你可以从参数列表中删除超全局,无论如何都可以访问它,因为它随处可用。