2

Have a form with a dropdown list. How can I get the value the user selects into my data base once a user submits the form? Can't get the code below to store the user selected value? Thanks for your help.

FORM

<form action="xxx.php" class="well" id="xxx" name"xxx" method="post">


<select name="extrafield5">
 <option value="NOW" selected="selceted">Submit order now</option>
 <option value="REVIEW">Submit my order for review</option>
</select>


</form>

PHP FILE

<?php

define('DB_NAME', 'xxx');
define('DB_USER', 'xxx');
define('DB_PASSWORD', 'xxx');
define('DB_HOST', 'xxx');

$connection = mysqli_connect(DB_HOST, DB_USER, DB_PASSWORD);

if(!$connection){
die('Database connection failed: ' . mysqli_connect_error());
}

$db_selected = mysqli_select_db($connection, DB_NAME);

if(!$db_selected){
die('Can\'t use ' .DB_NAME . ' : ' . mysqli_connect_error());
}

echo 'Connected successfully';


if (isset($_POST['extrafield5'])){
    $extrafield5 = $_POST['extrafield5'];
}

else {$extrafield5 = '';}




$sql = "INSERT INTO seguin_orders (extrafield5) 
        VALUES ('$extrafield5')";

if (!mysqli_query($connection, $sql)){
die('Error: ' . mysqli_connect_error($connection));
}

DATABASE

http://oi60.tinypic.com/9ppc0i.jpg

3
  • 1
    You should include more of the code. Is the <select> in a form that is posting to that PHP file? If you var_dump($extrafield5); exit; after the IF statement, what is the output? Do you have an established DB connection to run the INSERT with? I recommend PDO php.net/manual/en/pdo.connections.php Commented Dec 3, 2014 at 3:38
  • 1
    you should get the option value as the value of extrafield5. Are you getting a null value? also check if you have set the valid form method=post or else use if(isset($_REQUEST['extrafield5'])). Post you mysql connection code as well if possible Commented Dec 3, 2014 at 3:38
  • you'll have to connect to it first, and use either mysqli or PDO API, then create an insert statement, also consider using prepared statements Commented Dec 3, 2014 at 3:38

1 Answer 1

2

change your form as below.

<form action="xxx.php" class="well" id="xxx" name"xxx" method="post">


<select name="extrafield5">
<option value="NOW" >Submit order now</option>
<option value="REVIEW">Submit my order for review</option>
</select>


</form>
Sign up to request clarification or add additional context in comments.

Comments

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.