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)
Funk Forty Niner
  • 74,450
  • 15
  • 68
  • 141
Beef Bandito
  • 1
  • 1
  • 3
  • 5
    If you added a question to that, maybe we could help you. Maybe even some code. – Funk Forty Niner Mar 10 '15 at 20:51
  • 2
    This site is for programming questions. It is not your personal "todo" list recording system. – Marc B Mar 10 '15 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. – HddnTHA Mar 10 '15 at 21:02
  • there are more than two genders –  Mar 10 '15 at 21:03
  • Does anyone going to point ancient `mysql` functions? – HddnTHA Mar 10 '15 at 21:04
  • possible duplicate of [Inserting Data from dropdown into database with PHP](http://stackoverflow.com/questions/8000648/inserting-data-from-dropdown-into-database-with-php) – ReneS Mar 10 '15 at 21:05
  • The difficulty with that answer, @ReneS, is that in the accepted answer it continues to use mysql* functions instead of PDO or mysqli* with prepared statements. I wouldn't point people there. – Peter Bowers Mar 10 '15 at 21:08
  • Does your table has a `gender` column? Did you try `$_POST['formGender']` insert into your `sql` query? – HddnTHA Mar 10 '15 at 21:08
  • @HddnTHA Yes I have a three columns in my database (username, password and gender), and how would I go about doing that? Sorry I am very new to php and mysql – Beef Bandito Mar 10 '15 at 21:11
  • *"Inputting data into database"* - There's no code to support an INSERT, so I will pass on posting an answer for this one. – Funk Forty Niner Mar 10 '15 at 21:11
  • @BeefBandito show us your other html form which contains username and password. I am not sure but you should insert those 3 data via 1 form. – HddnTHA Mar 10 '15 at 21:15

3 Answers3

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);
?>
Peter Bowers
  • 3,063
  • 1
  • 10
  • 18
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.

David Demetradze
  • 1,361
  • 2
  • 12
  • 18
  • 1
    It is a poor answer because contains only code, explain OP what is your code achiving – HddnTHA Mar 10 '15 at 21:09
  • 1
    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? – Jay Blanchard Mar 10 '15 at 21:10
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;