passed variable not working correctly

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
steve_linn
Forum Newbie
Posts: 20
Joined: Thu Jul 10, 2008 1:10 pm

passed variable not working correctly

Post by steve_linn »

I have an error passing a variable
page 1 is set to pass $id (the id is a phone number..312-555-1212 for example)
Page 2 cannot retrieve the record
However, if there are no dashes between the numbers, it works.....

Page 1

Code: Select all

 
echo "<a href=\"index.php?content=enteraccount2&id=$id\">Enter Account Info</a><br>\n";
 
Page 2

Code: Select all

 
<?php
 
$accountid = $_GET['id'];
 
$query = "SELECT id,account,address1,city,state,zip,phone,rep from customers where id = $accountid";
 
$result = mysql_query($query) or die('Could not find that account');
$row = mysql_fetch_array($result, MYSQL_ASSOC) or die('No records retrieved');
 
$id = $row['id'];
$account = $row['account'];
$address1 = $row['address1'];
$city = $row['city'];
$state = $row['state'];
$zip = $row['zip'];
$phone = $row['phone'];
$rep = $row['rep'];
 
echo "<h1>$account</h1><br>\n";
echo "$address1<br>\n";
echo "$city, $state $zip<br>\n";
echo "$phone<br>\n";
echo "<br>\n";
echo "<b>Sales Rep:</b> $rep<br><br>\n";
 
echo "<a href=\"index.php?content=enteraccount2&id=$id\">Enter Account Info</a><br>\n";
echo "<a href=\"index.php\">Return Home</a><br>\n";
?>
 
not sure why it doesn't recognize the full number...
thanks
User avatar
aceconcepts
DevNet Resident
Posts: 1424
Joined: Mon Feb 06, 2006 11:26 am
Location: London

Re: passed variable not working correctly

Post by aceconcepts »

The dashes obviously seem to be causing a problem. Try using urlencode() before the variable is passed and urldecode() when you retrieve the variable.
User avatar
califdon
Jack of Zircons
Posts: 4484
Joined: Thu Nov 09, 2006 8:30 pm
Location: California, USA

Re: passed variable not working correctly

Post by califdon »

What data type is the id field in the table? If it is numeric, it will interpret your dashes as minus signs. id fields are commonly numeric, although there is nothing wrong with character data types for keys.
steve_linn
Forum Newbie
Posts: 20
Joined: Thu Jul 10, 2008 1:10 pm

Re: passed variable not working correctly

Post by steve_linn »

the data is the database is char(17)

php recommends not using urldecode for global $_GET functions...
User avatar
califdon
Jack of Zircons
Posts: 4484
Joined: Thu Nov 09, 2006 8:30 pm
Location: California, USA

Re: passed variable not working correctly

Post by califdon »

So when you click Submit, what does the url look like in the browser address window? Is the id value there at all? Is it possibly a number that might result from treating the dashes as minus signs? (if you enter: 123-456-7890, interpreting the dashes as minuses would give you -8223) Or is it something else?

If this doesn't point to the problem, you may need to assign the value to a variable and echo it back as debugging info, at various points in your script.
watson516
Forum Contributor
Posts: 198
Joined: Mon Mar 20, 2006 9:19 pm
Location: Hamilton, Ontario

Re: passed variable not working correctly

Post by watson516 »

steve_linn wrote: ...
Page 1

Code: Select all

 
echo "<a href=\"index.php?content=enteraccount2&id=$id\">Enter Account Info</a><br>\n";
 
...

Would you not have to use single quotes somewhere in there?
User avatar
aceconcepts
DevNet Resident
Posts: 1424
Joined: Mon Feb 06, 2006 11:26 am
Location: London

Re: passed variable not working correctly

Post by aceconcepts »

You could write it like this:

Code: Select all

echo '<a href="index.php?content=enteraccount2&id='.$id.'">Enter Account Info</a><br>\n';
steve_linn
Forum Newbie
Posts: 20
Joined: Thu Jul 10, 2008 1:10 pm

Re: passed variable not working correctly

Post by steve_linn »

that code did not work as expected....

still receive the error message 'no records retrieved'
User avatar
papa
Forum Regular
Posts: 958
Joined: Wed Aug 27, 2008 3:36 am
Location: Sweden/Sthlm

Re: passed variable not working correctly

Post by papa »

Use mysql error instead and see what's up!

or die(mysql_error());
steve_linn
Forum Newbie
Posts: 20
Joined: Thu Jul 10, 2008 1:10 pm

Re: passed variable not working correctly

Post by steve_linn »

tried replacing my custom error message with or die(mysql_error());
no error message shows up
Post Reply