0
<?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>?>

所以这是选择选项菜单,它是我从数据库中读取的值,但是当我尝试获取所选值时,我只得到第一个值。

<?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;

你能帮我一个想法来获得选定的选项吗

4

3 回答 3

9

编辑:(减少你的代码)

<?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>
于 2013-09-23T07:40:36.833 回答
0

我更喜欢使用 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);
?>

然后像这样将元素添加到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>
于 2017-04-07T17:09:25.117 回答
-1

我建议确保您对 SQL 查询中的字段进行排序以匹配您希望它们的顺序,然后使用:

$query = "SELECT path, name, id FROM categories ORDER BY ID DESC LIMIT  0,6";

echo "<option value='".$row[0]."'>'".$row[1]."'</option>";

然后,您的代码将作为“代码片段”更加可重用,您需要做的就是编写一个新的 SQL 查询

于 2014-10-21T21:23:35.767 回答