PHP and MySQL question (ENCODE and DECODE)

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
hurdy gurdy
Forum Commoner
Posts: 40
Joined: Mon Jun 09, 2003 8:19 pm

PHP and MySQL question (ENCODE and DECODE)

Post by hurdy gurdy »

Hey All,

What I'm trying to do, in a nutshell, us update a table row with some new info that has to be encoded, then retrieve the correct info.

I'm having the dickens of a time trying to figure out why I'm having so much trouble with DECODE(ing) info kept in a mysql column. I use two different keys to access the information. Key1 seems to return the correct data (decoded properly) while info using Key2 returns garbled info.

:oops: I should probably preface this with the confession of being a PHP and MySQL noob. :oops:

Maybe my code will make more sense...

a simplified sample of the original form (apply.php):

Code: Select all

$key = 'key';
$intMemberID = $_GET['intMemberID'];

$selMember = "SELECT intMemberID, 
DECODE(strNameFirst, '$key'), 
DECODE(strNameLast, '$key'), 
DECODE(strNameMI, '$key'), 
DECODE(strAddStreet1, '$key'), 
DECODE(strAddStreet2, '$key'), 
DECODE(strModelPhone, '$key') 
strAddCity, 
strAddState, 
strAddZIP, 
FROM Member WHERE intMemberID = '$intMemberID'";

$qryMember = mysql_query($selMember);
$resMember = mysql_fetch_array($qryMember);

<form action="add_proc.php" method="post">
<input type="hidden" name="intMemberID" value="<?=$intMemberID; ?>">
First Name : <input type="text" name="strNameFirst" value="<?=$resMember['strNameFirst']; ?>">
Last Name : <input type="text" name="strNameLast" value="<?=$resMember['strNameLast']; ?>">
Middle Name : <input type="text" name="strNameMI" value="<?=$resMember['strNameMI']; ?>">
Street Address 1 : <input type="text" name="strAddStreet1" value="<?=$resMember['strAddStreet1']; ?>">
Street Address 2 : <input type="text" name="strAddStreet2" value="<?=$resMember['strAddStreet2']; ?>">
City : <input type="text" name="strAddCity" value="<?=$resMember['strAddCity']; ?>">
State : <input type="text" name="strAddState" value="<?=$resMember['strAddState']; ?>">
ZIP : <input type="text" name="strAddZIP" value="<?=$resMember['strAddZIP']; ?>">
<input type="submit" value="save">
</form>
when the form is submitted it goes to add_proc.php:

Code: Select all

$key = 'key';

$updateApplicant = "UPDATE Member SET 
strNameFirst = 'ENCODE ($_POST[strNameFirst], $key)', 
strNameLast = 'ENCODE ($_POST[strNameLast], $key)', 
strNameMI = 'ENCODE ($_POST[strNameMI], $key)', 
strAddStreet1 = 'ENCODE ($_POST[strAddStreet1], $key)', 
strAddStreet2 = 'ENCODE ($_POST[strAddStreet2], $key)', 
strAddCity = '$_POST[strAddCity]', 
strAddState = '$_POST[strAddState]', 
strAddZIP = '$_POST[strAddZIP]' 
WHERE intMemberID = '$_POST[intMemberID]'";

if ( mysql_query($updateApplicant) ) {
	header ("Location: apply.php?intMemberID=".$_POST[intMemberID]);
	exit;
} else {
	echo 'error : '.mysql_error();
}
On it's successful completion it goes back to the original form and fills out the text fields, but the data is garbled. Does anyone see where I messed up or could offer some troubleshooting tips or solutions? I'm about out of my mind on this... :)

Thanks!
HG
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

  1. You're missing a comma after the DECODE of strModelPhone.
  2. You're storing the string of ENCODE(blah blah), not the result of the encode (usage of strings)
hurdy gurdy
Forum Commoner
Posts: 40
Joined: Mon Jun 09, 2003 8:19 pm

Post by hurdy gurdy »

Thanks,

but here's where I sound like an idiot... I've tried variations on the ENCODE line like:

Code: Select all

..snip..
strNameFirst = ENCODE ($_POST[strNameFirst], '$key'), 
..snip..
and

Code: Select all

..snip..
strNameFirst = ENCODE ('$_POST[strNameFirst]', '$key'), 
..snip..
and I continually get this error:
#Error: 1064 SQLSTATE: 42000 (ER_PARSE_ERROR)

Message: %s near '%s' at line %d

What is the right way of doing this?

Thanks again for any help. :)

HG
hurdy gurdy
Forum Commoner
Posts: 40
Joined: Mon Jun 09, 2003 8:19 pm

Post by hurdy gurdy »

Am I guessing right that UPDATE doesn't like an array ?

It seems that if I put the first name into a variable like:

Code: Select all

$firstname = $_POST[strNameFirst];
then structure my query like:

Code: Select all

...snip...
UPDATE members SET
strNameFirst = ENCODE('$firstname', '$key'), 
...snip...
it seems to work.

Does anyone know why when I use the $_POST[strNameFirst], it doesn't work? It makes no sense to me. :)

Thanks
HG
User avatar
jayshields
DevNet Resident
Posts: 1912
Joined: Mon Aug 22, 2005 12:11 pm
Location: Leeds/Manchester, England

Post by jayshields »

try

Code: Select all

$_POST['varmame']
instead of

Code: Select all

$_POSt[varname]
Post Reply