I have a form that I want to edit the values from the database. I am able to update all my values except for the radio boxes populated from my database. After my UPDATE command, the value being grabbed is the initial checked value from the database.

<?php $db_maritalstatus = $row_rsAdvancedPlan['maritalstatus']; ?>
<input name="maritalstatus" id="maritalstatus" value="single" <?php echo ($db_maritalstatus == "single") ? ' checked="checked"' : '' ?> type="radio" /> <label for="single" class="smaller">Single</label>

Then I use this code to grab new value, but it grabs the checked value initially loaded from database

$maritalstatus = mysql_real_escape_string($_POST['maritalstatus']);

Dani AI

Generated

The immediate cause was the missing semicolon that pointed out — that will stop the ternary from outputting the checked attribute. Beyond that, similar symptoms often come from rendering logic or DB/POST timing. A short checklist and a few safer patterns follow.

Quick checks: make sure every radio has the same name and a distinct value, the label for matches the input id, the form uses method="post", and no client script is forcing the initial state. To confirm what the server actually receives, dump the POST data on submit:

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
  var_dump($_POST);
  exit;
}

When re-displaying the form after an update, decide whether to prefer the freshly posted value or the value reloaded from the database. A common, robust approach is to (a) validate the posted value against an allowlist, (b) write it with a prepared statement, then (c) redirect (POST-Redirect-GET) so the page reloads with the DB value. If you must re-render without redirecting (for validation errors), prefer $_POST to keep the user selection visible. Example pattern for choosing and rendering the radios:

$selected = $_POST['marital'] ?? $rowFromDb['marital'];
$allowed = ['single','married','divorced'];

foreach ($allowed as $opt) {
  echo '<input type="radio" name="marital" id="marital_'. $opt . '" value="'.htmlspecialchars($opt).'"'
       . ($selected === $opt ? ' checked' : '') . '> <label for="marital_'. $opt .'">'.ucfirst($opt).'</label>';
}

Avoid the old mysql_* functions; use PDO or mysqli with prepared statements for updates and then redirect. Example update + redirect:

$stmt = $pdo->prepare('UPDATE people SET marital = :m WHERE id = :id');
$stmt->execute([':m' => $selected, ':id' => $id]);
header('Location: edit.php?id='.$id);
exit;

See the PHP manual on PDO prepared statements and header() redirects. Also use htmlspecialchars() when echoing values into HTML to avoid XSS.

Recommended Answers

All 3 Replies

Simple thing, but it looks like you didn't terminate your ternary operator with a semicolon.

Voila!! Thank you so much! My code now works!

No problem. Those darned semicolons (or lack thereof) can be quite a bugger sometimes.

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.