如何从下拉列表中检索值并将其插入数据库字段?

时间:2013-02-17 16:52:01

标签: php html sql selection option

首先,表格有效。

目前我有一个上传表单,上面有大约10个字段。表单定义地插入任何文本输入,但是当我从下面的下拉列表中选择一个选项时,它只是在数据库中插入一个空白字段。

我真的不明白为什么这样做但是有人的贡献会很棒,因为我已经有几天这个问题。

我的问题是:如何在下拉列表中选择选项以插入数据库字段?

请参阅以下文件:

upload.html     

<head>
<meta content="text/html; charset=utf-8" http-equiv="Content-Type" />
<title>test file upload</title>
<link rel="stylesheet" type="text/css" href="css/master.css"/>
</head>

<body>

<div id="wrapper">

<div class="uploadForm" align="center">

<form method="post" action="insert.php">

Heading:<br/><input name="heading" class="contentUpload" type="text" /><br/>

Text:<br/><input name="text" type="text"/><br/>

Frendly URL :<br/><input name="frendlyUrl" class="contentUpload" type="text" /><br/>

Location:<br/><select type="text" name="location" id="location">
                    <option value="Aberdeen">Aberdeen</option>
                    <option value="Aberdeenshire">Aberdeenshire</option>
                    <option value="Angus">Angus</option>
                    <option value="Argyll And Bute">Argyll &Aacute; Bute</option>
                    <option value="Clackmannanshire">Clackmannanshire</option>
                    <option value="Dumfries And Galloway">Dumfries &Aacute; Galloway</option>
                    <option value="Dundee">Dundee</option>
                    <option value="East Ayrshire">East Ayrshire</option>
                    <option value="East Dunbartonshire">East Dunbartonshire</option>
                    <option value="East Lothian">East Lothian</option>
                    <option value="East Renfrewshire">East Renfrewshire</option>
                    <option value="Edinburgh">Edinburgh</option>
                    <option value="Eilean Siar">Eilean Siar</option>
                    <option value="Falkirk">Falkirk</option>
                    <option value="Fife">Fife</option>
                    <option value="Glasgow">Glasgow</option>
                    <option value="Highlands">Highlands</option>
                    <option value="Inverclyde">Inverclyde</option>
                    <option value="Midlothian">Midlothian</option>
                    <option value="Moray">Moray</option>
                    <option value="North Ayrshire">North Ayrshire</option>
                    <option value="North Lanarkshire">North Lanarkshire</option>
                    <option value="Orkney">Orkney</option>
                    <option value="Perth And Kinross">Perth &Aacute; Kinross</option>
                    <option value="Renfrewshire">Renfrewshire</option>
                    <option value="Scottish Borders">Scottish Borders</option>
                    <option value="Shetland Islands">Shetland Islands</option>
                    <option value="South Ayrshire">South Ayrshire</option>
                    <option value="South Lanarkshire">South Lanarkshire</option>
                    <option value="Stirling">Stirling</option>
                    <option value="West Dunbartonshire">West Dunbartonshire</option>
                    <option value="West Lothian">West Lothian</option>
            </select>
<br/>

Age:<br/><input name="age" class="contentUpload" type="text" /><br/>

URL:<br/><input name="url" class="contentUpload" type="text" /><br/>

Image Name (example: abc.png):<br/><input name="imageName" class="contentUpload" type="text" /><br/>

Category:<br/><select type="text" name="category" id="category">
                    <option value="Financial">Financial</option>
                    <option value="Business And Administration">Business &Aacute; Administration</option>
                    <option value="Care">Care</option>
                    <option value="Construction">Construction</option>
                    <option value="Creative">Creative</option>
                    <option value="Energy">Energy</option>
                    <option value="Engineering">Engineering</option>
                    <option value="Food And Drink">Food &Aacute; Drink</option>
                    <option value="Hair And Beauty">Hair &Aacute; Beauty</option>
                    <option value="Hospitality">Hospitality</option>
                    <option value="IT">IT</option>
                    <option value="Landbased">Landbased</option>
                    <option value="Motor And Transport">Motor &Aacute; Transport</option>
                    <option value="Retail">Retail</option>
                    <option value="Scientific">Scientific</option>
                    <option value="Sport And Leisure">Sport &Aacute; Leisure</option>
                    <option value="Enterprise">Enterprise</option>
            </select>
<br/>

Type:<br/><input name="type" class="contentUpload" type="text" /><br/>

<input name="Submit" type="submit" value="submit" />

</form>

</div>

</div>

</body>

</html>

insert.php

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

mysql_select_db("x", $con);

$sql="INSERT INTO podContent (url, heading, text, age, location, imageName, category, type, friendlyUrl)
VALUES
('$_POST[url]','$_POST[heading]','$_POST[text]','$_POST[age]','$_POST[location]','$_POST[imageName]','$_POST[category]','$_POST[type]','$_POST[friendlyUrl]')";

if (!mysql_query($sql,$con))
  {
  die('Error: ' . mysql_error());
  }
echo "1 record added";

mysql_close($con);
?>

非常感谢提前。

1 个答案:

答案 0 :(得分:0)

尝试从select标记中删除type =“text”。

它不是有效的属性。

http://www.w3schools.com/tags/tag_select.asp

还要检查以确保您的DB字段能够处理值的长度。