Jump to content

Checked=checked if value exists in database


usrmd

Recommended Posts

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
//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()) {
?>
<h2>Update Entry #<?php echo $game_id;?><BR />

PLEASE BE CONSIDERATE WHEN EDITING AN ENTRY<br />
Result, Region, OPL Version, VMC Support, Load Methods & Modes are not reflected here.<br />
Please double check the current submission's details <u>before</u> updating entry.</h2>
<table width="543" border="0" bgcolor="#006699" align="center">
<form action="" method="post" name="form1" id="form1" onsubmit="return formCheck(this);">
<tr>
<td width="171" height="35" bgcolor="#003333"><strong>  Game Name</strong></td>
<td width="362" bgcolor="#003333">  
<input name="gamename" type="text" id="gamename" size="43" maxlength="40" value="<?php echo $row['gamename']; ?>"/></td>
</tr>
<tr>
<td height="33" bgcolor="#003333"><strong>  Region / OPL Ver</strong></td>
<td bgcolor="#003333">  
<select name="region" id="region">
<option value="U.png">NTSC-U</option>
<option value="J.png">NTSC-J</option>
<option value="E.png">PAL</option>
<option value="O.png">OTHER</option>
</select>
    
<select name="oplver" id="oplver">
<option value="0.6">OPL 0.6</option>
<option value="0.7">OPL 0.7</option>
<option value="0.8">OPL 0.8</option>
<option value="0.9">OPL 0.9</option>
</select></td>
</tr>
<tr>
<td height="25" bgcolor="#003333"><strong>  Result</strong></td>
<td bgcolor="#003333"><strong>  Compatible</strong>
<input type="radio" name="comp" id="comp" value="comp.gif" />
<strong>Incompatible</strong>
<input type="radio" name="comp" id="incomp" value="incomp.gif"/>
<strong>Issues</strong>
<input type="radio" name="comp" id="issues" value="issues.gif"/></td>
</tr>
<tr>
<td height="28" bgcolor="#003333"><strong>  VMC Support</strong></td>
<td bgcolor="#003333">  
Yay
<input name="vmc" type="radio" id="vmc" value="VMC" />
Nay
<input type="radio" name="vmc" id="mc" value="MC" /></td>
</tr>
<tr>
<td height="31" bgcolor="#003333"><strong>  Load Methods</strong></td>
<td bgcolor="#003333">  
<input type="hidden" name="smb" value="-" />
SMB
<input type="checkbox" name="smb" id="smb" value="SMB" />
<input type="hidden" name="usb" value="-" />
USB
<input type="checkbox" name="usb" id="usb" value="USB" />
<input type="hidden" name="hdd" value="-" />
HDD
<input type="checkbox" name="hdd" id="hdd" value="HDD" /></td><?php } ?>
</tr>
<tr>
<td height="25" bgcolor="#003333"><strong>  Modes</strong></td>
<td bgcolor="#003333">  1
<input type="checkbox" name="mode[1]" id="mode1" value="1" />
2
<input type="checkbox" name="mode[2]" id="mode2" value="2" />
3
<input type="checkbox" name="mode[3]" id="mode3" value="3" />
4
<input type="checkbox" name="mode[4]" id="mode4" value="4" />
5
<input type="checkbox" name="mode[5]" id="mode5" value="5" />
6
<input type="checkbox" name="mode[6]" id="mode6" value="6" />
7
<input type="checkbox" name="mode[7]" id="mode7" value="7" />
8
<input type="checkbox" name="mode[8]" id="mode8" value="8" /></td>
</tr>
<tr>
<td height="34" bgcolor="#003333"><strong>  Notes</strong></td>
<td bgcolor="#003333">  
<input name="notes" type="text" id="notes" size="43" maxlength="40" value="<?php echo $row['notes']; ?>"/>
<br /></td>
</tr>
<tr>
<td> </td>
<td> </td>
</tr>
<tr>
<td height="34" bgcolor="#003333"> 
<input type="Submit" name="Submit" value="Update" /></td>
<td bgcolor="#003333"><strong> *refrain from clicking submit more then once*</strong></td>
</tr>
</form>
</table>

 

