Checked checkbox save to Database

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
ShadowSkill
Forum Newbie
Posts: 22
Joined: Sun Jan 01, 2012 5:45 am

Checked checkbox save to Database

Post by ShadowSkill »

I want to put all the checked items into my database subjects
I'm confused more now how to store the checked value to the database @_@
Just give me a small sample code how to save the checked item to the database please (if possible detailed coz im newbie)
Thank you very much
User avatar
califdon
Jack of Zircons
Posts: 4484
Joined: Thu Nov 09, 2006 8:30 pm
Location: California, USA

Re: Checked checkbox save to Database

Post by califdon »

There's a lot you need to learn in order to do what you described. Start with What database are you using? What is the database schema? We don't generally supply detailed code to students in order to complete an assignment, although we are always glad to try to help you learn how to do it yourself.
User avatar
twinedev
Forum Regular
Posts: 984
Joined: Tue Sep 28, 2010 11:41 am
Location: Columbus, Ohio

Re: Checked checkbox save to Database

Post by twinedev »

Well it would depend what the checkboxes represent.
Some samples I have used:

enum('On','Off")
enum('Y','N')
enum('True','False')
enum('checked','') (i didn't care for this, was work "standard", they originally wasted data with actual string "checked")

If they are attribute of an item, think of it like a car listing:

Code: Select all

YEAR: 2004
MAKE: Chevy
MODEL: Venture
[  ] AC
[  ] Premium Sound
[  ] Power Windows
[  ] Power Doors
So in this case, I would have each feature assigned in a related database, so each item is actually an int value for the feature. Then in the database, have a field that stores them as ~3~4~10~12~ (so then later search, can do a search for ~10~ ).
Another option is to have a separate relations table that contains PK from your main data and the PK to the items you are checking.

So if you can give more example of what type of information about what your checkboxes are used for, can give you more specifc, here is sample based upong enum('Y','N'):

Code: Select all

<ul>
    <li><input type="checkbox" name="chkOpen24Hrs" id="chkOpen24Hrs" value="Y"><label for="chkOpen24Hrs">Open 24 Hours</label></li>
    <li><input type="checkbox" name="chkDriveThru" id="chkDriveThru" value="Y"><label for="chkDriveThru">Has Drive-Thru</label></li>
    <li><input type="checkbox" name="chkAcceptCC" id="chkAcceptCC" value="Y"><label for="chkAcceptCC">Accepts Credit Cards</label></li>
</ul>
and here is code to handle it:

Code: Select all

if (count($_POST) > 0) {
    // Set defaults for checkboxes...
    foreach(array('Open24Hrs','DriveThru','AcceptCC') as $key) {
        if (!isset($_POST['chk'.$key])) { $_POST['chk'.$key]='N'; }
    }

   // Now regular processing of Post Variables, you now have values for "unchecked" checkboxes...
-Greg
ShadowSkill
Forum Newbie
Posts: 22
Joined: Sun Jan 01, 2012 5:45 am

Re: Checked checkbox save to Database

Post by ShadowSkill »

Hmmmm... This my current codes but it wont work...
I want to save the checked values to my Phpadmin database and only the checked boxes will be saved...
My codes are not totally working and have some problems myself trying to search and study but i need to finish this within this week for my defense...
I used PHP as my main source for my system since i'm not good at Visual Basic and my cousin said HTML + PHP can do for a simple Enrollment system for masteral degree...
If you're wondering why i chose PHP which i don't have knowledge to start with is because the professors at our school doesn't teach much even at Visual Basic nor HTML and my cousin said go for PHP if you're going to study solo since PHP now is used and has high recommendation after i graduate... I'm studying PHP through W3school and other searched sites using google...

I hope you guys understand my situation as a self study thesis i'm just desperate. I posts at 3 different forums but this one is the best for it never fails to solve my questions

Code: Select all

<html>
<head>

<title>Untitled Document</title>
</head>

<body>


<table " align="center" border="1"  cellspacing="10" bgcolor="#00CCFF">
<tr><td colspan="3">
<h4>I. BASIC COURSES</h4></td><td align="right"><h4>9 UNITS	</h4>
</td></tr>

<form  method="GET">
 <input type="hidden" name="stdnum"  />

 <tr> <td><input type="checkbox" name="1BC1" value="DPEM 101 Philosophy of Man 3 units" /></td><td>DPEM 101 </td><td>Philosophy of Man</td><td>3 units</td><br /></tr>
 
 <tr><td><input type="checkbox" name="1BC2" value="DPEM 102 Philosophy of Management: Theory and Practice 3 units" /></td><td>DPEM 102 </td><td>Philosophy of Management: Theory and Practice</td><td>3 units</td><br /></tr>
 
 <tr><td><input type="checkbox" name="1BC3" value="DPEM 103a Advance Research 1 3 units" /></td><td>DPEM 103a </td><td>Advance Research 1</td><td>3 units</td><br /></tr>
 
 
<tr>
	<td align=right> <input type=Submit value="Submit" name="stdsubmit"></td>
	
</tr>
</form>


</table>

<?

if(isset($_GET["stdnum"]))
{
		
	
$database="stdrecords";

$1BC1=$_GET['1BC1'];
$1BC2=$_GET['1BC2'];
$1BC3=$_GET['1BC3'];


mysql_connect("localhost","root","");

@mysql_select_db($database) or die( "NO DATABASE");


$query = "INSERT INTO stddeg  VALUES('','$1BC1','$1BC2','$1BC3')";
mysql_query($query);


  header("Location: saved.php");

mysql_close();
}


?>









</body>
</html>
ShadowSkill
Forum Newbie
Posts: 22
Joined: Sun Jan 01, 2012 5:45 am

Re: Checked checkbox save to Database

Post by ShadowSkill »

If you want to see all my codes from add student, course, co1, search, update and all just message me and i'll send them thanks...
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: Checked checkbox save to Database

Post by Celauran »

I'm pretty sure I mentioned this in another thread already, but variable names cannot start with a number. Also, what does your stddeg table look like? Type 'DESCRIBE stddeg' into MySQL if you aren't sure.
ShadowSkill
Forum Newbie
Posts: 22
Joined: Sun Jan 01, 2012 5:45 am

Re: Checked checkbox save to Database

Post by ShadowSkill »

Thanks for that...
I didnt notice my variable started with a number...

my stdeg has sub1,sub2,sub3... sub17
i was hoping if the it was checked then it would be saved i those fields...
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: Checked checkbox save to Database

Post by Celauran »

ShadowSkill wrote:my stdeg has sub1,sub2,sub3... sub17
But what type are those fields?
ShadowSkill
Forum Newbie
Posts: 22
Joined: Sun Jan 01, 2012 5:45 am

Re: Checked checkbox save to Database

Post by ShadowSkill »

VARCHAR sorry late reply
Post Reply