我有一个类别列表作为PHP函数。我在表单中调用此函数来显示选项。但是,我无法获取表单将所选选项发布到我的数据库。我搜索了SO和在线,但大多数问题都与值是一个选择选项有关,这会更容易。不过我的名单很长,可能会改变。除了我正在处理的图片之外,表单中的其他值正确地发送到数据库,但是我找不到从表单中的下拉列表中插入所选值的方法。
这是我从表单向数据库发送信息的简单sql查询:
if (isset($_POST['submit']))
{
$title = $_POST['title'];
$subtitle = $_POST['subtitle'];
$description = $_POST['description'];
$category= $_POST['category'];
$pic1 = $_POST['pic1'];
$pic2 = $_POST['pic2'];
$pic3 = $_POST['pic3'];
$pic4 = $_POST['pic4'];
$pic5 = $_POST['pic5'];
$item_location = $_POST['location'];
$items_wanted = $_POST['wanted'];
mysql_query("INSERT INTO items_available (title, subtitle, description, category, image_path1, image_path2, image_path3, image_path4, image_path5, item_location, items_wanted) VALUES ('$title', '$subtitle', '$description', '$category', '$pic1', '$pic2', '$pic3', '$pic4', '$pic5', '$item_location', '$items_wanted')")or die(mysql_error());
header('Location: prompt.php?x=8');
}
这是从我的表单中选择类别字段的代码,这是一个下拉列表:
<div class="field">
<label for="category">Category:</label>
<select id=\"category\" name=\"category\" class=\"searchBox\">
<?php createCategoryList(); ?>
</select>
</div>
这是类别列表功能的代码:
//Creates Category <option>'s for search bar
function createCategoryList(){
if( ctype_digit($_GET['category']) ){ $x = $_GET['category']; }else{ $x = 999; }
echo "<option>All Categories</option>";
$i=0;
while(1){
if(numberToCategory($i)=="Category Does Not Exist"){
break;
}else{
echo " <option value=\"$i\" ";
if($i==$x){echo ' SELECTED ';}
echo " > ";
echo numberToCategory($i);
echo "</option>";
}
$i++;
}
}
//Category Number to String
function numberToCategory($n){
switch($n){
case 0:
$cat = "Antiques";
break;
case 1:
$cat = "Art";
break;
case 2:
$cat = "Automotive";
break;
case 3:
$cat = "Baby";
break;
case 4:
$cat = "Books";
break;
case 5:
$cat = "Business & Industrial";
break;
case 6:
$cat = "Cameras & Photo";
break;
case 7:
$cat = "Clothing & Accessories";
break;
case 8:
$cat = "Collectibles";
break;
case 9:
$cat = "Computers";
break;
case 10:
$cat = "Crafts";
break;
case 11:
$cat = "DVD's & Movies";
break;
case 12:
$cat = "Electronics";
break;
case 13:
$cat = "Health & Beauty";
break;
case 14:
$cat = "Home & Garden";
break;
case 15:
$cat = "Jewelry & Watches";
break;
case 16:
$cat = "Music";
break;
case 17:
$cat = "Pet Supplies";
break;
case 18:
$cat = "Services";
break;
case 19:
$cat = "Sports & Outdoors";
break;
case 20:
$cat = "Sports Memorabilia & Cards";
break;
case 21:
$cat = "Tools & Home Improvement";
break;
case 22:
$cat = "Toys & Hobbies";
break;
case 23:
$cat = "Video Games";
break;
case 24:
$cat = "Other";
break;
default:
$cat = "Category Does Not Exist";
}
目前已传递所选选项的数值,即如果我从下拉列表中选择婴儿选项,则数字3将输入到我的数据库中。如何将此数值替换为下拉列表中显示的相应值?
感谢所有和任何答案。
答案 0 :(得分:0)
只需使用echo " <option value=\"numberToCategory($i)\" ";
代替echo " <option value=\"$i\" ";
其他建议:
PHP的mysql扩展名已弃用,请使用mysqli
或pdo
The mysql extension is deprecated and will be removed in the future: use mysqli or PDO instead
还可以创建一个像$options = array('Antiques','Art'....)
这样的选项值数组,然后循环显示它
还会清理您的输入,您只是将文本保存并在db中保存。它很容易导致sql injection
答案 1 :(得分:0)
提交的值是选项标记的value属性设置为的值。因此,如果您将其更改为以下内容,则会提交类别名称而不是数字:
$catName = numberToCategory($i);
echo "<option value='$catName'>$catName</option>";
然而,马丁在评论中提出的建议会更好。包含类别的表应该有一个ID,这是您应该放入数据库并将其作为外键的字段。
一般来说,在数据库中使用特殊格式的字符串是不好的做法,而是创建一个包含字符串的表,并在其他表中引用该行id。