Hi everyone i dont know how to get the already filled value of radio buttons from sql using html/php here is my code but its giving error

<?php include("../includes/config.php"); ?>
<?php
 if ($_SESSION["isadmin"])
   {

   $con=mysql_connect($dbserver,$dbusername,$dbpassword);
if (!$con) { die('Could not connect: ' . mysql_error()); }

mysql_select_db($dbname, $con);
$accountid=$_GET["id"];
$result = mysql_query("SELECT * FROM accounts WHERE (id='".$accountid."')");


    while($row = mysql_fetch_array($result))
    {
       $id=$row['id'];
       $firstname = $row['firstname'];
       $lastname = $row['lastname'];
       $email=$row['email'];
       $type=$row['type'];
    }
mysql_close($con);
?>
<!DOCTYPE HTML>
<html>
<head>
<title>Edit User</title>
<link rel="StyleSheet" href="../admin/css/style.css" type="text/css" media="screen">
</head>


<body>
<?php include("../admin/includes/header.php"); ?>
<?php include("../admin/includes/nav.php"); ?>
<?php include("../admin/includes/aside.php"); ?>
<div id="maincontent">

    <div id="breadcrumbs">
        <a href="">Home</a> >
         <a href="">Manage Users</a> >
         <a href="">List Users</a> >
         Edit User
    </div>
    <h2>Edit User</h2>

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



</div>

</body>
<?php include("../admin/includes/footer.php"); ?>
</html>
<?php
   }
    else
    {
        header("Location: ".$fullpath."login/unauthorized.php");

    }
?>

thanks in advance

Dani AI

Generated

The undefined index error happens when no radio is submitted (nothing selected), and your current markup also gives both radios the same value so the server cannot tell Student from Teacher. For radios to work, all options in the group must share the same name but have distinct, constant values. Then, conditionally add the checked attribute to preselect what is in the database.

<?php
// Map stored values to labels; store compact codes like S/T if you wish.
$options = ['S' => 'Student', 'T' => 'Teacher'];
$current = $type ?? ''; // value fetched from DB

foreach ($options as $val => $label) {
    $checked = ($val === $current) ? ' checked' : '';
    echo '<label><input type="radio" name="type" value="' . $val . '"' . $checked . '> ' . $label . '</label><br>';
}
?>

On submit, validate that a permitted value was posted before updating. Also escape values when echoing into HTML and switch to prepared statements; the old mysql_* API is deprecated/removed. Example update with mysqli:

$id = (int)$_POST['id'];
$type = $_POST['type'] ?? '';
if (!in_array($type, ['S','T'], true)) { /* handle error */ }

$stmt = $mysqli->prepare('UPDATE accounts SET firstname=?, lastname=?, email=?, type=? WHERE id=?');
$stmt->bind_param('ssssi', $firstname, $lastname, $email, $type, $id);
$stmt->execute();

When prefilling form fields, use htmlspecialchars($value, ENT_QUOTES, 'UTF-8') to prevent XSS. See MDN on radio inputs (https://developer.mozilla.org/en-US/docs/Web/HTML/Element/input/radio), PHP’s htmlspecialchars (https://www.php.net/htmlspecialchars), and mysqli prepared statements (https://www.php.net/mysqli.prepare). The mysql extension deprecation is documented here: https://www.php.net/manual/en/intro.mysql.php.

Recommended Answers

All 9 Replies

What is the error?

error it gives after pressing edit button is this

Notice: Undefined index: type in C:\xampp\htdocs\project\admin\users-edit-action.php on line 7

and this id the script on which it gives the error..i think error is due to wrong placement of values in radio buttons..

<?php include("../includes/config.php");?>
<?php
$id=$_POST["id"];
$firstname=$_POST["firstname"];
$lastname=$_POST["lastname"];
$email=$_POST["email"];
$type=$_POST["type"];

$con=mysql_connect($dbserver,$dbusername,$dbpassword);
if (!$con) { die('Could not connect: ' . mysql_error()); }


 mysql_select_db($dbname, $con);
$query=("UPDATE accounts SET firstname='".$firstname."' , lastname='".$lastname." email='".$email."' type='".$type."' WHERE (id='".$id."')");
$result = mysql_query($query);
echo "User has been updated Successfully!!";
mysql_close($con);
?>

I am wondering if your name="type" is causing a conflict as type is used as part of a form elements details. Have you tried changing it to something else and see what happens?

Also, are you sure that one of the radio buttons is being selected?

Member Avatar for Member #120589
<input type="radio" name="type" value="<?php echo $type; ?>" /> Student<br />
<input type="radio" name="type" value="<?php echo $type; ?>" /> Teacher<br />

Why are you giving both radios the same value? It can't discrominate between them. I also suggest making one a default with checked="checked".

yup i know tht this is the wrong way but i cant understand how would i take the value from data base??? as i want to edit the already filled record then it should be checked already...how can i do that?? i mean how can i get the value of that type whether it is S or T..so that i could edit and change the type when i want to edit it..

Member Avatar for Member #46692

Like diafol said (off topic:when did you change your username?) the radio button names need to be unique.

Ipso facto. <- that's latin BTW.

So, you radio buttons need to have unique names!

Now from your PHP file it is quite tedious to go through each name and check if it isset();

So we create a grouped radio button with the [] syntax in the HTML. That way we can just loop through using the foreach syntax.

@diafol well i want to retrieve the value from database.. how would i do that..the values are uniqe as thay are coming from database, i dont know how to check which value is selected in database to update it further..

Like this:

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

thankyou very much.problem is solved :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.