php - 为什么我不能在 Drupal 中使用 PHP 函数?

标签 php mysql function drupal

我想通了!

@Marc B 建议我对我的文件使用 include_once(),而不是 include。这修复了我收到的错误。

但是,我仍然遇到验证功能无法正常运行的问题。我发现它没有将我的变量传递给函数。

显然,Drupal 的某些功能不允许函数通过简单地在同一函数内请求全局 $variables 来接收变量。我必须在函数外将 $variables 声明为全局变量,其中 $variables 是我的数组。

原始问题如下:



我创建了一个 PHP 文件,用于编辑数据库中公司的公司信息。当我访问 drupal 之外的页面时,一切都运行良好,但是当将它包含在 drupal 页面中时(或者甚至将代码粘贴到 drupal 页面中),我收到验证错误,因为它无法运行我的验证功能(或者如果我删除验证,我的流程功能不起作用)。我可以注释掉这些功能并在类似 if(isset($_POST['submit'])) 的语句中调用我的流程脚本,它在Drupal,但我想使用我的功能。

如果我在 Drupal 中编辑页面,我会看到以下错误:

fatal error :无法在/home/content 中重新声明 validate()(之前在/home/content/84/6649484/html/commons/profiles/drupal_commons/custom/editcompany/editcompany.php:416 中声明)/84/6649484/html/commons/profiles/drupal_commons/custom/editcompany/editcompany.php 第 452 行

(416 是我在验证函数中调用第一件事的地方,452 是相同的结束)

为什么在 Drupal 中包含 PHP 页面时不能使用函数?是什么导致我的功能挂断,有没有办法解决这个问题?这是我的代码:

<?php
//connect to database
include('db.php');



////////////////////////////////////////////////////
////////////////////////////////////////////////////
////                                            ////
//// Validate   /   Process Form                ////
////                                            ////
////////////////////////////////////////////////////
////////////////////////////////////////////////////

//set form variables
    $form['accountnumber'] = $_POST['accountnumber'];
    $form['companyname'] = $_POST['companyname'];
    $form['address'] = $_POST['address'];
    $form['address2'] = $_POST['address2'];
    $form['city'] = $_POST['city'];
    $form['state'] = $_POST['state'];
    $form['zip'] = $_POST['zip'];
    $form['beds'] = $_POST['beds'];



if(isset($_POST['submit'])) {

    //run the validate function
    $validated = validate();

    //if one of the validations returned false, let's declare $errors as true and we'll display a message
    if($validated[0] == false) {
        $v_errors = true;
    } else {
        $processed = process();

        //see if there were errors adding it to the database
        if($processed == false) {
            $db_errors = true;
        }

        if($processed == true) {
            $success = true;
        }
    }

}



?>





<?php
////////////////////////////////////////////////////
////////////////////////////////////////////////////
////                                            ////
//// Form                                       ////
////                                            ////
////////////////////////////////////////////////////
////////////////////////////////////////////////////


//choose company
?>

<form id="choosecompany" action="" method="get">


<?php

//get company from url
$company_id = $_GET['id'];



//get all active companies
$result = mysql_query("SELECT account_num AS 'a', name AS 'n', city AS 'c', state AS s FROM company_profiles WHERE type = 'Customer' ORDER BY name ASC");

?>

<select name="id" style="display: block; position: relative; margin: 5px auto;">

    <?
    while($row = mysql_fetch_array($result)) {
        ?>

        <option value="<?php echo $row['a']; ?>" <?php if($row['a'] == $company_id) { echo 'selected="selected"'; } ?>>
        <strong><?php echo  $row['n'] 
        . ' - ' . $row['c'];
        if($row['c']) { echo ', '; }
        echo $row['s']; ?></strong>
        <?php echo ' (' . $row['a'] . ')';?></option>

        <?php
    }
    ?>

</select>

<input type="submit" name="submit" value="Edit" style="display: block; position: relative; margin: 0 auto;" />


