469,917 Members | 1,724 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,917 developers. It's quick & easy.

An "Undefined Variable" Problem - or even more simple?

This has got to be one of those questions only a serious newbie would come up with....

Basicly: I select all the city names in the table and turn them into a pull down menu (code below)
Expand|Select|Wrap|Line Numbers
  1. print "<form action=\"update-city.php\" method=\"post\" name=\"theform\">";
  2. print "<SELECT id=\"city\">\n";
  3.  
  4.     while ($row =mysql_fetch_object($result)) {
  5.  
  6.     foreach ($row as $col=>$val){
  7.     print "<option value=\"\n";
  8.     print $val ;
  9.     print "\">";
  10.     print $val ;
  11.     print "</option>\n";
  12.     }
  13. }
  14. print "</select>\n";
  15. ?>
  16. <!-- Finish the form -->
  17. <input type="submit" name="action" value="Update City">
  18. </form>
  19.  
Then I'm playing with it (working up the way to update) but all the variables seem empty. (Here's code for a simple table)

Expand|Select|Wrap|Line Numbers
  1. $sql = "SELECT city_name, city_default_zip, seatac_price, boeing_price FROM char_city WHERE city_name = ('$city')";
  2. $result = mysql_query($sql) 
  3.   or die(mysql_error());
  4.  
  5. <table border="1">
  6.     <tr>
  7.         <td>City Name</td>
  8.         <td>Default Zip Code</td>
  9.         <td>Seatac Price</td>
  10.         <td>Boeing Price</td>
  11.     </tr>
  12. <? 
  13. while ($row =mysql_fetch_assoc($result)) {
  14. $lshit[] = $cityList[$row['id']] = $row['city_name'];
  15. $shit[] = $cityList[$row['id']] = $row['city_default_zip'];
  16. $mshit[] = $cityList[$row['id']] = $row['seatac_price'];
  17. $bshit[] = $cityList[$row['id']] = $row['boeing_price'];
  18.     print "<tr>\n";
  19.  
  20.     foreach ($row as $col=>$val){
  21.     print "<td>\n";
  22.     print $val ;
  23.     print "</td>\n";
  24.  
  25. }//end for each
  26.     print "</tr>\n";
  27. }//end while
  28. ?>
  29. </table>
As you can see - it doesn't get any more simple.... So why doesn't it work? And what's really got me whacked out... Why doesn't it work in IE 6 or 7 - Yet works just fine in FireFox?

I read the forum sticky before posting so for some more info...
I installed...
error_reporting(E_ALL);
ini_set('display_errors', True);

Which now gives me "Notice: Undefined index: city in /home/content......" For both IE and FireFox - but Firefox at least still fills out the table...

I also tried....
Expand|Select|Wrap|Line Numbers
  1. $sql = "SELECT city_name, city_default_zip, seatac_price, boeing_price FROM char_city WHERE city_name = ('$_POST[city]')";
(and a few variations of where to put the quote)
and
Expand|Select|Wrap|Line Numbers
  1. $city = $_POST['city'];
  2. $sql = "SELECT city_name, city_default_zip, seatac_price, boeing_price FROM char_city WHERE city_name = ('$city')";
Plus: I pulled the following out of a book regarding the global variables....
foreach
Expand|Select|Wrap|Line Numbers
  1. ($_POST as $key => $value) {
  2.   $$key = $value;
  3. }
[Please use CODE tags when posting source code. Thanks! --pbmods]

Watch. I'm missing some little punctuation mark or something -

Anybody see my problem?
Jun 23 '07 #1
6 2201
pbmods
5,821 Expert 4TB
Heya, SuperFool. Welcome to TSDN!

What is the problem you are having? If it's working in one browser, but not another, then the problem is not the PHP (though it could be the HTML).
Jun 23 '07 #2
Heya, SuperFool. Welcome to TSDN!

What is the problem you are having? If it's working in one browser, but not another, then the problem is not the PHP (though it could be the HTML).
Thanks.

