php 从 MySQL 获取值以检查单选按钮会生成通知

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

Getting the value from MySQL to check radio buttons generates a notice

phpmysql

提问by SporeDev

I'm trying to set the "checked" status of a radio button based on the value from the MySQL database. The main purpose of the code is to be a "edit user profile data" script. I managed to get all information from the database and everything gets edited as it should if the user choses to but I can't seem to make the radio buttons work. I get the following message:

我正在尝试根据 MySQL 数据库中的值设置单选按钮的“已检查”状态。代码的主要目的是成为“编辑用户配置文件数据”脚本。我设法从数据库中获取了所有信息,并且如果用户选择的话,一切都会按原样进行编辑,但我似乎无法使单选按钮起作用。我收到以下消息:

Notice: Use of undefined constant Male - assumed 'Male' in C:\xampp\htdocs\Signup\user_panel.php on line 57

注意:使用未定义的常量男性 - 在 C:\xampp\htdocs\Signup\user_panel.php 中第 57 行假定为“男性”

I checked SO for this notice but I couldn't find anything related to radio buttons AND the notice.

我为此通知检查了 SO,但找不到与单选按钮和通知相关的任何内容。

This is the code:

这是代码:

<form action="includes/user_panel.php" method="POST">
                <table>
                    <tr>
                        <td><label for="email"> E-mail </label></td>
                        <td><input type="email" name="email" id="email" value="<?php echo "{$fetch['email']}"; ?>"></td>
                    </tr>
                    <tr>
                        <td><label for="first_name"> First name </label></td>
                        <td><input type="text" name="first_name" id="first_name" value="<?php echo "{$fetch['first_name']}"; ?>"></td>
                    </tr>
                    <tr>
                        <td><label for="last_name"> Last name </label></td>
                        <td><input type="text" name="last_name" id="last_name" value="<?php echo "{$fetch['last_name']}"; ?>"></td>
                    </tr>
                    <tr>
                        <td><label for="birthday"> Birthday </label></td>
                        <td><input type="date" name="birthday" id="birthday" value="<?php echo "{$fetch['birthday']}"; ?>"></td>
                    </tr>
                    <tr>
                        <td><label for="sex"> Sex </label></td>
                        <?php
                        if ($fetch['sex'] = male)
                        {
                            echo "<td><input type='radio' name='sex' value='male' id='sex' checked> Male <input type='radio' name='sex' value='female' id='sex'> Female </td>";
                        }
                        else
                        {
                            echo "<td><input type='radio' name='sex' value='male' id='sex'> Male <input type='radio' name='sex' value='female' id='sex' checked> Female </td>";
                        }
                        ?>
                    </tr>
                </table>
                </br>
                <input type="submit" name="edit_user" value="Edit" class="button_1">
                <input type="hidden" name="edit_user_data">
            </form>

Additional information:

附加信息:

$initial_email = $_SESSION['email'];
$username = $_SESSION['username'];
$query_user_panel="SELECT * FROM users WHERE email = '$initial_email' OR username= '$username'";
$result_user_panel=mysql_query($query_user_panel) or die (mysql_error());
$fetch = mysql_fetch_assoc($result_user_panel)

Line 57 is the one with the "if".

第 57 行是带有“if”的那一行。

Scroll down the code and you'll see where the sex should get checked. I tried with a "if" but that generates me the notice that I wrote.

向下滚动代码,您将看到应该检查性别的位置。我尝试使用“如果”,但这会生成我写的通知。

I know that MySQL is depreciated and I should use MySQLi. If you have the time please check this question as well: Simplest MySQL to MySQLi transition

我知道 MySQL 已折旧,我应该使用 MySQLi。如果您有时间,也请检查这个问题:Simplest MySQL to MySQLi transition

Thank you very much and please let me know if I need to edit my post or provide with additional data.

非常感谢,如果我需要编辑我的帖子或提供其他数据,请告诉我。

回答by Vilsol

Your problem is that you have to forgot to put == instead of = as well as you have to put Male in quotes.

你的问题是你必须忘记把 == 而不是 = 以及你必须把 Male 放在引号中。

It should look like this:

它应该是这样的:

if ($fetch['birthday'] == "Male")

Also, are you sure that birthday field is correct one to check for sex?

另外,您确定生日字段是检查性别的正确字段吗?

回答by Shemeer M Ali

I found there are three mistakes

我发现有三个错误

1) $fetch['birthday'], I think it should be another field name like sex

1)$fetch['birthday'],我觉得应该是另外一个字段名,比如sex

2) if ($fetch['birthday'] = Male), =should be ==so, if ($fetch['birthday'] == "Male")

2) if ($fetch['birthday'] = Male),=应该是==这样,if ($fetch['birthday'] == "Male")

3) if ($fetch['birthday'] == "Male"), you missed double quotes for string so should be "Male"

3) if ($fetch['birthday'] == "Male"), 你错过了字符串的双引号所以应该是 "Male"

<?php

    if ($fetch['birthday'] == "Male")
    {
        echo "<td><input type='radio' name='sex' value='male' id='sex' checked> Male <input type='radio' name='sex' value='female' id='sex'> Female </td>";
     }
     else
     {
        echo "<td><input type='radio' name='sex' value='male' id='sex'> Male <input type='radio' name='sex' value='female' id='sex' checked> Female </td>";
     }
 ?>

回答by Allan Starr

I also wanted a solution where when data is pulled from the database to edit a form, if the account is "Active" it will check the "Active" radio button and if the account is "Inactive" it will check the Inactiveradio button on the html form. I have used tinyint in the mysql database using: 1 - Activeand 0 - InactiveThe code i have used that does this successfully is below:

我也想一个解决方案,当数据从数据库中抽取到编辑形式,如果该帐户是“激活”它会检查“活动”单选按钮,如果账户是“无效”,它会检查无效单选按钮html表单。我在 mysql 数据库中使用了 tinyint:1 - Active和 0 - Inactive我使用的成功执行此操作的代码如下:

<label for="Active">Active</label>
<input id="Active" type="radio" name="Active" value="1"<?php if ($row['Active'] == 1): ?> checked = "checked"<?php endif; ?> />

<label for="Active">Inactive</label>
<input id="Active" type="radio" name="Active" value="0"<?php if ($row['Active'] != 1): ?> checked = "checked"<?php endif; ?> />

So when i query a record and it has a value of '1' in the database field then the Activeradio button will be filled and the Inactivebutton empty and when i query a record and it has a value of '0' in the database field then the Inactiveradio button will be filled and the Activebutton empty.

因此,当我查询一条记录并且它在数据库字段中的值为“ 1”时,活动单选按钮将被填充,而“非活动”按钮为空;当我查询一条记录时,它在数据库中的值为“ 0”字段,然后将填充Inactive单选按钮,而Active按钮为空。

Hope this helps

希望这可以帮助

回答by Ahmed Yousry Ebrahim

try this if ($fetch['sex'] == 'Male')

试试这个 if ($fetch['sex'] == 'Male')