我明白了!
@Marc B建议我对我的文件使用include_once(),而不是include。这修复了我收到的错误。
但是,我仍然遇到验证功能无法正常运行的问题。我发现它没有将我的变量传递给函数。
显然,使用Drupal的SOMETHING不允许函数通过简单地在同一函数内请求全局$变量来接收变量。我不得不声明$变量在函数之外是全局的,其中$ variables是我的数组。
下面的原始问题:
我创建了一个PHP文件,用于编辑数据库中公司的公司信息。当我访问drupal之外的页面时,一切都运行得很漂亮,但是当它包含在drupal页面中时(甚至将代码粘贴到drupal页面中,我得到验证错误,因为它无法运行我的验证功能(或者如果我删除)验证,我的过程功能不起作用。。我可以注释掉函数并在一个语句中调用我的过程脚本,如 if(isset($ _ POST ['submit'])),它可以在Drupal,但我想使用我的功能。
如果我在Drupal中编辑页面,我会看到以下错误:
致命错误:无法在/ home / content中重新声明validate()(之前在/home/content/84/6649484/html/commons/profiles/drupal_commons/custom/editcompany/editcompany.php:416中声明)第452行/84/6649484/html/commons/profiles/drupal_commons/custom/editcompany/editcompany.php
(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;
}
?>
答案 0 :(得分:4)
您可以在Drupal中使用任何您想要的PHP函数。问题是你在include()
'd文件中定义了一个函数,该函数被多次包含。错误消息非常具体:“无法重新声明validate()`” - 一旦声明了一个函数,就不能重新声明它。
将该函数放入一个单独的库文件中,该文件通过include_once()
或require_once()
加载,以便只加载一次。
答案 1 :(得分:1)
Drupal可能有自己的validate()函数。 drupal的标准做法是在函数名前加上模块的名称,如下所示:editcompany_validate()。试试看,看看它是否能解决冲突。您应该能够进行简单的搜索和替换。
答案 2 :(得分:0)
您可以为所有函数添加前缀,以确保与Drupal内部没有冲突,或者如果使用5.3及更高版本,则可以使用PHP命名空间。
在调用全局函数时,使用命名空间可能需要对代码进行一些更改。但是,这是有据可查的。 http://ca3.php.net/manual/en/language.namespaces.fallback.php