0

I have created a simple register system that works as intended, although I'd like to give the user an option of selecting a value from a drop down menu as they register, e.g. Male or female. The user will then be distinguishable in the database as a male or female, along with their username and password. I am using a simple php dropdown menu to select the data from.

I would like to know how I could implement this into my register system, to allow the users to select a gender from a dropdown menu when they register and have it inserted into the database along with their username and password. Thanks

HTML -

<form action="" method="POST">
    <select name="formGender">

<option value="M">Male</option>
<option value="F">Female</option>

</select>

PHP -

if(isset($_POST["submit"])){



if(!empty($_POST['user']) && !empty($_POST['pass'])) {
$user=$_POST['user'];
$pass=$_POST['pass'];

$con=mysql_connect('localhost','root','') or die(mysql_error());

mysql_select_db('user_registration') or die("cannot select DB");

$query=mysql_query("SELECT * FROM login WHERE username='".$user."'");
$numrows=mysql_num_rows($query);
if($numrows==0)
11
  • 5
    If you added a question to that, maybe we could help you. Maybe even some code. Commented Mar 10, 2015 at 20:51
  • 2
    This site is for programming questions. It is not your personal "todo" list recording system. Commented Mar 10, 2015 at 21:01
  • Can you show us some code, otherwise i will begin to type about low quality question and @halfer will come here to warn me. Commented Mar 10, 2015 at 21:02
  • there are more than two genders Commented Mar 10, 2015 at 21:03
  • Does anyone going to point ancient mysql functions? Commented Mar 10, 2015 at 21:04

3 Answers 3

1

I'll let you handle the if ($_POST[...]) business - this will be more showing you how to use prepared statements. I've copied this from http://php.net/manual/en/mysqli.prepare.php and then made changes.

<?php
$link = mysqli_connect("localhost", "my_user", "my_password", "world");

/* check connection */
if (mysqli_connect_errno()) {
    printf("Connect failed: %s\n", mysqli_connect_error());
    exit();
}

$gender = $_POST['formGender'];
$username = $_POST['username'];
$password = $_POST['password']; // obviously you'll encrypt or something

/* create a prepared statement */
if ($stmt = mysqli_prepare($link, "INSERT INTO table (username, password, gender) VALUES (?, ?, ?)")) {

    /* bind parameters for markers */
    mysqli_stmt_bind_param($stmt, "sss", $username, $password, $gender);

    /* execute query */
    mysqli_stmt_execute($stmt);

    /* close statement */
    mysqli_stmt_close($stmt);
}

/* close connection */
mysqli_close($link);
?>
Sign up to request clarification or add additional context in comments.

3 Comments

Nice answer but you should consider change the $_POST['gender'] to $_POST[' formGender '] because OP's form name for gender is formGender
Whew, @HddnTHA, hope I didn't put 2 spaces after my periods either... :-)
it is hard to write code blocks with a thin weird mobilr qwerty, sorry for it (-:
0

Hopefully I understood your question, but it sounds to me like you have a registration form, and you want one of the questions to use a drop down rather than a text option.

If you have a static list you want them to choose from, such as gender as you gave in your example, you can simply use

<form action="register.php" method="post">
   ...
   <select name="gender"> 
      <option>Male</option>
      <option>Female</option>
   </select>
</form>

Then make your "register.php" or whatever you choose to call it say this:

//Create mysqli object
$link = new mysqli($db_host, $db_user, $db_pass, $db_name);

// Check connection
if($link === false){
   die("ERROR: Could not connect. " . mysqli_connect_error());
}

//Set variables for insert

$gender= $_POST['gender'];

//Insert data (you'll use all your form fields)
$sql = "INSERT INTO yourtablename (name, email, gender, etc)
VALUES ('$name', '$email', '$gender','$etc')";

if(mysqli_query($link, $sql)){

    header('Location: whereyouwantusertoendup.php');
    } else{
    echo "There was an error";
}
exit;

1 Comment

Mysqli functions no sense without prepared statements
0
<form method="POST" action="typewhatyouwant.php">    
    //here can be other form fields.....
    <select name="gender">
        <option value="male">Male</option>
        <option vaue="female">Female</option>
    </select>
    <input type="Submit" value="Register"/>
</form>
//Server Side "typewhatyouwant.php"
$gender = $_POST['gender'];
$con = mysqli_connect("host","username","pass","dbname");
$ins = mysqli_query($con, "INSERT INTO `dbname` VALUES ('$gender')");

This is simple client-server model of saving form data. After user clicks submit button, form method is activated and data is sent via HTTP Post method to "typewhatyouwant.php". In php all post data is saved in $_POST superglobal, which is associative array and it means that you can get value of each form field by typing in there name attribute values correctly in $_POST array as keys. so $gender variable's value is select element's value. With mysqli_connect function you connect to the database and mysqli_query inserts your $gender variable's value into table.

2 Comments

It is a poor answer because contains only code, explain OP what is your code achiving
Please add an explanation of what you did and why you did it that way not only for the OP but for future visitors to SO. How does your answer solve the problem?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.