Any info/direction greatly appreciated.

Edited by usrmd
Link to comment
Share on other sites

According to the code you posted it will not select radio buttons or check checkboxes according to existing data. You say you fixed that "technically"? What's your code like after that?

 

Yes, the code at the top of my post does indeed check the checkbox named "smb" (itself) if the value "SMB" exists in $rows['comp'].. I can apply this code to any type of input simply by changing the parameters. I was just supplying the method I used to get my half way desired result, this is why I said "technically".. because that code does exactly what is stated in the title to this topic.Unfortunately, it doesn't do all of what I need. All it does is select the check boxes, but I think there is an underlying problem as to why when using that code for my inputs, the corresponding sql data cannot be updated with new values when selecting non checked boxes. Please view the live example I provided to get a 100% accurate idea of what I am trying to explain.

Link to comment
Share on other sites

I understand what you're saying. I'd still like to see the code: the stuff you have that correctly selects/checks fields, not the one you had a while ago that is close but not quite accurate.

 

[edit] Which I say also because I see differences between what you posted and what's on the site. Like how all the comp images are for comp.gif and how the mode arrays don't use keys.

Edited by requinix
Link to comment
Share on other sites

I understand what you're saying. I'd still like to see the code: the stuff you have that correctly selects/checks fields, not the one you had a while ago that is close but not quite accurate.

 

[edit] Which I say also because I see differences between what you posted and what's on the site. Like how all the comp images are for comp.gif and how the mode arrays don't use keys.

 

Ah, I don't have a code that correctly selects/checks fields :(

 

Hmm.. I don't see that all the images are for comp.gif. I use the same name "comp" for 3 different radio buttons, because only 1 can be checked, and each one needs to represent a different image url within the same column. The long code I posted is pretty much a copy of the live code on the site you saw.. and yeah I don't know why I added [1] [2] etc for the modes, I was just sitting on my hands trying to make things work. it's just got a few small changes from the past hour trying to figure this checkbox stuff out. Here is the exact~ same code that is currently on the website you saw.

 


