简体   繁体   中英

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

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

<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..

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:

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

And the other input tag would also be there, but without the checked attribute. You just need to do the PHP check on what element to put the attribute.

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 />

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 />

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM