Reputation: 780
I figured it out!
@Marc B suggested that I use include_once() for my file, rather than include. This fixed the error that I was receiving.
However, I still had the problem of my validation function not running correctly. I figured out that it wasn't passing my variables to the function.
Evidently, SOMETHING with Drupal doesn't allow a function to receive variables by simply requesting global $variables inside of same function. I had to declare the $variables to be global outside of the function, where $variables is my array.
Original Question Below:
I have a PHP file that I created to edit company information for companies in my database. Everything works beautifully when I access the page outside of drupal, but when including it in a drupal page (or even pasting the code into a drupal page, I get my validation errors because it can't run my validation function (or if i remove validation, my process function does not work). I can comment out the functions and call my process script in a statement like if(isset($_POST['submit'])), and it works within Drupal, but I'd like to use my functions.
If I go to edit for the page in Drupal, I see the following error:
Fatal error: Cannot redeclare validate() (previously declared in /home/content/84/6649484/html/commons/profiles/drupal_commons/custom/editcompany/editcompany.php:416) in /home/content/84/6649484/html/commons/profiles/drupal_commons/custom/editcompany/editcompany.php on line 452
(416 is where I call the first thing in my validate function, 452 is the closing of same)
Why can't I use functions when including a PHP page in Drupal? What is causing the hangup with my functions, and is there a way to fix this? Here's my code:
<?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;
}
?>
Upvotes: 0
Views: 581
Reputation: 5431
You can prefix all of your functions with something unique to make sure there are no clashes with Drupal's internals, or you can use PHP namespaces if you are using 5.3 and above.
Using namespaces might require some changes in the code when you call global functions. However, this is well documented. https://www.php.net/manual/en/language.namespaces.fallback.php
Upvotes: 0
Reputation: 364
Drupal may have its own validate() function. Standard practice in drupal is to prepend your function names with the name of your module, like so: editcompany_validate(). Try that and see if it clears up the conflict. You should be able to do a simple search and replace.
Upvotes: 1
Reputation: 360792
You can use any PHP function you want within Drupal. The problem is that you're defining a function in an include()
'd file, which is being included multiple times. The error message is very specific: "Cannot redeclare validate()`" - once a function is declared, you can't redeclare it.
Put the function into a separate library file which gets loaded via include_once()
or require_once()
, so that it's only loaded ONE time.
Upvotes: 4