php 选择从数据库读取的选项菜单并使用它的值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18953688/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Select option menu read from database and use it's values
提问by Avtontom_
<?php
mysql_connect("localhost", "root","") or die(mysql_error());
mysql_select_db("tnews2") or die(mysql_error());
$query = "SELECT name,id FROM categories ORDER BY ID DESC LIMIT 0,6";
$result = mysql_query($query) or die(mysql_error()."[".$query."]");?>
<select name="categories">
<?php while ($row = mysql_fetch_array($result)){
?>
<option value=" <?php $row['path']; ?> ">
<?php echo $row['name']; ?>
</option>
<?php
}
?>
</select>?>
So this is select option menu and it's value I read from database, but when I try to Get the selected value i get only the first one.
所以这是选择选项菜单,它是我从数据库中读取的值,但是当我尝试获取所选值时,我只得到第一个值。
<?php
mysql_connect("localhost", "root","") or die(mysql_error());
mysql_select_db("tnews2") or die(mysql_error());
if(!empty($_POST['title']) && !empty($_POST['date']) && !empty($_POST['txt']) && !empty($_POST['image'])){
$TITLE=$_POST['title'];
$DATE=$_POST['date'];
$TXT=$_POST['txt'];
$IMAGE=$_POST['image'];
$CATEGORIES=$_POST['categories'];
echo $CATEGORIES;
$ANSWER=$_POST['main'];
$MAINPAGE=0;
Could you help me with an idea to get the selected option
你能帮我一个想法来获得选定的选项吗
回答by
EDIT:(reduces ur code)
编辑:(减少你的代码)
<?php
mysql_connect("localhost", "root","") or die(mysql_error());
mysql_select_db("tnews2") or die(mysql_error());
$query = "SELECT name,id,path FROM categories ORDER BY ID DESC LIMIT 0,6";
$result = mysql_query($query) or die(mysql_error()."[".$query."]");
?>
<select name="categories">
<?php
while ($row = mysql_fetch_array($result))
{
echo "<option value='".$row['path']."'>'".$row['name']."'</option>";
}
?>
</select>
回答by The Only Smart Boy
I would prefer doing it like this using mysqli
我更喜欢使用 mysqli 这样做
<?php
/* Database connection settings */
$host = 'localhost';
$user = 'root';
$pass = '';
$db = 'tnews2';
$mysqli = new mysqli($host,$user,$pass,$db) or die($mysqli->error);
/* Your query */
$result = $mysqli->query("SELECT name,id,path FROM categories ORDER BY ID DESC LIMIT 0,6";) or die($mysqli->error);
?>
Then add the elements to html like this:
然后将元素添加到 html 中,如下所示:
<select name="categories">
<option value="Select School">Select Shool</option>
<?php
while ($row = mysqli_fetch_array($result)) {
echo "<option value='" . $row['path'] . "'>'" . $row['name'] . "'</option>";
}
?>
</select>
回答by Algy56
I would recommend making sure that you order the fields in the SQL Query to match the order you want them in and then use:
我建议确保您对 SQL 查询中的字段进行排序以匹配您希望它们的顺序,然后使用:
$query = "SELECT path, name, id FROM categories ORDER BY ID DESC LIMIT 0,6";
echo "<option value='".$row[0]."'>'".$row[1]."'</option>";
Your code will then be much more reusable as a 'code snippet', all you need to do is write a new SQL Query
您的代码将更可重用为“代码片段”,您需要做的就是编写一个新的 SQL 查询

