Jump to content

SIMPLE PHP DB QUERY


phpstudent

Recommended Posts

Hello everyone I am totally new to the php community and I want to learn this stuff! I am currently taking a class and our first project is driving me nuts so here I am.

Here is a working example of what I am trying to accomplish. Dont laugh I know its simple but this is a complete foreign language to me and im trying my best. :(
http://75.32.150.185/~david/ps7_8.dogfood.php

I am trying to make a dropmenu using php as you can see from the working example above and I am not sure if I am even doing it right. ANY insight or help you can provide will be greatly appreciated!

Here is the error which I am getting Parse error: syntax error, unexpected T_STRING on line 45

43      while ($stateArr = mysql_fetch_array($statesRsc)) {
44 $dropMenu = dropmenu
45 <select name="$result">
46 <option value="stateCode">AK</option>;
47 </select>
48 // Use each row as to generate an <option> tag for select box.
49 // Use the stateCode field as your label and the stateID code as your value.
50 }


Thanking you in advance!
Link to comment
Share on other sites

You are switching from html to php and as a result have to create "echo" or "print" statements.  like this...

[code]
while ($stateArr = mysql_fetch_array($statesRsc))
{
    $dropMenu = dropmenu; // add a ";" to close of each line

    //you MUST echo to the screen
    echo "<select name=\"".$result."\">";
    echo "<option value=\"stateCode\">AK</option>";
    echo "</select>";

    // Use each row as to generate an <option> tag for select box.
    // Use the stateCode field as your label and the stateID code as your value.

}
[/code]
Link to comment
Share on other sites

Firstly, you need to tell PHP when you want to enter code, and when you want to enter HTML.  For example:

[code]<select name='state'>
<option value='<? echo $stateArr['stateCode']; ?>'> <? echo $stateArr['stateCode']; ?> </option>
</select>[/code]

The '<?' says "enter php mode".  The "?>" says "Finish with php mode, go back to HTML".

The other style you can use is like this:

[code]print "<select name='$state'>";
print "<option value='{$stateArr['stateCode']}'>{$stateArr['stateCode']}</option>";
print "</select>";[/code]

In this case, you never leave PHP mode.  Instead, you print out the html using commands within php itself.

In both cases, the select tags must go outside the loop, as they only appear once.  The option tags must be inside the loop, as they appear once for each state.

Hope that helps.. feel free to ask more questions :)
Link to comment
Share on other sites

Hello btherl and cunoodle2! Thank you very much for your insight into my first journey into php! First real project = Very excited / frustrated.  :o
I tried to understand both of your suggestions and from that I was very excited to see the drop down menu work finally! You can actually see the states list plus it gives a value!  So kool! ;D Now I have 2 errors so far....  :( (Also btherl thank you for your note on the select tags going outside the loop) So there are many states but I will list a couple just so you can see what I did.

                    while ($stateArr = mysql_fetch_array($statesRsc))
print "<select name=\"".$result."&nbsp;\">";
{
$dropMenu = dropmenu;
print "<option value=\"1\">AK</option>";
print "<option value=\"2\">AL</option>";
}
print "</select>";
            }

The first error that I noticed happend when I viewed the source from the browser after the page loaded. I noticed in the source of the loaded page towards the bottom it said,"<b>Warning</b>:  mysql_fetch_array(): supplied argument is not a valid MySQL result resource on line <b>43</b>

So then I went to line 43 and it reads as, "while ($stateArr = mysql_fetch_array($statesRsc))" like the 1st line of code above.

The second error I came upon was when I actually tried to select a state from the drop menu. I selected AK and as the page loaded it said, "Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource on line 120".

Line 120 reads as, "while ($cityArr = mysql_fetch_array($citiesRsc)) {"

Question1 - What do I do to even begin to fix these errors? What does it mean? I have spent so many hours/days working on this that I dont know what else to do. My idea is once I get this thing working then I can make sense of each and every line of code and hopefully that will give me a clue.
Question2 - Why does one error show when the page is loaded and why does one error show only if you view the source?
Question3 - What do you recommend is the best way to jump in the huge ocean of knowledge on php? Get a tutor? Anyone live in the Bay Area? haha
Question4 - Do you think I will ever get this thing looking like this example? lol http://75.32.150.185/~david/ps7_8.dogfood.php (right now - questionable)

I hope I am posting somewhat intelligent posts with just enough information so that you guys can understand what I am trying to achieve. If not tell me what you need.

Thank you both again for helping this noobie out...
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.