@Marc乙建議我用include_once()的爲我的文件,而不是包含。這解決了我收到的錯誤。
然而,我仍然有我的驗證功能無法正常運行的問題。我發現它並沒有將我的變量傳遞給函數。
顯然,使用Drupal進行SOMETHING不允許通過簡單地在同一個函數中請求全局$變量來接收變量。我不得不聲明$變量是函數外的全局變量,其中$變量是我的數組。
原始的問題:其他
我有我創建的編輯公司的公司信息在我的數據庫的PHP文件。當我訪問drupal之外的頁面時,一切都很好用,但是當它包含在drupal頁面中時(甚至將代碼粘貼到drupal頁面中,我得到我的驗證錯誤,因爲它無法運行我的驗證功能(或者如果我刪除驗證,我的處理功能不工作),我可以註釋掉的功能,如果叫我處理腳本在一份聲明中像(isset($ _ POST [「提交」])),它的工作範圍內的Drupal,但我ð喜歡用我的功能
如果我去編輯在Drupal的網頁,我看到以下錯誤:
致命錯誤:不能重新聲明的validate()(先前聲明的在/ home /內容/ 6649484分之84/ HTML /公/型材/ drupal_commons /自定義/ editcompany/editcompany.php:416)在/home/content/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;
}
?>
有http://drupal.stackexchange.com/爲Drupal問題... – confiq 2012-01-30 18:00:17