Checked=Checked if value exists in database

I suppose technically I have already managed to get the checkboxes checked=checked if the data exists, but I did it using this code

<input type="checkbox" name="smb" id="smb" value="<?php echo $row["comp"]; ?>" <?php echo ($row['comp'] == "SMB") ? "CHECKED='CHECKED'" : ''; ?> />

This does correctly check & select radio buttons, checkboxes, drop down menus and the like with the relative value specified with == “”, though the problem with this method is that when the form is submitted, the database is not updated with the newly checked/selected values. Instead, the previous data will be used as the current selection… or for the SMB/HDD/USB (see the code) checkboxes, replaced by the value of their hidden input counterparts.

You can see a live example here
oplinfo.x11s.org/opllist/update.php?game_id=105
Notice that the “compatible” radio button is already selected, but if you try to update that value by selecting “issues”, for example, it will just revert back to the original selection.

Here is the page’s code for reference.
[php]

<?php //CONNECT TO THE MYSQL DATABASE $link = mysqli_connect('host','user','pass','database'); if (mysqli_connect_errno()) { printf("Connect failed: %s\n", mysqli_connect_error()); exit(); } //CREATE THE VARIABLES TO IDENTIFY THE DIFFERENT FORM INPUTS $tbl_name="opl_comp"; //VARIABLE TO ASSIGN PRIMARY KEY VALUE SO THE FORM KNOWS WHICH ROW IS BEING EDITED $game_id = isset($_GET['game_id']) ? (int)$_GET['game_id'] : 0; //IMPLODE THE CHECKBOX SELECTIONS EG 1, 2, 3 if(isset($_POST['mode'])) { $mode = implode(",", $_POST['mode']); } else { $mode = "None"; } //VARIABLES TO ASSIGN THE FORMS INPUT VALUES TO WHICH COLUMN IT WILL BE INSERTED INTO $region=$_POST['region']; $vmc=$_POST['vmc']; $smb=$_POST['smb']; $hdd=$_POST['hdd']; $usb=$_POST['usb']; $notes=$_POST['notes']; $comp=$_POST['comp']; $oplver=$_POST['oplver']; $gamename=$_POST['gamename']; ?> <?php //ESCAPE THE VARIABLE INPUTS $gamename = mysqli_real_escape_string($link, $_POST['gamename']); $region = mysqli_real_escape_string($link, $_POST['region']); $notes = mysqli_real_escape_string($link, $_POST['notes']); $oplver = mysqli_real_escape_string($link, $_POST['oplver']); $usb = mysqli_real_escape_string($link, $_POST['usb']); $smb = mysqli_real_escape_string($link, $_POST['smb']); $hdd = mysqli_real_escape_string($link, $_POST['hdd']); $comp = mysqli_real_escape_string($link, $_POST['comp']); //ON SUBMIT, UPDATE THE MYSQL DATABASE TABLE WITH THE NEW DATA INSIDE THE FORM INPUTS if(isset($_POST['Submit'])) { $update="UPDATE $tbl_name SET notes='$notes', gamename='$gamename', region='$region', mode='$mode', smb='$smb', hdd='$hdd', usb='$usb', comp='$comp', vmc='$vmc', oplver='$oplver' WHERE id='".$game_id."'"; $result=mysqli_query($link,$update) or die("Error: ".mysqli_error($update)); } ?> <?php //SELECT ALL FROM SQL DATABASE TABLE WHERE THE id COLUMN IS THE PRIMARY KEY VALUE $sql = "SELECT * FROM $tbl_name WHERE id = '".$game_id."'"; //SUBMIT QUERY TO DATABASE $result = $link->query($sql) or die(mysqli_error($sql)); ?> <?php //LOOP RETREIEVED DATA TO BE DISPLAYED INSIDE OF THE FORM while ($row = $result->fetch_assoc()) { ?>

Update Entry #<?php echo $game_id;?>

PLEASE BE CONSIDERATE WHEN EDITING AN ENTRY

Result, Region, OPL Version, VMC Support, Load Methods & Modes are not reflected here.

Please double check the current submission’s details before updating entry.

<?php } ?>
  Game Name   
  Region / OPL Ver    NTSC-U NTSC-J PAL OTHER      OPL 0.6 OPL 0.7 OPL 0.8 OPL 0.9
  Result   Compatible Incompatible Issues
  VMC Support    Yay Nay
  Load Methods    SMB USB HDD
  Modes   1 2 3 4 5 6 7 8
  Notes   
   
   *refrain from clicking submit more then once*
[/php]

Any info/direction greatly appreciated.

Corrected code
[php] <input type=“radio” name=“comp” id=“comp” value=“http://www.oplinfo.x11s.org/files/images/comp.gif” <?php echo ($row['comp'] == "http://www.oplinfo.x11s.org/files/images/comp.gif") ? "CHECKED='CHECKED'" : ''; ?>[/php]

Sponsor our Newsletter | Privacy Policy | Terms of Service