Oh no! Where's the JavaScript?
Your Web browser does not have JavaScript enabled or does not support JavaScript. Please enable JavaScript on your Web browser to properly view this Web site, or upgrade to a Web browser that does support JavaScript.
Sign In
Not a member yet? Click here to register.

User Field Dilema

First I realize that there are hundreds of posts in this forum dealing with user fields; I have read them ALL as well as most posts on PF 7.02.0x threads. I cannot find an answer to my problem. User fields that add a column to the users table are pretty straight forward and easy to configure. Fields that contain only a-tags/links to pull up another page/infusion are a little trickier but I don't have a problem with them anymore. However; I have been trying for days to fix a custom user field that accesses and INSERTS a record or DELETES a record in this foreign table, depending on the SAVED value of the user field which is a zero or a one. I'm currently at work and do not have access to any of my files so I cannot at the moment show the coding. The main problem seems to be WHERE in the user field to add this foreign table access coding? If someone can understand this just from my description, any tips/help would be appreciated.
If you can show your code and filename, we'll be able to figure it out together.
OK... 12:40am, I'm home now. Here is the userfield code for: 'user_newsletter_include.php'
CodeDownload  
<?php
/*-------------------------------------------------------+
| PHP-Fusion Content Management System
| Copyright © 2002 - 2008 Nick Jones
| http://www.php-fusion.co.uk/
+--------------------------------------------------------+
| Filename: user_newsletter_include.php
| Author: Terry Broullette (Grimloch)
+--------------------------------------------------------+
| This program is released as free software under the
| Affero GPL license. You can redistribute it and/or
| modify it under the terms of this license which you
| can read by viewing the included agpl.txt or online
| at www.gnu.org/licenses/agpl.html. Removal of this
| copyright header is strictly prohibited without
| written permission from the original author(s).
+--------------------------------------------------------*/
if (!defined("IN_FUSION")) { die("Access Denied"); }

if ($profile_method == "input") {
   $user_newsletter = isset($user_data['user_newsletter']) ? stripinput($user_data['user_newsletter']) : "";
   if ($this->isError()) { $user_newsletter = isset($_POST['user_newsletter']) ? stripinput($_POST['user_newsletter']) : $user_newsletter; }

   echo "<tr>\n";
   echo "<td class='tbl".$this->getErrorClass("user_newsletter")."'><label for='user_newsletter'>".$locale['uf_user_newsletter'].$required."</label></td>\n";
   echo "<td class='tbl".$this->getErrorClass("user_newsletter")."'>";
   echo "<label><input type='radio' id='user_newsletter' name='user_newsletter' value='1'".($user_newsletter == "1"?" checked='checked'":"")." />".$locale['uf_user_newsletter_yes']."</label>&nbsp;&nbsp;&nbsp;&nbsp;\n";
   echo "<label><input type='radio' id='user_newsletter' name='user_newsletter' value='0'".($user_newsletter == "0"?" checked='checked'":"")." />".$locale['uf_user_newsletter_no']."</label>\n";
   echo "</td></tr>\n";

   if ($required) { $this->setRequiredJavaScript("user_newsletter", $locale['uf_user_newsletter_error']); }

} elseif ($profile_method == "display") {
   if ($user_data['user_newsletter'] == 1) {
      echo "<tr>\n";
      echo "<td class='tbl1'>".$locale['uf_user_newsletter_desc']."</td>\n";
      echo "<td align='right' class='tbl1'>".$locale['uf_user_newsletter_sub1']."</td>\n";
      echo "</tr>\n";
   } else {
      echo "<tr>\n";
      echo "<td class='tbl1'>".$locale['uf_user_newsletter_desc']."</td>\n";
      echo "<td align='right' class='tbl1'>".$locale['uf_user_newsletter_sub2']."</td>\n";
      echo "</tr>\n";
   }
} elseif ($profile_method == "validate_insert"  || $profile_method == "validate_update") {
   // Get input data
   if (isset($_POST['user_newsletter']) && ($_POST['user_newsletter'] != "" || $this->_isNotRequired("user_newsletter"))) {
      // Set update or insert user data
      $this->_setDBValue("user_newsletter", stripinput(trim($_POST['user_newsletter'])));
      }
include INFUSIONS."news_letter_panel/infusion_db.php";
$user = dbquery("SELECT * FROM ".DB_USERS." WHERE user_id='".$_GET['user']."'");
if (dbrows($user)) { $user_data = dbarray($user); }
if (isset($user_data['user_newsletter']) == 1) {
   $result = dbquery("INSERT INTO ".DB_NEWS_LETTER_SUBS." VALUES('', '".$user_data['user_email']."','".$user_data['user_name']."','1','0','time()','1')");
} elseif (isset($user_data['user_newsletter']) == 0)  {
   $result = dbquery("DELETE FROM ".DB_NEWS_LETTER_SUBS." WHERE nl_sub_mail='".$user_data['user_email']."'");

   } else { $this->_setError("user_newsletter", $locale['uf_user_newsletter_error'], true); }
}
?>


