我很难掌握通过 php 从数据库中回显变量的概念。取而代之的是字符串格式的变量,我得到一个整数。如何通过选择框显示类别字符串变量?
这是我数据库中的信息:
CREATE TABLE `category` (
`c_id` tinyint(4) not null auto_increment,
`category` varchar(30) not null,
PRIMARY KEY (`c_id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=5;
INSERT INTO `category` (`c_id`, `category`) VALUES ('1', 'Animals >');
INSERT INTO `category` (`c_id`, `category`) VALUES ('2', 'Humans >');
INSERT INTO `category` (`c_id`, `category`) VALUES ('3', 'Technology >');
INSERT INTO `category` (`c_id`, `category`) VALUES ('4', 'Oceans >');
INSERT INTO `category` (`c_id`, `category`) VALUES ('5', 'Desert >');
INSERT INTO `category` (`c_id`, `category`) VALUES ('6', 'Jungles >');
这是我的php:
<select name="category" size="6" onChange="autoSubmit();">
<?php
//POPULATE DROP DOWN MENU WITH CATEGORIES
$conn = new PDO($DSN, $USERNAME, $PASSWORD);
$sql = "SELECT * FROM category ORDER BY c_id";
while($row = mysql_fetch_array($categories))
{
echo ("<option value=\"$row[c_id]\" " . ($category == $row["c_id"] ? " selected" : "") . ">$row[category]</option>");
}
?>
<option value="1" <?php if($category == 1) echo " selected"; ?>><? echo $category; ?></option>
<option value="2" <?php if($category == 2) echo " selected"; ?>><? echo $category; ?></option>
<option value="3" <?php if($category == 3) echo " selected"; ?>><? echo $category; ?></option>
<option value="4" <?php if($category == 4) echo " selected"; ?>><? echo $category; ?></option>
<option value="5" <?php if($category == 5) echo " selected"; ?>><? echo $category; ?></option>
<option value="6" <?php if($category == 6) echo " selected"; ?>><? echo $category; ?></option>
</select>