如何在提交后在数据库中存储select标记中的值

时间:2014-08-25 12:33:15

标签: php html phpmyadmin

我在html / php中有一个select tag命令。在用户选择一个选项并提交表单后,我希望它的值存储在我的sql数据库中。我有一个单独的php文件用于select标签和存储数据库。

这是我表单中的select tag命令:

<form name="stdntdetails" action="submit_stdntdetails.php" method="post">
    <select>
        <option value="IT">Information Technology</option>
        <option value="IS">Information System</option>
        <option value="CS">Computer Science</option>
    </select>
</form>

我不知道在submit_stdntdetails.php中放入什么

3 个答案:

答案 0 :(得分:1)

<form name="stdntdetails" action="submit_stdntdetails.php" method="post">
    <select name="department">
        <option value="IT">Information Technology</option>
        <option value="IS">Information System</option>
        <option value="CS">Computer Science</option>
    </select>
    <input type="submit" />
</form>

在submit_stdntdetails.php中

<?php
include_once('connectdb.php');
if(isset($_POST['department'])){
$department=$_POST['department'];
$sql=mysql_query("insert into tbl_name(department) values('$department')");
}
?>

答案 1 :(得分:0)

要将form元素值提交到操作页面,您必须使用name元素的form属性。

name属性添加到下拉列表中,

<form name="stdntdetails" action="submit_stdntdetails.php" method="post">
    <select name="selectname">
        <option value="IT">Information Technology</option>
        <option value="IS">Information System</option>
        <option value="CS">Computer Science</option>
    </select>
   <input type="submit" value="submit" />
</form>

您可以在操作页面 submit_stdntdetails.php 中检索选定的值

echo $_POST['selectname'];

答案 2 :(得分:-2)

在html中将其更改为

<form name="stdntdetails" action="submit_stdntdetails.php" method="post">
    <select name="select">
        <option value="IT">Information Technology</option>
        <option value="IS">Information System</option>
        <option value="CS">Computer Science</option>
    </select>
<input type="submit" value="submit" /> <!-- you need a submit button to submit the form-->
</form>

然后在php页面

<?php
$con = mysql_connect("localhost","mysql_user","mysql_pwd");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }

// some code

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

$query = "INSERT INTO table_name (col1) VALUES ('$select')";
mysql_query($query);
}

mysql_close($con);
?>