0
votes

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)
3
If you added a question to that, maybe we could help you. Maybe even some code.Funk Forty Niner
This site is for programming questions. It is not your personal "todo" list recording system.Marc B
Can you show us some code, otherwise i will begin to type about low quality question and @halfer will come here to warn me.HddnTHA
there are more than two gendersuser557846
Does anyone going to point ancient mysql functions?HddnTHA

3 Answers

1
votes

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);
?>
0
votes

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;
0
votes
<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.