NameSilo

Need help with drop down selection in PHP and MySQL

Spaceship Spaceship
Watch

JsteRmX

Established Member
Impact
2
I am in the process of developing a website, and I want people to be able to register on the site. I am using PHP with MySQL, and what I need help with is creating the register.php page. On this page I want users to enter all their info which will then be added to the database. I am wondering two things:

what data type should I use for the email field in the MySQL database? I think it should be VARCHAR. but i am not %100 sure.

and.... I want users to select their state from a drop down menu. how do i make this? I found this piece of code and i am wondering if it will work, or what else I need to do to connect it to the database.

Code:
<? 
$category = array( 
1=> "Alabama", 
2=> "Alaska", 
3=> "Arizona", 
4=> "Arkansas", 
5=> "California", 
); 
$category = str_replace(" ", " ", $category); 

echo '<SELECT name=category>'; 
foreach ($category as $key => $value) 
{ 
echo '<OPTION value='.$value.'> '.$value.''; 
} 
echo '</select>'; 

?>

i am kind of confused, and would really appreciate help with this. If the above code is ok to use, how do i implement it? Let me tell you, that i am not really learning PHP from the start, I am just more so learning as I go, learning what I need to make the site. Please help, and many thanks in advance to anyone who can help me out. I dont have much to offer you, except for some rep+ hehe.
Thanks!!!
 
0
•••
The views expressed on this page by users and staff are their own, not those of NamePros.
AfternicAfternic
to be safe incase someone has some ridiculously long email use text.
some of those latin names can get long -_-

it used to be 64character@64 characters MAX
now i think its 64characters@255characters MAX

and that code for the drop down is fine except i'm not really sure what
PHP:
$category = str_replace(" ", " ", $category);
is used for.
 
Last edited:
0
•••
thanks. well the source i got this code from says that it is to make sure any spaces will be populated if added to a database. though i am not sure exactly what that means either.
 
0
•••
no idea, jsut leave it then heh.
 
0
•••
hmm, so what data type and length should i assign to the field "state" in my database if it is going to be a drop down selection?
 
0
•••
use can use tiny int since there are only 50 states.
Your drop down is saving the states' numeric value (id)
 
0
•••
Noobie said:
and that code for the drop down is fine except i'm not really sure what
PHP:
$category = str_replace(" ", " ", $category);
is used for.

That code makes absolutely no sense to be honest because all it is doing is replacing a space with a space in the category. No point replacing something with the same thing as you just end up with what you started with.


Remember to validate EVERYTHING that is in the form, for example if someone selects a state ensure the state is in the list, if the username should only contain a-z make sure it only contians thos characters etc (always when possible check the string HAS what you require instead of blocking everything it shouldn't, if you do the latter it is easy to forget something that could be harmfull).
 
0
•••
thanks filth. now i am having some other problems. I am still not sure how to get the drop down menu to work.

I have created a new table in my database named states with two fields: stateid and state. i have stateid set to autoincrement, and i am going to add all the state names to the state field. Now i am trying to add the correct code to my php file so that the drop down menu will work.

Here is a snippet of the code i have, this is for the city field, and then following it should be the code for users to select their state. I need help generating the correct code to insert here ( after "style5> )

PHP:
    <td><p class="style1"><span class="style2"><span class="style3"><span class="style4"><span class="style5"><b>City/Town:</b> </span></span></span></span></p>
        </td>
        <td><p align="left" class="style1"><span class="style2"><span class="style3"><span class="style4"><span class="style5">
            <input type="text" size="20" maxlength="50" name="city" />
</span></span></span></span></p>
        </td>
      </tr>
      <tr>
        <td><p class="style1"><span class="style2"><span class="style3"><span class="style4"><span class="style5> WHAT CODE GOES HERE FOR THE DROP DOWN MENU TO POPULATE FROM THE SQL DB, AND THEN WILL SAVE THE STATE THE USER CHOSE TO THEIR RECORD IN THE user TABLE?

please advise!! also, i am wondering what this means exactly: name="city"
What purpose does this serve? what is it connecting to?
 
0
•••
yes! well i got the drop down to work. now my problem is that not all the submitted information is being inserted into the database, but some of it is. hmmmm. gotta get to work on that.
 
0
•••
your best bet would be to echo out the insert query and try it yourself in phpmyadmin. I personally find it easier to find the problem that way.
 
0
•••
Dynadot โ€” .com Registration $8.99Dynadot โ€” .com Registration $8.99
Appraise.net
Unstoppable Domains
Domain Recover
DomainEasy โ€” Zero Commission
  • The sidebar remains visible by scrolling at a speed relative to the pageโ€™s height.
Back