使用mysql的值填充选择输入字段 [英] Populating select input field with value from mysql

查看:245
本文介绍了使用mysql的值填充选择输入字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的表单具有输入字段,其值从mysql表中填充.在我的select语句中,我将这些值传递给字段.该表称为person,并具有唯一的ID person_id和外键academy_id.每个人的状态activeinactive存储在字段名称person_status中.我很难从每个人的person_status中提取值.如何在选择查询中显示每个人的状态? 示例

My form has input fields with values populated from mysql table. In my select statement I am passing these values to the fields. The table is called person and has a unique id person_id and foreign key academy_id. Each person has a status active or inactive stored in field name person_status. I am having difficulties pulling the values from person_status for each person. How would I show the status for each person inside the select query? EXAMPLE

选择要填充的查询

<?php

   $id = 15; 
$db_select2  = $db_con->prepare("
        SELECT     a.name, 
                   a.academy_id,
                   p.person_id,
                   p.person_status,
                   p.first_name
        FROM academy a
        LEFT JOIN person p ON a.academy_id = p.academy_id
        WHERE a.academy_id = :id
        ");
        if (!$db_select2) return false;
        if (!$db_select2->execute(array(':id' => $id))) return false;
            $results2 = $db_select2->fetchAll(\PDO::FETCH_ASSOC);
            if (empty($results2)) return false;
            $result2 = '';
            $s = 1;
            echo "<table>";
            echo "<tbody>";
        foreach ($results2 as $value2){ 
            echo "<tr>";
            echo "<td>Name #".$s."<input type=\"hidden\" name=\"person_id_".$s."\" value='". $person_id = $value2['person_id']."' readonly=\"readonly\"/><input id=\"person_fname_".$s."\" name=\"person_fname_".$s."\" placeholder=\"Person #".$s." First Name\" type=\"text\" value='" . $value2['first_name'] ."'/></td>";
            echo "</tr>";
                $s++;   
        }
        echo "</tbody>";
        echo "</table>";    


?>

想将其集成到select语句中:

would like to integrate this to select statement:

<?php
$table_name2 = "person";
$column_name2 = "person_status";

    echo "<select name=\"$column_name2\"><option>Select one</option>";
    $sql1 = 'SHOW COLUMNS FROM '.$table_name2.' WHERE field="'.$column_name2.'"';
    $row1 = $db_con->query($sql1)->fetch(PDO::FETCH_ASSOC);
    $selected1 = '';
    foreach(explode("','",substr($row1['Type'],6,-2)) as $option) {
    if ($status == $option){
          $selected1 = "selected=selected";
    }else{
          $selected1='';
    }
          echo "<option value='$option'" . $selected1. ">$option</option>";
    }
    echo "</select></br>";   
?>

推荐答案

仅获取一次选项(无需为每个人重复此操作):

Get options only once (no need to repeat this for every person):

$sqlStatuses = 'SHOW COLUMNS FROM '.$table_name2.' WHERE field="'.$column_name2.'"';
$rowStatuses = $db_con->query($sql1)->fetch(PDO::FETCH_ASSOC);
$personStatuses = explode("','",substr($rowStatuses['Type'],6,-2));

然后,走过去的人

foreach ($results2 as $value2) { 
    // Your code
    echo "<tr>";
    echo "<td>Name #".$s."<input type=\"hidden\" name=\"person_id_".$s."\" value='". $person_id = $value2['person_id']."' readonly=\"readonly\"/><input id=\"person_fname_".$s."\" name=\"person_fname_".$s."\" placeholder=\"Person #".$s." First Name\" type=\"text\" value='" . $value2['first_name'] ."'/></td>";

    // Added
    echo '<td><select name="person_status_'.$s.'">';
    foreach($personStatuses as $option) {
        echo '<option value="'.htmlspecialchars($option).'" ';
        if ($value2['person_status'] == $option) {
            echo 'selected="selected"';
        }
        echo '>' . htmlspecialchars($option) . '</option>';
    }
    echo '</select></td>';

    // Your code again
    echo "</tr>";
    $s++;   
}

将其内置到一个SELECT查询中是不必要的复杂操作(虽然可能,但是会给您带来不可读的代码).

Building this into one SELECT-query is unneccessary complex (although possible, but gives you unreadable code).

哦,看看 htmlspecialchars (),如果名称中包含一个"-character HTML"搞砸了

Oh, and take a look at htmlspecialchars(), if a name contains a "-character your HTML get screwed up

这篇关于使用mysql的值填充选择输入字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