从 php 中检索单选按钮的 mysql 值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/33580946/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 23:33:09  来源:igfitidea点击:

Retrieve mysql value from php for radio buttons

phpmysqlradio-button

提问by Liquid Core

As trivial as it can seems, I'm having problems retrieving values for radio buttons from MySql database through PHP. It's my first learning project so I'm trying my best

尽管看起来微不足道,但我在通过 PHP 从 MySql 数据库中检索单选按钮的值时遇到了问题。这是我的第一个学习项目,所以我正在努力

Question has already been asked but I found no useful answer.

问题已经被问到,但我没有找到有用的答案。

The php code does a simple "Select *" so I retrieve all the fields.

php 代码做了一个简单的“选择 *”,所以我检索了所有的字段。

This is the php code

这是php代码

<label>Owner: <?php echo $row['Owner']; ?></label></br>
<input type="radio" name="Owner" checked=<?php if($row['Owner'] = "A") { echo "true"; }?>  value="A">A
<input type="radio" name="Owner" checked=<?php if($row['Owner'] = "B") { echo "true"; }?> value="B">B</br></br>

and I retrieve the values with mysqli_fetch_array().

我用 mysqli_fetch_array() 检索值。

This is the result:

这是结果:

enter image description here

在此处输入图片说明

As you can see the label retrieves the correct value, the radio buttons not.

如您所见,标签检索到了正确的值,而单选按钮则没有。

I've already tried putting == instead of = and putting ' instead of " but I don't know why the checkbox "B" is checked, since Owner value is A.

我已经试过用 == 代替 = 并用 ' 代替 " 但我不知道为什么选中了复选框“B”,因为所有者值是 A。

Also, if there are any best practices which are better than this, you're welcome.

此外,如果有任何比这更好的最佳实践,欢迎您。

回答by trincot

The HTMLattribute checkedshould not get a value, its mere presence indicates that the radio button is checked. So do this:

HTML属性checked不应获得值,它的存在表明单选按钮已被选中。所以这样做:

<label>Owner: <?php echo $row['Owner']; ?></label></br>
<input type="radio" name="Owner" <?php if($row['Owner']=="A") {echo "checked"}?> value="A">A
<input type="radio" name="Owner" <?php if($row['Owner']=="B") {echo "checked"}?> value="B">B

Or using the more compact short echo tag <?= .. ?>and ternary operator:

或者使用更紧凑的短回声标签<?= .. ?>三元运算符

<label>Owner: <?=$row['Owner']?></label></br>
<input type="radio" name="Owner" <?=$row['Owner']=="A" ? "checked" : ""?> value="A">A
<input type="radio" name="Owner" <?=$row['Owner']=="B" ? "checked" : ""?> value="B">B

Note that you need double equals signs for comparisons.

请注意,您需要使用双等号进行比较。

回答by jay

try this code not =but use ==

不试试这个代码,=而是使用==

<input type="radio" name="Owner" <?php if($row['Owner'] == "A") { echo "checked"; }?> value="A">

回答by Muhammad

$gender=$row['gender'];


<input type="radio" name="gender" <?php if($gender=="Male"){?> checked="true" <?php } ?> />Male

<input type="radio" name="gender" <?php if($gender=="Female"){?> checked="true" <?php } ?>/>Female

回答by Mostafa Aabed

$owner=$row['owner'];
$owners= ['A'=>'', 'B'=> ''];
$owners[$owner] = 'checked';

<input type="radio" name="Owner" <?php echo $owners['A']?>  value="A">A
<input type="radio" name="Owner" <?php echo $owners['B']?>  value="B">B

i think it's easy and simple and so useful if you have many values in DB.

如果您在 DB 中有很多值,我认为这很简单,而且非常有用。

回答by shabir ullah

i tried the answer given by @trincot but it give me errors so i have make little changes to improve the answer

我尝试了@trincot 给出的答案,但它给了我错误,所以我几乎没有做任何改动来改进答案

<input type="radio" name="Owner" <?php if($row['Owner']=="A") {?> <?php echo "checked";?> <?php }?> value="A">A

<input type="radio" name="Owner" <?php if($row['Owner']=="B") {?> <?php echo "checked";?> <?php }?> value="B">B