Overclock.net › Forums › Software, Programming and Coding › Coding and Programming › Web Coding › php & mysql checkboxes
New Posts  All Forums:Forum Nav:

php & mysql checkboxes

post #1 of 9
Thread Starter 
I'm trying to figure out how to edit using a form. I have several checkboxes may or may not be selected in the DB and their value = 1

How do I pull that information from the mysql table and push it to an edit form where it would show the boxes checked where as they were entered?
Code:
<br><input type="checkbox" name="earslot" checked="<? echo $row['earslot']; ?>" /><br>

currently that shows as checked all the time.
Speedy
(13 items)
 
  
CPUMotherboardGraphicsRAM
Intel Core i7 920 EX58-UD4P NVIDIA GeForce GTX 285 Corsair  
Hard DriveCoolingOSMonitor
Seagate Barracuda CM Hyper 212+ Ubuntu 12.04 w/KDE 25.6" Hanns-G 
KeyboardPowerCaseMouse
Saitek Eclipse II Corsair 750 Watt Antec 902 Logitech Wireless Trackball 
  hide details  
Reply
Speedy
(13 items)
 
  
CPUMotherboardGraphicsRAM
Intel Core i7 920 EX58-UD4P NVIDIA GeForce GTX 285 Corsair  
Hard DriveCoolingOSMonitor
Seagate Barracuda CM Hyper 212+ Ubuntu 12.04 w/KDE 25.6" Hanns-G 
KeyboardPowerCaseMouse
Saitek Eclipse II Corsair 750 Watt Antec 902 Logitech Wireless Trackball 
  hide details  
Reply
post #2 of 9
Well, first, that should be
Code:
<?php, not just <?
, it'll work most of the time, but is sloppy form, and is actually broken in some PHP versions.

Second, make sure the value in that row is "checked", and that that is what's being inputted. Try echoing $row to the page to see what pops out. '1' or '0' does not work, the checked attribute takes the value 'checked'. If it's in boolean form (1 or 0), you'll have to interpret it with the PHP and change it to checked if it's 1.
post #3 of 9
Thread Starter 
Code:
<?php
if (row['earslot'] == 1) {
   $value = "true";
   echo '<br><input type="checkbox" checked= ."$value">'<br>'
}

something like that then?
Speedy
(13 items)
 
  
CPUMotherboardGraphicsRAM
Intel Core i7 920 EX58-UD4P NVIDIA GeForce GTX 285 Corsair  
Hard DriveCoolingOSMonitor
Seagate Barracuda CM Hyper 212+ Ubuntu 12.04 w/KDE 25.6" Hanns-G 
KeyboardPowerCaseMouse
Saitek Eclipse II Corsair 750 Watt Antec 902 Logitech Wireless Trackball 
  hide details  
Reply
Speedy
(13 items)
 
  
CPUMotherboardGraphicsRAM
Intel Core i7 920 EX58-UD4P NVIDIA GeForce GTX 285 Corsair  
Hard DriveCoolingOSMonitor
Seagate Barracuda CM Hyper 212+ Ubuntu 12.04 w/KDE 25.6" Hanns-G 
KeyboardPowerCaseMouse
Saitek Eclipse II Corsair 750 Watt Antec 902 Logitech Wireless Trackball 
  hide details  
Reply
post #4 of 9
Quote:
Originally Posted by Iggy0828 View Post

