如何将表单下拉列表选中的值存储到mysql数据库中

时间:2014-12-03 03:35:35

标签: php mysql database forms dropdownbox

有一个带有下拉列表的表单。一旦用户提交表单,我怎样才能获得用户选择进入我的数据库的价值?无法获取下面的代码来存储用户选择的值?谢谢你的帮助。

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文件

<?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

1 个答案:

答案 0 :(得分:2)

更改您的表单如下。

<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>