<?php
//CONNECT TO THE MYSQL DATABASE
$link = mysqli_connect('host','user','password','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()) {
?>
       <h2>Update Entry #<?php echo $game_id;?><BR />

 PLEASE BE CONSIDERATE WHEN EDITING AN ENTRY<br />
 Result, Region, OPL Version, VMC Support, Load Methods & Modes are not reflected here.<br />
 Please double check the current submission's details <u>before</u> updating entry.</h2>
       <table width="543" border="0" bgcolor="#006699" align="center">
         <form action="" method="post" name="form1" id="form1" onsubmit="return formCheck(this);">
           <tr>
             <td width="171" height="35" bgcolor="#003333"><strong>  Game Name</strong></td>
             <td width="362" bgcolor="#003333">  
               <input name="gamename" type="text" id="gamename" size="43" maxlength="40" value="<?php echo $row['gamename']; ?>"/></td>
           </tr>
           <tr>
             <td height="33" bgcolor="#003333"><strong>  Region / OPL Ver</strong></td>
             <td bgcolor="#003333">  
               <select name="region" id="region">
                 <option value="http://www.oplinfo.x11s.org/files/images/U.png">NTSC-U</option>
                 <option value="http://www.oplinfo.x11s.org/files/images/J.png">NTSC-J</option>
                 <option value="http://www.oplinfo.x11s.org/files/images/E.png">PAL</option>
                 <option value="http://www.oplinfo.x11s.org/files/images/O.png">OTHER</option>
               </select>
                   
               <select name="oplver" id="oplver">
              <option value="0.6">OPL 0.6</option>
              <option value="0.7">OPL 0.7</option>
              <option value="0.8">OPL 0.8</option>
              <option value="0.9">OPL 0.9</option>
               </select></td>
           </tr>
           <tr>
             <td height="25" bgcolor="#003333"><strong>  Result</strong></td>
             <td bgcolor="#003333"><strong>  Compatible</strong>
               <input type="radio" name="comp" id="comp" value="<?php echo $row["comp"]; ?>" <?php echo ($row['comp'] == "http://www.oplinfo.x11s.org/files/images/comp.gif") ? "CHECKED='CHECKED'" : ''; ?> />
               <strong>Incompatible</strong>
               <input type="radio" name="comp" id="incomp" value="<?php echo $row["comp"]; ?>" <?php echo ($row['comp'] == "http://www.oplinfo.x11s.org/files/images/incomp.gif") ? "CHECKED='CHECKED'" : ''; ?> />
               <strong>Issues</strong>
               <input type="radio" name="comp" id="issues" value="<?php echo $row["comp"]; ?>" <?php echo ($row['comp'] == "http://www.oplinfo.x11s.org/files/images/issues.gif") ? "CHECKED='CHECKED'" : ''; ?> /></td>
           </tr>
           <tr>
             <td height="28" bgcolor="#003333"><strong>  VMC Support</strong></td>
             <td bgcolor="#003333">  
               Yay
               <input name="vmc" type="radio" id="vmc" value="VMC" />
               Nay
             <input type="radio" name="vmc" id="mc" value="MC" /></td>
           </tr>
           <tr>
             <td height="31" bgcolor="#003333"><strong>  Load Methods</strong></td>
             <td bgcolor="#003333">  
               <input type="hidden" name="smb" value="-" />
               SMB
               <input type="checkbox" name="smb" id="smb" value="SMB" />
               <input type="hidden" name="usb" value="-" />
               USB
               <input type="checkbox" name="usb" id="usb" value="USB" />
               <input type="hidden" name="hdd" value="-" />
               HDD
               <input type="checkbox" name="hdd" id="hdd" value="HDD" /></td>
           </tr>
           <tr>
             <td height="25" bgcolor="#003333"><strong>  Modes</strong></td>
             <td bgcolor="#003333">  1
               <input type="checkbox" name="mode[]" id="mode1" value="1" />
               2
               <input type="checkbox" name="mode[]" id="mode2" value="2" />
               3
               <input type="checkbox" name="mode[]" id="mode3" value="3" />
               4
               <input type="checkbox" name="mode[]" id="mode4" value="4" />
               5
               <input type="checkbox" name="mode[]" id="mode5" value="5" />
               6
               <input type="checkbox" name="mode[]" id="mode6" value="6" />
               7
               <input type="checkbox" name="mode[]" id="mode7" value="7" />
               8
               <input type="checkbox" name="mode[]" id="mode8" value="8" /></td>
           </tr>
           <tr>
             <td height="34" bgcolor="#003333"><strong>  Notes</strong></td>
             <td bgcolor="#003333">  
               <input name="notes" type="text" id="notes" size="43" maxlength="40" value="<?php echo $row['notes']; ?>"/>
               <br /></td>
           </tr>
           <tr>
             <td> </td>
             <td> </td>
           </tr>
           <tr>
             <td height="34" bgcolor="#003333"> 
               <input type="Submit" name="Submit" value="Update" /></td>
             <td bgcolor="#003333"><strong> *refrain from clicking submit more then once*</strong></td>
           </tr>
         </form>
       </table>
 <p><A href='index.php'><strong>VIEW GAME COMPATABILITY DATABASE</strong></A><br />
</p>
</center>

<?php
//END THE LOOP OF DATA TO THE FORM
}
?>

<?php 
//CLOSE THE CONNECTION TO THE DATABASE
mysqli_close($link);
?>

Link to comment
Share on other sites

<input type="hidden" name="hdd" value="-" />

HDD

<input type="checkbox" name="hdd" id="hdd" value="HDD" /></td>

 

You shouldn't name your inputs the same like that.  PHP will only keep one of the values, which one depends on the order in which they were submitted (which generally is source-order).  I gather you did that so that $_POST['hdd'] will be set to '-' if the checkbox is not set, but a more proper way would be to only have the checkbox and then process it as:

 

