douyin7416 2014-12-14 22:34
浏览 59
已采纳

将$ conn变量传递给函数时遇到问题

I am trying to set up a basic shopping cart application with a link to a database. Obviously I have the connection set up at the top of the code with:

        $dsn = "mysql:dbname=MYDATABASE";
        $username = "root";
        $password = "MYPASSWORD";
        try {
            global $conn;
            $conn = new PDO( $dsn, $username, $password );
            $conn->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );
        } catch ( PDOException $e ) {
          echo "Connection failed: " . $e->getMessage();
        }

And then I have my first step, which asks for various information, and then would save it to a database. The way that I am doing this is as follows:

            function processStep1() {
            $_SESSION["firstname"] = $_POST["firstname"];
            $_SESSION["lastname"] = $_POST["lastname"];
            $_SESSION["address"] = $_POST["address"];
            $_SESSION["city"] = $_POST["city"];
            $_SESSION["state"] = $_POST["state"];
            $_SESSION["zip"] = $_POST["zip"];


            $sql = "INSERT INTO customers VALUES ( '" . $_SESSION["firstname"] . "', '" . $_SESSION["lastname"] . "', '" . $_SESSION["address"] . "', '" . $_SESSION["state"] . "', '" . $_SESSION["city"] . "', '" . $_SESSION["zip"] . "' );";

            try {
              $conn->query( $sql );
            } catch ( PDOException $e ) {
              echo "Query failed: " . $e->getMessage();
            }

            displayStep2();
        }

When I run this page, however, I get the error:

"Notice: Undefined variable: conn" as well as "Fatal error: Call to a member function query() on a non-object".

I did manage to read online that this is because $conn is not being passed to the function, but I am not sure how I would go about passing it, because my method for calling the function processStep1() is as follows:

        if ( isset( $_POST["step"] ) and $_POST["step"] >= 1 and $_POST["step"] <= 5 ) {
          call_user_func( "processStep" . (int)$_POST["step"] );
        } else {
          displayStep1();
        }

So as to facilitate a multi-page system in a streamlined fashion, it simply checks for the step number whenever the form is submitted and then assembles the function call using call_user_func, so how would I pass $conn into that? I'm not certain.

Any help would be appreciated!

  • 写回答

1条回答 默认 最新

  • dongyou5068 2014-12-14 22:43
    关注

    Change the function to take $conn as a parameter:

    function procesStep1($conn) {
        ...
    }
    

    and pass the parameter when you call it:

    call_user_func("processStep" . (int)$_POST["step"], $conn);
    

    Also, in modern versions of PHP you don't need to use call_user_func, you can use variable functions:

    $processStep = "processStep" . (int)$_POST["step"];
    $processStep($conn);
    

    If you don't want to change all your functions like this, you can put

    global $conn;
    

    into the functions. It doesn't need to be in the code outside functions.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 #MATLAB仿真#车辆换道路径规划
  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建
  • ¥15 数据可视化Python
  • ¥15 要给毕业设计添加扫码登录的功能!!有偿
  • ¥15 kafka 分区副本增加会导致消息丢失或者不可用吗?
  • ¥15 微信公众号自制会员卡没有收款渠道啊
  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