我想使用 php 更新数据库(mysql)中单选按钮的值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12151718/
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
I want to update a value of radio button in database (mysql) using php
提问by trouble creator
I am new to php and mysql. i've used radio buttons to save the type of my account records i can insert the data successfully but when i try to get that data (whether type S is selected or type T is selected) from sql to update it further i cant do that. i dont know how to get the data of radio buttons from my sql.. please help
我是 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>
this is how i get the value from database.. im sure iam doing wrong, please help me solve this..
这就是我从数据库中获取值的方式..我确定我做错了,请帮我解决这个..
回答by Shomz
You have to check which type you got from the database and according to that add this attribute to the input element: checked="checked". So the whole selected tag would look like this:
你必须检查你从数据库中获得,并根据所添加此属性的输入元素,其类型:checked="checked"。所以整个选定的标签看起来像这样:
<input name="type" type="radio" checked="checked" value="the one PHP returned" />
<input name="type" type="radio" checked="checked" value="the one PHP returned" />
And the other input tag would also be there, but without the checkedattribute.
You just need to do the PHP check on what element to put the attribute.
另一个输入标签也会在那里,但没有checked属性。您只需要对放置属性的元素进行 PHP 检查。
The whole part:
整个部分:
<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 />
回答by jeroen
The value of your radio-buttons is fixed and does not depend on the value in the database, only whether it is checked or not.
你的单选按钮的值是固定的,不依赖于数据库中的值,只取决于它是否被选中。
It should be something like:
它应该是这样的:
<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 />