Code:
<?php
if (row['earslot'] == 1) {
   $value = "true";
   echo '<br><input type="checkbox" checked= ."$value">'<br>'
}
something like that then?
Code:
<?php
$mycon = mysql_connect("localhost","root","yourpassword");
$mydb = mysql_select_db("yourdb",$mycon");
$myquery = mysql_query("SELECT * FROM yourtable");
while($row = mysql_fetch_array($myquery)
{
      $checkedval = $row['earslot'];
}
?>

//some html

<?php
if($checkedval == "checked") //or whatever value your "earslot" row contains when you want it to be checked
{
     echo "<br />><input type="checkbox" checked="checked"><br />";
}
else
{
     echo "<br />><input type="checkbox"><br />";
}
?>

Make sure you are maintaining correct PHP syntax as well. When you concatenate a variable, you muse add periods to both sides of the variable that is being concatenated. ex:
Code:
...
$v = "test";
$v2 = "This is a ".$v."!";
...

Only time this wouldn't be the case is if it's at the end of the string. ex:
Code:
....
$v = "test";
$v2 = "This is a".$v;
...

Hope that helps!
Edited by ZFedora - 9/24/12 at 6:55am
Server
(13 items)
 
  
CPUMotherboardGraphicsRAM
Intel Core i7 2600k Intel H67DE PNY Verto 9600GT (Thanks to ihasfip!) 16GB G.Skill Ripjaws 1333Mhz 
Hard DriveOSMonitorPower
(3) Seagate 7200K 1TB (2) Hitachi 7200K.12 1TB Proxmox Samsung 17" Corsair GS600 
Case
Chenbro tower 
  hide details  
Reply
Server
(13 items)
 
  
CPUMotherboardGraphicsRAM
Intel Core i7 2600k Intel H67DE PNY Verto 9600GT (Thanks to ihasfip!) 16GB G.Skill Ripjaws 1333Mhz 
Hard DriveOSMonitorPower
(3) Seagate 7200K 1TB (2) Hitachi 7200K.12 1TB Proxmox Samsung 17" Corsair GS600 
Case
Chenbro tower 
  hide details  
Reply
post #5 of 9
He could also use a ternary operator to make it shorter, plus he's storing a tinyint, not a varchar containing "checked".
Code:
<br><input type="checkbox" name="earslot" <?php echo ($row['earslot'] ? 'checked="checked"' : ''); ?> /><br>
post #6 of 9
Quote:
Originally Posted by Arcalys View Post

He could also use a ternary operator to make it shorter, plus he's storing a tinyint, not a varchar containing "checked".
Code:
<br><input type="checkbox" name="earslot" <?php echo ($row['earslot'] ? 'checked="checked"' : ''); ?> /><br>

Please read my comment in the code thumb.gif
Server
(13 items)
 
  
CPUMotherboardGraphicsRAM
Intel Core i7 2600k Intel H67DE PNY Verto 9600GT (Thanks to ihasfip!) 16GB G.Skill Ripjaws 1333Mhz 
Hard DriveOSMonitorPower
(3) Seagate 7200K 1TB (2) Hitachi 7200K.12 1TB Proxmox Samsung 17" Corsair GS600 
Case
Chenbro tower 
  hide details  
Reply
Server
(13 items)
 
  
CPUMotherboardGraphicsRAM
Intel Core i7 2600k Intel H67DE PNY Verto 9600GT (Thanks to ihasfip!) 16GB G.Skill Ripjaws 1333Mhz 
Hard DriveOSMonitorPower
(3) Seagate 7200K 1TB (2) Hitachi 7200K.12 1TB Proxmox Samsung 17" Corsair GS600 
Case
Chenbro tower 
  hide details  
Reply
post #7 of 9
Didn't see that comment (due to the lack of colors i guess tongue.gif), sorry.
post #8 of 9
Quote:
Originally Posted by Arcalys View Post

Didn't see that comment (due to the lack of colors i guess tongue.gif), sorry.

Yeah, idk why the code tag isnt working hahah, but yeah I saw that he was using an int, not sure what compelled me to use a varchar, but either way works smile.gif
Server
(13 items)
 
  
CPUMotherboardGraphicsRAM
Intel Core i7 2600k Intel H67DE PNY Verto 9600GT (Thanks to ihasfip!) 16GB G.Skill Ripjaws 1333Mhz 
Hard DriveOSMonitorPower
(3) Seagate 7200K 1TB (2) Hitachi 7200K.12 1TB Proxmox Samsung 17" Corsair GS600 
Case
Chenbro tower 
  hide details  
Reply
Server
(13 items)
 
  
CPUMotherboardGraphicsRAM
Intel Core i7 2600k Intel H67DE PNY Verto 9600GT (Thanks to ihasfip!) 16GB G.Skill Ripjaws 1333Mhz 
Hard DriveOSMonitorPower
(3) Seagate 7200K 1TB (2) Hitachi 7200K.12 1TB Proxmox Samsung 17" Corsair GS600 
Case
Chenbro tower 
  hide details  
Reply
post #9 of 9
Thread Starter 
Thank you! +rep
Speedy
(13 items)
 
  
CPUMotherboardGraphicsRAM
Intel Core i7 920 EX58-UD4P NVIDIA GeForce GTX 285 Corsair  
Hard DriveCoolingOSMonitor
Seagate Barracuda CM Hyper 212+ Ubuntu 12.04 w/KDE 25.6" Hanns-G 
KeyboardPowerCaseMouse
Saitek Eclipse II Corsair 750 Watt Antec 902 Logitech Wireless Trackball 
  hide details  
Reply
Speedy
(13 items)
 
  
CPUMotherboardGraphicsRAM
Intel Core i7 920 EX58-UD4P NVIDIA GeForce GTX 285 Corsair  
Hard DriveCoolingOSMonitor
Seagate Barracuda CM Hyper 212+ Ubuntu 12.04 w/KDE 25.6" Hanns-G 
KeyboardPowerCaseMouse
Saitek Eclipse II Corsair 750 Watt Antec 902 Logitech Wireless Trackball 
  hide details  
Reply
New Posts  All Forums:Forum Nav:
  Return Home
  Back to Forum: Web Coding
Overclock.net › Forums › Software, Programming and Coding › Coding and Programming › Web Coding › php & mysql checkboxes