2017-04-11 84 views
1

的config.php不能包含在類中的PHP文件構建

<?PHP

define('DB_SERVER','localhost'); 
define('DB_USER','root'); 
DEFINE('DB_PASSWORD',''); 
DEFINE('DB','bank'); 

$conn = new mysqli(DB_SERVER, DB_USER, DB_PASSWORD, DB); 

// Check connection 
if ($conn->connect_error) { 
    die("Connection failed: " . $conn->connect_error); 
} 


?> 

edit_action.php

<?php 
    #Edit User code 
    print_r($_POST); 
    Class edit_user 
    { 
     public function __construct() 
     { 
      include 'config.php'; 

       #including config file 
    } 
     public function edit() 
     { 

      print_r($_POST); 

       #All the updated code defined here 
    } 
    } 

當我嘗試執行此它給出錯誤的代碼,不包括配置文件。

錯誤:未定義的變量:康恩在edit_action.php

+0

你可以分享你的'config.php'嗎? –

回答

3

您正在提交您的表單='edit_action.php',其中只有print_r($_POST),您尚未在此文件中調用任何其他內容。

你可以做的是增加對edit_action.php

$obj1 = new edit_user; 
$obj1->edit(); 

更新的代碼下這些行:

<?php 
#Edit User code 
Class edit_user 
{ 
    public function edit() 
    { 
     include 'config.php'; 
     print_r($_POST); 

      #All the updated code defined here 
} 
} 
$obj1 = new edit_user; 
$obj1->edit(); 

更新的答案,根據註釋請求。

<input type="hidden" value="edit" name='action' /> 

在你的窗體添加這,這將提交您的形式edit()。您可以將其更改爲edit_role以呼叫edit_role()

edit_action.php

<?php 
    #Edit User code 
    Class edit_user 
    { 
     public function run() 
     { 
      switch($this->post['action']) 
      { 
       case 'edit': 
        $this->edit(); 
        break; 

       case 'edit_role': 
        $this->edit_role(); 
        break; 
      } 
     } 

     public function edit() 
     { 
      include 'config.php'; 
      print_r($_POST); 

       #All the updated code defined here 
     } 

     public function edit_role() 
     { 
      #edit_role code goes here 
     } 
    } 

    $obj1 = new edit_user; 
    $obj1->run(); 

我希望這會幫助你。

快樂編碼!

+0

它確實有效,但如果我在同一個類中包含edit_role(),並且需要包含在其他某個函數中,那麼在這種情況下,如果對象是在同一個文件中創建的,那麼函數edit()和edit_role()都將包含。 –

+0

@JasshhAndrews 請檢查更新的答案。我希望這能幫到您。謝謝 –

+0

嘿,謝謝它的工作正常。 :) :) :)只是一個更多的事情如何在__construct()中包含一個文件。因爲它每次都會給我錯誤。 –

0

你是在PHP文件edit_action.php那裏沒有呼籲(new edit_user())->edit()所以這就是爲什麼它僅僅是打印$_POST

更改此提交表單:

<form class="w3-container " action='edit_action.php' method='post' name="form" id="form-e" role="form"> 

此:

<form class="w3-container " action='edit_user.php' method='post' name="form" id="form-e" role="form">