</form>


<?php
if($company_id) {
    //get company info from db
    $result = mysql_query("SELECT * FROM company_profiles WHERE account_num = '$company_id'");

    while($row = mysql_fetch_array($result)) {
        $form['accountnumber'] = $row['account_num'];
        $form['companyname'] = $row['name'];
        $form['address'] = $row['address'];
        $form['address2'] = $row['address2'];
        $form['city'] = $row['city'];
        $form['state'] = $row['state'];
        $form['zip'] = $row['zip'];
        $form['beds'] = $row['beds'];
    }

}

?>





<form id="editcompany" action="" method="post">

    <h1>Edit Company</h1>

    <?php

    if($v_errors) {
        echo '<span id="errors"> Company not updated. Please enter required information.';
        echo '</span>';
    }

    if($db_errors) {
        echo '<span id="errors"> Company not updated. Please contact your system admin. </span>';
    }

    if($success) {
        echo '<span id="success"> Company information successfully updated. </span>';
    }

    ?>


    <ul id="block1">
        <li id="accountnumber">
            <label>Account #</label>
            <input readonly type="text" name="accountnumber" value="<?php echo $form['accountnumber']; ?>" <?php if($validated[1] == 'error') { echo 'class="error"'; } ?> />
        </li>


        <li id="companyname">
            <label>Company Name</label>
            <input type="text" name="companyname" value="<?php echo $form['companyname']; ?>" <?php if($validated[2] == 'error') { echo 'class="error"'; } ?> />
        </li>



        <li id="address">
            <label>Address</label>
            <input type="text" name="address" value="<?php echo $form['address']; ?>" />
            <input type="text" name="address2" value="<?php echo $form['address2']; ?>" />
        </li>




        <li id="csz">
            <label>City, State, Zip</label>
            <input id="city" type="text" name="city" value="<?php echo $form['city']; ?>" <?php if($validated[3] == 'error') { echo 'class="error"'; } ?> />

            <input id="state" type="text" name="state" maxlength="2" value="<?php echo $form['state']; ?>" <?php if($validated[4] == 'error') { echo 'class="error"'; } ?> />

            <input id="zip" type="text" name="zip" maxlength="5" value="<?php echo $form['zip']; ?>" />
        </li>


    </ul>



    <ul id="block2">
        <li id="products">
            <label>Products</label>
            <ul>
           <?php 

           //get all products from database
            $getproducts = mysql_query("SELECT id, name, url FROM products ORDER BY weight ASC");

            while ($rowproducts = mysql_fetch_array($getproducts)) {

                $product_id = $rowproducts['id'];
                $product_name = $rowproducts['name'];
                $product_url = $rowproducts['url'];

                $getuserhasproduct = mysql_query("SELECT DISTINCT product_id FROM products_accounts WHERE account_number = '$form[accountnumber]' AND product_id = '$product_id'");
                $user_has_product = mysql_num_rows($getuserhasproduct);

                if($user_has_product){
                    $hasproduct = true;
                }



            //list all products 
            ?>
                <li>
                    <label><?php echo $product_name; ?></label>
                    <input type="checkbox" name="<?php echo $product_id; ?>" value="TRUE" <?php if($user_has_product) { echo 'checked'; } ?> />
                </li>
            <?php



            //end while
            }
            ?>



            </ul>
        </li>


        <li id="demographics">
            <ul>
                <li id="beds">
                    <label>Beds</label>
                    <input type="text" name="beds" value="<?php echo $form['beds']; ?>" />
                </li>

            </ul>
        </li>


    </ul>


    <input type="submit" name="submit" value="Update" />


</form>





<?php
////////////////////////////////////////////////////
////////////////////////////////////////////////////
////                                            ////
//// Validate Function                          ////
////                                            ////
////////////////////////////////////////////////////
////////////////////////////////////////////////////

