Page 1 of 1

passed variable not working correctly

Posted: Mon Oct 27, 2008 12:13 pm
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

Re: passed variable not working correctly

Posted: Mon Oct 27, 2008 12:29 pm
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.

Re: passed variable not working correctly

Posted: Mon Oct 27, 2008 4:31 pm
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.

Re: passed variable not working correctly

Posted: Mon Oct 27, 2008 5:34 pm
by steve_linn
the data is the database is char(17)

php recommends not using urldecode for global $_GET functions...

Re: passed variable not working correctly

Posted: Mon Oct 27, 2008 6:20 pm
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.

Re: passed variable not working correctly

Posted: Tue Oct 28, 2008 7:58 am
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?

Re: passed variable not working correctly

Posted: Tue Oct 28, 2008 9:51 am
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';

Re: passed variable not working correctly

Posted: Tue Oct 28, 2008 10:19 am
by steve_linn
that code did not work as expected....

still receive the error message 'no records retrieved'

Re: passed variable not working correctly

Posted: Tue Oct 28, 2008 10:23 am
by papa
Use mysql error instead and see what's up!

or die(mysql_error());

Re: passed variable not working correctly

Posted: Tue Oct 28, 2008 11:49 am
by steve_linn
tried replacing my custom error message with or die(mysql_error());
no error message shows up