打印所选选项中的2个值,使用mysql来更新列表

li9yvcax  于 2021-06-17  发布在  Mysql
关注(0)|答案(1)|浏览(236)

我有一个mysql连接在处理一个查询。

<?php
$enlace = mysqli_connect ('localhost:3306',  'user', 'pass');

$consulta = "SELECT id, nameFROM dbapps.dbapp";
$result=mysqli_query($enlace, $consulta);
?>

我选择了两个值。选项值=id描述=id+名称

<select name="sqllist[]" multiple> 
<?php 
while($row = mysqli_fetch_row($result))
{
    echo "<option value=" .$row['0'].">".$row['0']."  ".$row['1']."</option>";
}

?>
</select>

我正在获取所选的值,但我想获取一个变量的所选id以及与另一个变量的id相关的名称

if(isset($_POST['submit'])){
    foreach ($_POST['sqllist'] as $select)
    {
        echo $select."<br>"; 
    }
}

iyfjxgzm

iyfjxgzm1#

// Read all the results from the query
$rows = mysqli_fetch_all ( $result, MYSQLI_ASSOC);

// Id/name map
$names = array_column($rows, 'name', 'id');

// If the data was submitted, use it, otherwise, consider the list empty
$sqllist = isset($_POST['submit']) ? $_POST['sqllist'] : [];

// Create the select statement
<select name="sqllist[]" multiple> 
<?php 
foreach ($rows as $row)
{
    // If the id for this row is in the sqllist, set the selected attribute
    $selected = in_array($row['id'],$sqllist) ? 'selected' : '';
    echo "<option $selected value='" .$row['id']."'>".$row['id']."  ".$row['name']."</option>";
}
?>
</select>
<?php
foreach ($sqllist as $id) {
    echo $id.' '.$names[$id].'<br>';
}

相关问题