Trouble returning custom field values from DB - php

I am integrated a php login system and created some custom fields in the user registration form which populate the database just fine. But I can't seem to return the custom field values from the database--only the fields that were built in like username. I created a field called displayname and want to show that on a page. I know the answer could be dependent on the code, but in general, shouldn't I be able to return any value regardless as long as the session is active and the connection to the database works and retuns the built in field data?
here is the php code on each page (that works for native username)
<?php
session_start();
if ( !isset($_SESSION['login']) || $_SESSION['login'] !== true) {
if(empty($_SESSION['access_token']) || empty($_SESSION['access_token']['oauth_token']) ||
empty($_SESSION['access_token']['oauth_token_secret'])){
if ( !isset($_SESSION['token'])) {
if ( !isset($_SESSION['fb_access_token'])) { ...etc.
Then I am trying to call my custom field "bio" with:
<?php echo "hi ".$_SESSION['username']; ?>
<?php echo "Your Bio ".$_SESSION['bio']; ?>
Why will username return the value but not my custom bio field? Again, I know it might depend on the script I am using, but in theory, shouldn't this work? Or is their some general thoughts anyone might have on why it doesn't return custom field values?

Session doesn't contains every field value. Suppose you are retrieving the bio from database and storing it in $bio. You must also store bio in session using $_SESSION['bio'] = $bio; Only then you will be able to use it everywhere until your session is destroyed.
Edit
Session Tutorial
http://www.tizag.com/phpT/phpsessions.php

Related

How to use PHP global variable for 2 functions?

I'm trying to create an interface where a workshop description can be updated on the server after it has been created by a user. There is a text field and a button that brings up the workshop by the number it was assigned. (This is also the name of the directory in the submissions/workshop# on the server). When I set the variable $workshopPath using this method, I want to be able to access this global variable when a text input is filled out with the string to update the title of the workshop. The $workshopPath is registering as an empty string in the 'updateTextItems' function, so it is writing the text file to the root directory instead of to the correct workshop directory. I thought I was correctly referencing the global variable within the functions, but this isn't working. I also tried using $GLOBALS['workshopPath'], but that isn't working either. Can someone help me figure out how to pass the variable to the second function? Thanks :-)
<?php
$workshopPath;
if (isset($_POST['gotoWorkshop'])) {
if (empty($_POST['numberInput'])) {
echo "Please enter a valid workshop number";
} else { //Assign the name variable form the posted field info if one was entered.
$workshopNumber = stripslashes($_POST['numberInput']);
global $workshopPath;
$workshopPath = "submissions/" . $workshopNumber . "/";
}
}
if (isset($_POST['updateTextItems'])) {
if ($_POST['titleInput']) {
//Assign the name variable form the posted field info if one was entered.
$titleInput = stripslashes($_POST['titleInput']);
}
if ($titleInput) {
global $workshopPath;
$titleFile = fopen($workshopPath . "title.txt", "w") or die("There was an error creating the title file.");
fwrite($titleFile, $titleInput);
fclose($titleFile);
}
}
?>
Do I understood you correct? The user fill in the a form for the workshop click submit and get an othewr form for the text? My guess is you sen to requests to the server. So $GLOBAL will not work for you. It only works per request and I think most time you do not realy need it. If you want to save some values across requests, you need a session when you start your session. After you have started your session with session_start() you can use $_SESSION[] to store and get your value

Is_unique form validation in codeigniter

I am working on a CMS backend,and I use Codeigniter to help with the development.
So, I am working on a user table. For example, I allow the backend admin to insert, update the user info.
First there is a table of the user, for each row there is an "edit" button. After clicking on the "edit" button, there is a form for that particular user.
e.g.
UserName: Mr. ABC
Email: abc#a.com
Password: 1234
Since I need to ensure the username is unique and email is unique, I need to use isUnique validation
However, the problem is, how to handle the case, "only the user modify the data, then add the is Unique rule"? Because if I don't change the username, then I will pass the original name to check , and it can not pass the is Unique check , thanks
$this->form_validation->set_rules('name', 'Username', 'trim|required|min_length[4]|is_unique[admin.username]');
You're sending the userID anyway to the server in order to update, You can run a query to check if that username/userID combo exists. if it does, that mean this user didn't change. if it doesn't, you need to check further with is_unique.
That's probably best implemented in a callback if you insist of using form_validation.
basically in your controller you'll two functions
function form_grabber()
{
//get your form and stuff. run this line :
$this->form_validation->set_rules('name', 'Username', 'call_back_isUserNameNotChanged[userID]');
//assuming userID comes from the form aswell.
}
function isUserNameNotChanged($username,$userID)
{
//query DB, return true if match exists or false if it doesn't
}
I don't use codeigniter much, so I'm not sure the exact code to go about this, but how about something like this:
$current_name = // current name in db
if ($this->input->post('name') != $current_name) {
// Name has been changed. Do validation.
}
...
Basically, just check if the posted name is different than what is already in the db. If so, run your validation and do your update. If it's the same, just ignore it.

How do i use a Fabrik (joomla component) form to connect to database table & search for match & return a result?

I am using the Joomla! component Fabrik.
The functionality I am trying to get is to have users buy a unique code from my website using e-junkie. On completion of payment, e-junkie will process a script that will send the code to a MySQL database table.
After buying, they will then be redirected to a page on the website to activate their code by filling in a form. In order to activate their code, they will need to enter data into three text fields:
First Name
Last Name
Unique Code
The form is already set up with Fabrik and displayed nicely on the website.
What I need now is when the form is submitted, I need it to connect to the database where the unique codes are stored, search the database table for the matching firstname,lastname and unique code and return a success message if a match is found and a failure message if no match is found.
Fabrik allows for PHP plugin scripts to run when the form is submitted, I will use a separate plugin for each field on the form.
So far I have come up with the snippet of code below which would be executed when the form is submitted. I am just attempting to validate the firstname field on the form at this stage just to get it working. once it is working i will duplicate the code and adapt it for the other fields (e.g lastname and uniquecode).
Problem is i just cant get it to work. It uses Joomla's JFactory/getDBO to connect to the database.
My Table name is travelcodes
The fields on the form are, firstname, lastname and uniquecode
the columns on the table are also firstname,lastname and uniquecode
$firstname = '{travelcodes___firstname}';
$value = '{tablename___firstname}';
$db =JFactory::getDBO();
$query=("SELECT firstname FROM travelcodes where field = '$firstname'");
$db->setQuery($query);
$response = $db->loadResult();
if ($response == $firstname) {
return true;
} else {
return false;
}
Any help with this would be much appreciated.
Thanks.
Bailey
I have no problem using the following setting
I think you will need to add JRequest::getVar before ('you_element') and have the setting to be onBeforeProcess
$db = JFactory::getDbo();
$date = date("Y-m-d H:i:s", time());
$value = JRequest::getVar('off_line_ap___offline_reason');
$user =& JFactory::getUser();
$uid = $user->id;
$db->setQuery("INSERT INTO d2_exchange_log (date_time, part, quantity, user_id, purpose) VALUES ('{$date}', '{$value}', '99','{$uid}', 'Test')");
$submissionsNo=$db->loadResult();

Postcode / Page Finder using HTML / PHP

I need to create a simple postcode / page finder for a site where a user will enter their postcode and be redirected to a page based on whether or not that location is covered by the company.
Basically the same as used on this site: http://www.dailypoppins.co.uk/
The fact it is a postcode is kind of irrelevant. For simplicity I'd probably just like a PHP file which takes the value entered into the form, checks it against a predefined list of values (Postcodes, there aren't that many currently covered) and then redirects the user to the correct page depending on the value / postcode entered.
Does anyone know how to do this?
This is a simple example..
$validPostcodes = array('123', '456', '789');
// get form value
$postcode = $_GET['postcode'];
if (isset($validPostcodes[$postcode])) {
header('Location: http://www.newsite.com/'.$postcode);
}
else {
echo 'Postcode does not exist';
}
Like I said..this is only a very simple example. I might be better to have the postcodes stored in a database and do some error checking on the form value.
I suggest to read about PHP form validation and PHP database access and of course HTML forms.
PHP database access 1,
PHP database access 2,
PHP Form handling,
HTML forms

Display data if usertype is admin otherwise display error message

I currently have a list of users in my mysql database. One of the columns is "type". I am trying to display certain data if type is equal to admin. If type is equal to anything else, it should just echo an error message.
Unfortunately, I have tried multiple methods but it just does not seem to be working out for me. Can anyone help me get this to work properly?
This is what I have, but obviously I am doing something wrong....
<?php
$usertype = $_SESSION['type'];
if ($usertype == "admin" ){
?>
admin stuff only goes here
<?
}
else
{
echo "not priveleged usertype";
}
?>
EDIT:
The following code works when displaying via username, however, I need content displayed by usertype, not the username.
<?php
if($_SESSION['user']['username'] == "oneoftheadminusernames" )
{
?>
Each page has to start with
<?php
#session_start();
?>
otherwise, php does not "see" the sessions contents. So that's probably it.
The # prevents the php error: A session has already been started... by the way.
Now, every page that uses the session must have this directive at the top.
At least, in a quick example, that reproduces your error perfectly.
If you are saving each logged in users type field in $_SESSION['type'] variable than the code you are writing is correct. Or if you are storing type in another variable than you that variable to check.
i have an idea like add a field EnableFlag in the table. if enablee flag is set to 1 consider it as a admin else as a User;

Categories