我想使用php更新数据库(mysql)中单选按钮的值


I want to update a value of radio button in database (mysql) using php

我是php和mysql的新手。我已经使用单选按钮来保存我的帐户记录类型,我可以成功插入数据,但当我试图从sql中获取数据(无论选择类型S还是选择类型T)以进一步更新它时,我无法做到这一点。我不知道如何从我的sql中获取单选按钮的数据。。请帮助

<form method="post" action="users-edit-action.php">
    <input type="hidden" value="<?php echo $accountid; ?>" name="id" />
    <label>Email/Username:</label><input type="text" name="email" value="<?php echo $email; ?>" /><br /><br />
    <label>Password:</label><input type="password" name="password" value="<?php echo $password;?>" /><br /><br />
    <label>First Name:</label><input type="text" name="firstname" value="<?php echo $firstname; ?>" /><br /><br />
    <label>Last Name:</label><input type="text" name="lastname" value="<?php echo $lastname; ?>" /><br /><br />
    <label>Type:</label><br />
    <input type="radio" name="type" value="<?php echo $type; ?>" /> Student<br />
    <input type="radio" name="type" value="<?php echo $type; ?>" /> Teacher<br />
        <input type="submit" value="Edit" />
    </form>

这就是我从数据库中获取值的方式。。我肯定我做错了,请帮我解决这个问题。。

您必须检查从数据库中获得的类型,并根据该类型将此属性添加到输入元素:checked="checked"。因此,整个选定的标签将如下所示:

<input name="type" type="radio" checked="checked" value="the one PHP returned" />

另一个输入标签也会在那里,但没有checked属性。您只需要对放置属性的元素进行PHP检查。

整个部分:

<input type="radio" name="type" value="S" <?php if ($type == 'S') echo 'checked="checked"'; ?>" /> Student<br />
<input type="radio" name="type" value="T" <?php if ($type == 'T') echo 'checked="checked"'; ?>" /> Teacher<br />

单选按钮的值是固定的,不取决于数据库中的值,只取决于是否选中它。

应该是这样的:

<input type="radio" name="type" value="S" <?php echo ($type == 'S') ? 'checked' : ''; ?> /> Student<br />
<input type="radio" name="type" value="T" <?php echo ($type == 'T') ? 'checked' : ''; ?> /> Teacher<br />