This works all day long as far as setting the db_users 'user_newsletter' field to 1 or 0. But the 'news_letter_subs' code never gets executed and it does produce errors in the log.

Here is the code for 'user_newsletter_include_var.php':
CodeDownload  
<?php
/*-------------------------------------------------------+
| PHP-Fusion Content Management System
| Copyright (C) PHP-Fusion Inc
| https://www.php-fusion.co.uk/
+--------------------------------------------------------+
| Filename: user_newsletter_include_var.php
| Author: Terry Broullette (Grimloch)
+--------------------------------------------------------+
| This program is released as free software under the
| Affero GPL license. You can redistribute it and/or
| modify it under the terms of this license which you
| can read by viewing the included agpl.txt or online
| at www.gnu.org/licenses/agpl.html. Removal of this
| copyright header is strictly prohibited without
| written permission from the original author(s).
+--------------------------------------------------------*/
if (!defined("IN_FUSION")) { die("Access Denied"); }

// Version of the user fields api
$user_field_api_version = "1.01.00";

$user_field_name = $locale['uf_user_newsletter'];
$user_field_desc = $locale['uf_user_newsletter_desc'];
$user_field_dbname = "user_newsletter";
$user_field_group = 3;
$user_field_dbinfo = "TINYINT(1) NOT NULL DEFAULT '0'";
?>


The purpose of this userfield is to allow subscribing to my newsletters at registration time.
Line 52:
CodeDownload  

$user = dbquery("SELECT * FROM ".DB_USERS." WHERE user_id='".$_GET['user']."'");
if (dbrows($user)) { $user_data = dbarray($user); }



wrong GET.

It's $_GET['lookup']

Please make sure you sanitize it, that code is extremely vulnerable esp use in v7. v9 have a line.

CodeDownload  

if (!isnum($_GET['lookup'])) redirect(BASEDIR.'index.php');



However, for your case, you should have done this bare minimum

CodeDownload  

$this_user_id = stripinput($_GET['lookup']);
$user = dbquery("SELECT * FROM ".DB_USERS." WHERE user_id='$this_user_id");
if (dbrows($user)) { $user_data = dbarray($user); }



In v9, you can do this in a flash:

CodeDownload  
$user = fusion_get_user($_GET['lookup']);

OK. I changed it to this:
CodeDownload  
require_once INFUSIONS."news_letter_panel/infusion_db.php";
$this_user_id = stripinput($_GET['lookup']);
$user = dbquery("SELECT * FROM ".DB_USERS." WHERE user_id='$this_user_id'");
if (dbrows($user)) { $user_data = dbarray($user); }
   if ($user_data['user_newsletter'] == 1) {
   $result = dbquery("INSERT INTO ".DB_NEWS_LETTER_SUBS." VALUES('', '".$user_data['user_email']."','".$user_data['user_name']."','1','0','time()','1')");
} elseif ($user_data['user_newsletter'] == 0) {
   $result = dbquery("DELETE FROM ".DB_NEWS_LETTER_SUBS." WHERE nl_sub_mail='".$user_data['user_email']."'");
   }


Still doesn't work and I get these errors:
CodeDownload  
public_html/maincore.php
mysql_fetch_assoc() expects parameter 1 to be resource, boolean given Line: 273

user_fields/user_newsletter_include.php
Undefined variable: user_data Line: 57

user_fields/user_newsletter_include.php
Undefined variable: user_data Line: 56

user_fields/user_newsletter_include.php
Undefined variable: user_data Line: 54

user_fields/user_newsletter_include.php
Undefined index: lookup Line: 51