$hdd = isset($_POST['hdd'])?$_POST['hdd']:'-';

 

Link to comment
Share on other sites

You shouldn't name your inputs the same like that. PHP will only keep one of the values, which one depends on the order in which they were submitted (which generally is source-order). I gather you did that so that $_POST['hdd'] will be set to '-' if the checkbox is not set, but a more proper way would be to only have the checkbox and then process it as:

 

$hdd = isset($_POST['hdd'])?$_POST['hdd']:'-';

 

Thank you for that information. I will try this.. and yes you are correct in your assumption as to why I included the hidden checkboxes :)

Link to comment
Share on other sites

So the comp stuff is the only one you're trying to pre-check now, right?

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

Take a close look at the value of the radio button. Same value you use for the other two even.

Link to comment
Share on other sites

So the comp stuff is the only one you're trying to pre-check now, right?

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

Take a close look at the value of the radio button. Same value you use for the other two even.

 

I figure if I can get one set working, I'd be able to get the others working in the same way.. except for the mode checkboxes, anyways.. as those are imploded values in a single column.. and I'm definitely not sure how to define which box get's checked depending on the specific values in the column.

 

I'm inspecting the value, and I have tried different things.. for instance, removing the first echo $rows["comp"], but then no checked=checked happens for the radio button. I feel like I am missing something small, and when I figure it out I am going to feel awfully silly xD

Link to comment
Share on other sites

Your comp radio buttons need to have different values. Right now you're setting them all to the same value so there is no way for you to distinguish which one was actually checked when the form was submitted.

 

Hahahahaha... /bow. Way too much weed in my life right now.

Very humbling. Echoing the same information and wondering why it isn't changing. I had it like this not long ago, but it wasn't working.. I must have had a different issue, so ended up changing it. Should have realized. The radio buttons and other check boxes are all working as needed now after correcting the code as shown below.

 

<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'" : ''; ?> />

 

Now, those pesky mode checkboxes... Their values are imploded into a single column. What would enable the script to know which box checked=checked based off of specific values inside the column? eg column data is "1, 5, 7" so checkboxes 1, 5 and 7 would checked=checked.

Link to comment
Share on other sites

explode and in_array are your friends.

 

But really the database should be normalized: keep a separate table for distinct game/mode pairs, one row for each pair. For starters it makes searching much, much easier.

 

Fortunately I don't think I will run into any problems by keeping the values imploded inside the same columns, as the values will never exceed 8.

 

After checking out explode and in_array, I came up with this

 

 

<?php
$modes = ($rows['mode']);
$modexplode = explode(",", $modes);
?>
1
<input type="checkbox" name="mode[]" id="mode1" value="1" <? if (in_array("1", $modexplode)){echo 'checked="checked"';} ?>/>

 

Though it comes up with no boxes checked. I tried a lot of different versions of this, also, and looked up examples from other people to try those as well. I also looked into creating a function to do the job, but I'm too tired to read that much right now.

Would you say the method above will get the job done? If yes, how can I display the column values inside "$modes=" ? I can enter my own values there[1, 2, 3, 4, 5, 6, 7, 8] but that results in every box being checked.

Edited by usrmd
Link to comment
Share on other sites

Bit of trial and error..

 

Here is the corrected code:

<?php
$boxes= explode(",", $row["mode"]);
for($i=0; $i<sizeof($boxes); $i++);
?>
<input type="checkbox" name="mode[]" id="mode1" value="1" <?=(in_array('1',$boxes))?('checked')'');?>>

 

Imploded check boxes are now reflecting checked if their individual values exist within the column "mode". :happy-04:

Thanks for all the help here.

Edited by usrmd
Link to comment
Share on other sites

That for loop you have there isn't doing anything, you can cut it out. Leaves you with basically the same thing you had before.

 

I should have tried it without the for loop, I suppose. I assumed I would have needed it, though. Removing it did keep the same functionality. This is good, because I was confused as to why~ for or foreach would be needed for this (thanks to all the terrible examples throughout the internet). The way it is working makes much more sense without that line there :) Thanks.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.