草庐IT

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

coder 2024-05-03 原文

我收到以下错误: fatal error :无法在第 47 行的 C:\Program Files (x86)\Zend\Apache2\htdocs\includes\class-insert.php 中重新分配自动全局变量 _POST 当试图访问我的 class-insert.php 页面(或我的大部分其他页面)时。 此代码来自以下视频:https://www.youtube.com/watch?v=Wgbmgf84ZOo可以在这里找到:http://www.johnmorrisonline.com/lesson/how-to-create-a-social-network-using-php/

class-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;
    ?>

谢谢!

最佳答案

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

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();

关于php - 无法重新分配自动全局变量 _POST,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22459557/

有关php - 无法重新分配自动全局变量 _POST的更多相关文章

  1. ruby-on-rails - 使用 Ruby on Rails 进行自动化测试 - 最佳实践 - 2

    很好奇,就使用ruby​​onrails自动化单元测试而言,你们正在做什么?您是否创建了一个脚本来在cron中运行rake作业并将结果邮寄给您?git中的预提交Hook?只是手动调用?我完全理解测试,但想知道在错误发生之前捕获错误的最佳实践是什么。让我们理所当然地认为测试本身是完美无缺的,并且可以正常工作。下一步是什么以确保他们在正确的时间将可能有害的结果传达给您? 最佳答案 不确定您到底想听什么,但是有几个级别的自动代码库控制:在处理某项功能时,您可以使用类似autotest的内容获得关于哪些有效,哪些无效的即时反馈。要确保您的提

  2. ruby-on-rails - 由于 "wkhtmltopdf",PDFKIT 显然无法正常工作 - 2

    我在从html页面生成PDF时遇到问题。我正在使用PDFkit。在安装它的过程中,我注意到我需要wkhtmltopdf。所以我也安装了它。我做了PDFkit的文档所说的一切......现在我在尝试加载PDF时遇到了这个错误。这里是错误:commandfailed:"/usr/local/bin/wkhtmltopdf""--margin-right""0.75in""--page-size""Letter""--margin-top""0.75in""--margin-bottom""0.75in""--encoding""UTF-8""--margin-left""0.75in""-

  3. ruby-on-rails - 无法使用 Rails 3.2 创建插件? - 2

    我对最新版本的Rails有疑问。我创建了一个新应用程序(railsnewMyProject),但我没有脚本/生成,只有脚本/rails,当我输入ruby./script/railsgeneratepluginmy_plugin"Couldnotfindgeneratorplugin.".你知道如何生成插件模板吗?没有这个命令可以创建插件吗?PS:我正在使用Rails3.2.1和ruby​​1.8.7[universal-darwin11.0] 最佳答案 随着Rails3.2.0的发布,插件生成器已经被移除。查看变更日志here.现在

  4. ruby - 无法运行 Rails 2.x 应用程序 - 2

    我尝试运行2.x应用程序。我使用rvm并为此应用程序设置其他版本的ruby​​:$rvmuseree-1.8.7-head我尝试运行服务器,然后出现很多错误:$script/serverNOTE:Gem.source_indexisdeprecated,useSpecification.Itwillberemovedonorafter2011-11-01.Gem.source_indexcalledfrom/Users/serg/rails_projects_terminal/work_proj/spohelp/config/../vendor/rails/railties/lib/r

  5. ruby - 如何在续集中重新加载表模式? - 2

    鉴于我有以下迁移:Sequel.migrationdoupdoalter_table:usersdoadd_column:is_admin,:default=>falseend#SequelrunsaDESCRIBEtablestatement,whenthemodelisloaded.#Atthispoint,itdoesnotknowthatusershaveais_adminflag.#Soitfails.@user=User.find(:email=>"admin@fancy-startup.example")@user.is_admin=true@user.save!ende

  6. ruby-on-rails - 如何使用 instance_variable_set 正确设置实例变量? - 2

    我正在查看instance_variable_set的文档并看到给出的示例代码是这样做的:obj.instance_variable_set(:@instnc_var,"valuefortheinstancevariable")然后允许您在类的任何实例方法中以@instnc_var的形式访问该变量。我想知道为什么在@instnc_var之前需要一个冒号:。冒号有什么作用? 最佳答案 我的第一直觉是告诉你不要使用instance_variable_set除非你真的知道你用它做什么。它本质上是一种元编程工具或绕过实例变量可见性的黑客攻击

  7. ruby - 如何模拟 Net::HTTP::Post? - 2

    是的,我知道最好使用webmock,但我想知道如何在RSpec中模拟此方法:defmethod_to_testurl=URI.parseurireq=Net::HTTP::Post.newurl.pathres=Net::HTTP.start(url.host,url.port)do|http|http.requestreq,foo:1endresend这是RSpec:let(:uri){'http://example.com'}specify'HTTPcall'dohttp=mock:httpNet::HTTP.stub!(:start).and_yieldhttphttp.shou

  8. ruby-on-rails - 无法在centos上安装therubyracer(V8和GCC出错) - 2

    我正在尝试在我的centos服务器上安装therubyracer,但遇到了麻烦。$geminstalltherubyracerBuildingnativeextensions.Thiscouldtakeawhile...ERROR:Errorinstallingtherubyracer:ERROR:Failedtobuildgemnativeextension./usr/local/rvm/rubies/ruby-1.9.3-p125/bin/rubyextconf.rbcheckingformain()in-lpthread...yescheckingforv8.h...no***e

  9. Ruby Koans about_array_assignment - 非平行与平行分配歧视 - 2

    通过ruby​​koans.com,我在about_array_assignment.rb中遇到了这两段代码你怎么知道第一个是非并行赋值,第二个是一个变量的并行赋值?在我看来,除了命名差异之外,代码几乎完全相同。4deftest_non_parallel_assignment5names=["John","Smith"]6assert_equal["John","Smith"],names7end45deftest_parallel_assignment_with_one_variable46first_name,=["John","Smith"]47assert_equal'John

  10. ruby - 无法让 RSpec 工作—— 'require' : cannot load such file - 2

    我花了三天的时间用头撞墙,试图弄清楚为什么简单的“rake”不能通过我的规范文件。如果您遇到这种情况:任何文件夹路径中都不要有空格!。严重地。事实上,从现在开始,您命名的任何内容都没有空格。这是我的控制台输出:(在/Users/*****/Desktop/LearningRuby/learn_ruby)$rake/Users/*******/Desktop/LearningRuby/learn_ruby/00_hello/hello_spec.rb:116:in`require':cannotloadsuchfile--hello(LoadError) 最佳

随机推荐