lookup only appears in maincore 1 time and it's part of the profile_link setup.
What I don't understand is why I can't use the variables that are already established in if ($profile_method == "input") { and in elseif ($profile_method == "display") { ... is it a problem of scope or is there another reason?

Merged on Oct 23 2017 at 11:42:18:
This is what I have now after looking at 'profile.php' and using if (isset($_GET['lookup']) && isnum($_GET['lookup'])) ...
CodeDownload  
} elseif ($profile_method == "validate_insert"  || $profile_method == "validate_update") {
   // Get input data
   if (isset($_POST['user_newsletter']) && ($_POST['user_newsletter'] != "" || $this->_isNotRequired("user_newsletter"))) {
      // Set update or insert user data
      $this->_setDBValue("user_newsletter", stripinput($_POST['user_newsletter']));
      }
if (isset($_GET['lookup']) && isnum($_GET['lookup'])) {
$this_user_id = stripinput($_GET['lookup']);
$user = dbquery("SELECT * FROM ".DB_USERS." WHERE user_id='$this_user_id'");
if (dbrows($user)) { $user_data = dbarray($user); }
include INFUSIONS."news_letter_panel/infusion_db.php";
   if ($user_data['user_newsletter'] == 1) {
   $result = dbquery("INSERT INTO ".DB_NEWS_LETTER_SUBS." (nl_sub_mail, nl_sub_name, nl_sub_stat, nl_admin_add, nl_sub_date, nl_sub_who) VALUES('', '".$user_data['user_email']."','".$user_data['user_name']."','1','0','time()','1')");
} elseif ($user_data['user_newsletter'] == 0) {
   $result = dbquery("DELETE FROM ".DB_NEWS_LETTER_SUBS." WHERE nl_sub_mail='".$user_data['user_email']."'");
      }
   }
}


This way totally solves the errors in the error log but: it still doesn't work.
CodeDownload  
<?php
/*-------------------------------------------------------+
| PHP-Fusion Content Management System
| Copyright © 2002 - 2008 Nick Jones
| http://www.php-fusion.co.uk/
+--------------------------------------------------------+
| Filename: user_newsletter_include.php
| Author: Terry Broullette (Grimloch)
+--------------------------------------------------------+
| This program is released as free software under the
| Affero GPL license. You can redistribute it and/or
| modify it under the terms of this license which you
| can read by viewing the included agpl.txt or online
| at www.gnu.org/licenses/agpl.html.  Removal of this
| copyright header is strictly prohibited without
| written permission from the original author(s).
+--------------------------------------------------------*/
if (!defined("IN_FUSION")) { die("Access Denied"); }

if ($profile_method == "input") {
   $user_newsletter = isset($user_data['user_newsletter']) ? stripinput($user_data['user_newsletter']) : "";
   if ($this->isError()) { $user_newsletter = isset($_POST['user_newsletter']) ? stripinput($_POST['user_newsletter']) : $user_newsletter; }

   echo "<tr>\n";
   echo "<td class='tbl".$this->getErrorClass("user_newsletter")."'><label for='user_newsletter'>".$locale['uf_user_newsletter'].$required."</label></td>\n";
   echo "<td class='tbl".$this->getErrorClass("user_newsletter")."'>";
   echo "<label><input type='radio' id='user_newsletter' name='user_newsletter' value='1'".($user_newsletter == "1"?" checked='checked'":"")." />".$locale['uf_user_newsletter_yes']."</label>&nbsp;&nbsp;&nbsp;&nbsp;\n";
   echo "<label><input type='radio' id='user_newsletter' name='user_newsletter' value='0'".($user_newsletter == "0"?" checked='checked'":"")." />".$locale['uf_user_newsletter_no']."</label>\n";
   echo "</td></tr>\n";

   if ($required) { $this->setRequiredJavaScript("user_newsletter", $locale['uf_user_newsletter_error']); }

} elseif ($profile_method == "display") {
   if ($user_data['user_newsletter'] == 1) {
      echo "<tr>\n";
      echo "<td class='tbl1'>".$locale['uf_user_newsletter_desc']."</td>\n";
      echo "<td align='right' class='tbl1'>".$locale['uf_user_newsletter_sub1']."</td>\n";
      echo "</tr>\n";
   } else {
      echo "<tr>\n";
      echo "<td class='tbl1'>".$locale['uf_user_newsletter_desc']."</td>\n";
      echo "<td align='right' class='tbl1'>".$locale['uf_user_newsletter_sub2']."</td>\n";
      echo "</tr>\n";
   }
} elseif ($profile_method == "validate_insert"  || $profile_method == "validate_update") {
   // Get input data
   if (isset($_POST['user_newsletter']) && ($_POST['user_newsletter'] != "" || $this->_isNotRequired("user_newsletter"))) {
      // Set update or insert user data
      $this->_setDBValue("user_newsletter", stripinput(trim($_POST['user_newsletter'])));
      }
if (iMEMBER) {
require_once INFUSIONS."news_letter_panel/infusion_db.php";

   
   if (!empty($_GET['lookup']) && ($user_data['user_id'] == $userdata['user_id'])){
      if ($user_data['user_newsletter'] == 1) {
         $result = dbquery("INSERT INTO ".DB_NEWS_LETTER_SUBS." VALUES('', '".$user_data['user_email']."', '".$user_data['user_name']."', '1', '0', '".time()."', '1')");
      } elseif ($user_data['user_newsletter'] == 0) {
         $result = dbquery("DELETE FROM ".DB_NEWS_LETTER_SUBS." WHERE nl_sub_mail='".$user_data['user_email']."'");
      } else { $this->_setError("user_newsletter", $locale['uf_user_newsletter_error'], true); }
   
   }
}

}
?>

@karrak
Thought maybe you were onto something there. I tried it; still doesn't work as far as changing the news_letter_subs table.
CodeDownload  
<?php
/*-------------------------------------------------------+
| PHP-Fusion Content Management System
| Copyright © 2002 - 2008 Nick Jones
| http://www.php-fusion.co.uk/
+--------------------------------------------------------+
| Filename: user_newsletter_include.php
| Author: Terry Broullette (Grimloch)
+--------------------------------------------------------+
| This program is released as free software under the
| Affero GPL license. You can redistribute it and/or
| modify it under the terms of this license which you
| can read by viewing the included agpl.txt or online
| at www.gnu.org/licenses/agpl.html.  Removal of this
| copyright header is strictly prohibited without
| written permission from the original author(s).
+--------------------------------------------------------*/
if (!defined("IN_FUSION")) { die("Access Denied"); }

if ($profile_method == "input") {
   $user_newsletter = isset($user_data['user_newsletter']) ? stripinput($user_data['user_newsletter']) : "";
   if ($this->isError()) { $user_newsletter = isset($_POST['user_newsletter']) ? stripinput($_POST['user_newsletter']) : $user_newsletter; }

   echo "<tr>\n";
   echo "<td class='tbl".$this->getErrorClass("user_newsletter")."'><label for='user_newsletter'>".$locale['uf_user_newsletter'].$required."</label></td>\n";
   echo "<td class='tbl".$this->getErrorClass("user_newsletter")."'>";
   echo "<label><input type='radio' id='user_newsletter' name='user_newsletter' value='1'".($user_newsletter == "1"?" checked='checked'":"")." />".$locale['uf_user_newsletter_yes']."</label>&nbsp;&nbsp;&nbsp;&nbsp;\n";
   echo "<label><input type='radio' id='user_newsletter' name='user_newsletter' value='0'".($user_newsletter == "0"?" checked='checked'":"")." />".$locale['uf_user_newsletter_no']."</label>\n";
   echo "</td></tr>\n";

   if ($required) { $this->setRequiredJavaScript("user_newsletter", $locale['uf_user_newsletter_error']); }

} elseif ($profile_method == "display") {
   if ($user_data['user_newsletter'] == 1) {
      echo "<tr>\n";
      echo "<td class='tbl1'>".$locale['uf_user_newsletter_desc']."</td>\n";
      echo "<td align='right' class='tbl1'>".$locale['uf_user_newsletter_sub1']."</td>\n";
      echo "</tr>\n";
   } else {
      echo "<tr>\n";
      echo "<td class='tbl1'>".$locale['uf_user_newsletter_desc']."</td>\n";
      echo "<td align='right' class='tbl1'>".$locale['uf_user_newsletter_sub2']."</td>\n";
      echo "</tr>\n";
   }
} elseif ($profile_method == "validate_insert"  || $profile_method == "validate_update") {
   // Get input data
   if (isset($_POST['user_newsletter']) && ($_POST['user_newsletter'] != "" || $this->_isNotRequired("user_newsletter"))) {
      // Set update or insert user data
      $this->_setDBValue("user_newsletter", stripinput(trim($_POST['user_newsletter'])));

      if (iMEMBER) {
         require_once INFUSIONS."news_letter_panel/infusion_db.php";

         if (!empty($_GET['lookup']) && ($user_data['user_id'] == $userdata['user_id'])){
            if ($_POST['user_newsletter'] == 1 && $user_data['user_newsletter'] != 1) {
                  $result = dbquery("INSERT INTO ".DB_NEWS_LETTER_SUBS." VALUES('', '".$user_data['user_email']."', '".$user_data['user_name']."', '1', '0', '".time()."', '1')");
            } elseif ($_POST['user_newsletter'] == 0) {
                  $result = dbquery("DELETE FROM ".DB_NEWS_LETTER_SUBS." WHERE nl_sub_mail='".$user_data['user_email']."'");
            }
         }
      }
    }

}
?>

@karrak and Chan
Unfortunately this will not work either. I have tried many many combinations of coding and have come to the conclusion that (with the current 7.02.07 profile and userfield methods and coding), that it cannot be done. I am now seriously considering downgrading my (profile/userfield coding) to the 7.01.06 coding standard where it WILL work as I need it to work.
CodeDownload  
<?php
/*-------------------------------------------------------+
| PHP-Fusion Content Management System
| Copyright © 2002 - 2008 Nick Jones
| http://www.php-fusion.co.uk/
+--------------------------------------------------------+
| Filename: user_newsletter_include.php
| Author: Terry Broullette (Grimloch)
+--------------------------------------------------------+
| This program is released as free software under the
| Affero GPL license. You can redistribute it and/or
| modify it under the terms of this license which you
| can read by viewing the included agpl.txt or online
| at www.gnu.org/licenses/agpl.html.  Removal of this
| copyright header is strictly prohibited without
| written permission from the original author(s).
+--------------------------------------------------------*/
if (!defined("IN_FUSION")) { die("Access Denied"); }

if ($profile_method == "input") {
   $user_newsletter = isset($user_data['user_newsletter']) ? stripinput($user_data['user_newsletter']) : "";
   if ($this->isError()) { $user_newsletter = isset($_POST['user_newsletter']) ? stripinput($_POST['user_newsletter']) : $user_newsletter; }

   echo "<tr>\n";
   echo "<td class='tbl".$this->getErrorClass("user_newsletter")."'><label for='user_newsletter'>".$locale['uf_user_newsletter'].$required."</label></td>\n";
   echo "<td class='tbl".$this->getErrorClass("user_newsletter")."'>";
   echo "<label><input type='radio' id='user_newsletter' name='user_newsletter' value='1'".($user_newsletter == "1"?" checked='checked'":"")." />".$locale['uf_user_newsletter_yes']."</label>&nbsp;&nbsp;&nbsp;&nbsp;\n";
   echo "<label><input type='radio' id='user_newsletter' name='user_newsletter' value='0'".($user_newsletter == "0"?" checked='checked'":"")." />".$locale['uf_user_newsletter_no']."</label>\n";
   echo "</td></tr>\n";

   if ($required) { $this->setRequiredJavaScript("user_newsletter", $locale['uf_user_newsletter_error']); }

} elseif ($profile_method == "display") {
   if ($user_data['user_newsletter'] == 1) {
      echo "<tr>\n";
      echo "<td class='tbl1'>".$locale['uf_user_newsletter_desc']."</td>\n";
      echo "<td align='right' class='tbl1'>".$locale['uf_user_newsletter_sub1']."</td>\n";
      echo "</tr>\n";
   } else {
      echo "<tr>\n";
      echo "<td class='tbl1'>".$locale['uf_user_newsletter_desc']."</td>\n";
      echo "<td align='right' class='tbl1'>".$locale['uf_user_newsletter_sub2']."</td>\n";
      echo "</tr>\n";
   }
} elseif ($profile_method == "validate_insert"  || $profile_method == "validate_update") {
   // Get input data
   if (isset($_POST['user_newsletter']) && ($_POST['user_newsletter'] != "" || $this->_isNotRequired("user_newsletter"))) {
      // Set update or insert user data
      $this->_setDBValue("user_newsletter", stripinput(trim($_POST['user_newsletter'])));
      if (iMEMBER) {
         require_once INFUSIONS."news_letter_panel/infusion_db.php";
         $result = dbquery("SELECT * FROM ".DB_USERS." WHERE user_name='".$_POST['user_name']."'");
         if (dbrows($result)) { $user = dbarray($result); }

         if ($_POST['user_newsletter'] == 1 && $user['user_newsletter'] != 1) {
               dbquery("INSERT INTO ".DB_NEWS_LETTER_SUBS." VALUES('', '".$user['user_email']."', '".$user['user_name']."', '1', '0', '".time()."', '1')");
         } elseif ($_POST['user_newsletter'] == 0) {
               dbquery("DELETE FROM ".DB_NEWS_LETTER_SUBS." WHERE nl_sub_mail='".$user['user_email']."'");
         }
      }
    }

}
?>

Thread Information
Author
Replies
10 posts
Views
1,388 times
Last Post
Last updated on 2 years ago
You can view all discussion threads in this forum.
You cannot set up a bounty in this discussion thread.
You can start a new discussion thread in this forum.
You cannot reply in this discussion thread.
You cannot start on a poll in this forum.
You cannot upload attachments in this forum.
You can download attachments in this forum.
You cannot up or down-vote on the post in this discussion thread.
Users who participated in discussion: Chan, karrak, Grimloch