I'm upset with myself over how much time I'm spending on this problem - however the more I learn about PHP the more excited I get about the possibilities.

My problem is that while all variations I've created on this attempt work fine in FireFox - they come up blank in both IE 6 & 7, which unfortunately runs about 88% of the visitors, so as cool as it is for my friend - it's useless if IE can't see it.

Right now, I'm just trying to make it work - I'm able to make it pretty later. It's just trying to get a grasp on it with nothing more than me and a couple books is pretty difficult. Maybe if I started on chapter 1... but then that wouldn't be me - I'm more foolish than that.

Back to the subject at hand....
When I input the display error code bit I found in the sticky for this forum - I keep getting told "Notice: Undefined variable: name in /home/content...blah, blah"

So I guess it's a matter of trying to print every variable name I can come up with. Well, that's a decent way to learn I guess. I can't imaging that I have to build a giant list of IF THEN statements for every possible city that could be chosen from the pull down menu - that seems to just contradict the purpose.

I'll figure it out - I just welcome a place to ask some questions occasionally.
Thanks for your insight
Jun 23 '07 #3
pbmods
5,821 Expert 4TB
Heya, SuperFool.

Instead of mysql_fetch_object(), try using mysql_fetch_assoc().

mysql_fetch_object() returns an object, not an array.

As for why it's coming up blank in IE, are you accessing the same server on each computer? In other words, are you typing 'localhost' in the address bar, but using a different computer?
Jun 23 '07 #4
Hello pbmods


Instead of mysql_fetch_object(), try using mysql_fetch_assoc().

mysql_fetch_object() returns an object, not an array.
Nope - No change. I used object a while ago because assoc gave me duplicate results of everything.

As for why it's coming up blank in IE, are you accessing the same server on each computer? In other words, are you typing 'localhost' in the address bar, but using a different computer?
I'm doing everything on the server - not using my local machine for anything but Dreamweaver - well that and FTP to upload it to the server.

Accessing the db is set up through include commands - so no danger of mistyping anything from one to the other.

And now, I've managed to play with it enough to break FireFox for this now as well.

I've added a table, where I've tried to list out every variable I can think of, but of course they all come up blank too.

thanks again
Jun 23 '07 #5
pbmods
5,821 Expert 4TB
Heya, SuperFool.

Ok, let's start from the beginning. What happens when you:

Expand|Select|Wrap|Line Numbers
  1. var_dump($city);
  2.  
If $city is empty, you will get no results from your query, unless there are entries where city_name is empty.

Next:
Expand|Select|Wrap|Line Numbers
  1. print($sql);
and try manually running that query in your MySQL client. Do you get any results?

Next:
Expand|Select|Wrap|Line Numbers
  1. var_dump($result);
and make sure that mysql_query() is returning the proper value.

Next:
Expand|Select|Wrap|Line Numbers
  1. or die(mysql_error());
  2. // Add this line:
  3. print('!');
after your die() statement to make sure that your code is executing properly.

At this point, the HTML in your script HAS to be getting output.

Next, make sure your mysql_query returned the proper results.
Expand|Select|Wrap|Line Numbers
  1. while($row = mysql_fetch_assoc($result)) {
  2.     // Add this line:
  3.     print('<pre>' . print_r($row, true) . '</pre>');
  4. .
  5. .
  6. .
  7.     print('<hr />');
  8. }
That's my reaction at this point. Somewhere, something isn't doing what it's supposed to. Welcome to unit testing. Fun stuff, huh?
Jun 24 '07 #6
Previous Post Deleted.............. I'll try this again.....
Jun 24 '07 #7

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

1 post views Thread by JKop | last post: by
13 posts views Thread by Don Vaillancourt | last post: by
49 posts views Thread by matty | last post: by
26 posts views Thread by Frederick Gotham | last post: by
5 posts views Thread by Pseudonyme | last post: by
1 post views Thread by Waqarahmed | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.