mysql可选输入是什么意思_使用mysql的值填充选择输入字段

bd96500e110b49cbb3cd949968f18be7.png

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

Select query to populate

$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 "

echo "

";

foreach ($results2 as $value2){

echo "

";

echo "

Name #".$s."";

echo "

";

$s++;

}

echo "

";

echo "

";

?>

would like to integrate this to select statement:

$table_name2 = "person";

$column_name2 = "person_status";

echo "Select one";

$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";

}

echo "";

?>

解决方案

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

Then, walk over the persons

foreach ($results2 as $value2) {

// Your code

echo "

";

echo "

Name #".$s."";

// Added

echo '

';

foreach($personStatuses as $option) {

echo '

if ($value2['person_status'] == $option) {

echo 'selected="selected"';

}

echo '>' . htmlspecialchars($option) . '

';

}

echo '

';

// Your code again

echo "

";

$s++;

}

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

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值