function validate() {
    //get variables
    global $form;

    $v = true;

    //validate account number

    if(!$form['accountnumber']) {
        $v = false;
        $v1 = 'error';
    }

    if(!$newaccount) {
        $v5 = 'error';
    }

    //validate company name
    if(!$form['companyname']) {
        $v = false;
        $v2 = 'error';
    }

    //validate city
    if(!$form['city']) {
        $v = false;
        $v3 = 'error';
    }

    //validate state
    if(!$form['state']) {
        $v = false;
        $v4 = 'error';
    }

    $validated = array($v,$v1,$v2,$v3,$v4,$v5);
    return $validated;

}








////////////////////////////////////////////////////
////////////////////////////////////////////////////
////                                            ////
//// Process Function                           ////
////                                            ////
////////////////////////////////////////////////////
////////////////////////////////////////////////////

function process() {
    //get variables
    global $form;
    global $_POST;

    //set variables for clean entry into database
    $an = mysql_real_escape_string($form['accountnumber']);
    $n = mysql_real_escape_string($form['companyname']);
    $a = mysql_real_escape_string($form['address']);
    $a2 = mysql_real_escape_string($form['address2']);
    $c = mysql_real_escape_string($form['city']);
    $s = mysql_real_escape_string($form['state']);
    $z = mysql_real_escape_string($form['zip']);
    $b = mysql_real_escape_string($form['beds']);




    //get all products from database
            $getproducts = mysql_query("SELECT id, name, url FROM products ORDER BY weight ASC");

            while ($rowproducts = mysql_fetch_array($getproducts)) {

                $product_id = $rowproducts['id'];
                $product_name = $rowproducts['name'];
                $product_url = $rowproducts['url'];

                $getuserhasproduct = mysql_query("SELECT DISTINCT product_id FROM products_accounts WHERE account_number = '$form[accountnumber]' AND product_id = '$product_id'");
                $user_has_product = mysql_num_rows($getuserhasproduct);

                //if the user has the product, let's delete it if we need to delete it, otherwise leave it alone.
                if($user_has_product){

                    if($_POST[$product_id] == false) {
                        mysql_query("DELETE FROM products_accounts WHERE account_number = '$form[accountnumber]' AND product_id = '$product_id'");
                    }

                //if the user doesn't have the product, let's add it if we need to add it, otherwise leave it alone.
                } else {

                    if($_POST[$product_id] == true) {
                        mysql_query("INSERT INTO products_accounts (account_number, product_id) VALUES ('$form[accountnumber]', '$product_id')");
                    }
                }


            }




    $result = mysql_query("UPDATE company_profiles SET name = '$n', address = '$a', address2 = '$a2', city = '$c', state = '$s', zip = '$z', beds = '$b' WHERE account_num = '$an'");

    if(!$result) {
        $processed = false;
        die('Could not connect: ' . mysql_error());
    } else {
        $processed = true;
    }


    return $processed;

}



?>

最佳答案

您可以在 Drupal 中使用任何您想要的 PHP 函数。问题是您在 include() 文件中定义了一个函数,该文件被多次包含。错误消息非常具体:“无法重新声明 validate()`”- 函数一旦声明,您就无法重新声明它。

将函数放入一个单独的库文件中,该文件通过 include_once()require_once() 加载,因此它只加载一次。

关于php - 为什么我不能在 Drupal 中使用 PHP 函数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9068264/

相关文章:

php - "could not find driver"命令的 "new PDO()"错误,PHP 7.0.25

javascript - 嵌套的 Javascript 如何返回 "pass up" promise ?

php - Laravel 5.4 中的 Group By 替代方案

php - 如何解决 codeigniter 中的日历任务问题?

PHP+MySQL join 语句将多个值排列为 mysql_fetch 数组中的行

mysql - 从 2 个表中仅选择表 2 中的新条目

c - 不会进入我的功能

javascript - 从动态外部文件运行 Nodejs 函数

PHP __call 与 method_exists

php - 如何开始使用 php/mysql/jquery 中的饮食记录脚本