Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > PHP/MySQL > PHP Databases
Password Reminder
Register
| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
PHP Databases Using PHP in conjunction with databases. PHP questions not specific to databases should be directed to one of the other PHP forums.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the PHP Databases section of the Wrox Programmer to Programmer discussions. This is a community of tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old September 28th, 2011, 09:57 AM
Authorized User
Points: 250, Level: 4
Points: 250, Level: 4 Points: 250, Level: 4 Points: 250, Level: 4
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Apr 2006
Location: , , Greece.
Posts: 46
Thanks: 0
Thanked 1 Time in 1 Post
Default checkboxes insert values to db

Hi, I am new at PHP and i d like some help

I ve created a form with checkboxes and i d like when i 'm checking some and press submit to insert the values to db.

my code:

html:

AIR CONDITION: <input type="checkbox" name="id_
xar[]" value="1"/><br/>
PARKING: <input type="checkbox" name="id_
xar[]" value="2"/><br/>
POOL: <input type="checkbox" name="id_
xar[]" value="3"/><br/>
HEATING: <input type="checkbox" name="id_xar[]" value="4"/><br/>
GAS: <input type="checkbox" name="id_
xar[]" value="5"/>
<br><br>
<input name="add_button" type="submit" value="SUBMIT">
<input name="cancel_button" type="reset" value="CANCEL">



php:

mysql_query("INSERT INTO fotos (id_akinhtou, id_xar)
VALUES
('$id', '$id_xar');");
Reply With Quote
  #2 (permalink)  
Old September 29th, 2011, 05:06 AM
Friend of Wrox
Points: 651, Level: 9
Points: 651, Level: 9 Points: 651, Level: 9 Points: 651, Level: 9
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: May 2011
Location: North East UK
Posts: 125
Thanks: 0
Thanked 24 Times in 24 Posts
Default

Greetings,

The way you've got the checkboxes setup you'll be passing an array back to the php, you would be better off doing something like:
Code:
Air Condition: <input type="checkbox" name="air_con" value="1" /><br />
Parking: <input type="checkbox" name="parking" value="1" />
Then all you'd do is check to see if the item is in the POST array, and if it is send it to the db, note the above assumes you have seperate fields for each option you would use a TINYINT and set the option to 1 = Yes and 0 = No.

The php:
Code:
$air_con = ( isset($_POST['air_con']) ? intval($_POST['air_con']) : 0);
$parking = ( isset($_POST['parking']) ? intval($_POST['parking']) : 0);
$pool = ( isset($_POST['pool']) ? intval($_POST['pool']) : 0);
$heating = ( isset($_POST['heating']) ? intval($_POST['heating']) : 0);
$gas = ( isset($_POST['gas']) ? intval($_POST['gas']) : 0);

// Values will default to zero if not ticked
$sql = "INSERT INTO fotos ( air_con, parking, pool, heating, gas ) VALUES( $air_con, $parking, $pool, $heating, $gas )";
mysql_query($sql);
Reply With Quote
  #3 (permalink)  
Old September 29th, 2011, 06:03 AM
Authorized User
Points: 250, Level: 4
Points: 250, Level: 4 Points: 250, Level: 4 Points: 250, Level: 4
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Apr 2006
Location: , , Greece.
Posts: 46
Thanks: 0
Thanked 1 Time in 1 Post
Default

thanx a lot,

but the db table, has only two columns (id, xar) and is like:

id xar
1 1
1 2
1 3
2 2
3 2
3 1
..........

where xar1 = pool xar2= gas xar3=aircondition etc.

if I use

Air Condition: <input type="checkbox" name="air_con" value="1" /><br />
Parking: <input type="checkbox" name="parking" value="1" />
the value of xar will be always 1 (where 1=pool).
Reply With Quote
  #4 (permalink)  
Old September 29th, 2011, 10:33 AM
Friend of Wrox
Points: 651, Level: 9
Points: 651, Level: 9 Points: 651, Level: 9 Points: 651, Level: 9
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: May 2011
Location: North East UK
Posts: 125
Thanks: 0
Thanked 24 Times in 24 Posts
Default

Greetings,

Strange way to do it, I would have done it this way:

id - id field with auto increment
description - varchar field describing the item
aircon - tinyint field
parking - tinyint field
pool - tinyint field
heating - tinyint field
gas - tinyint field

that way you'd end up with the table looking like this:

Code:
id	desc	aircon	park	pool	heat	gas
1	item 1	1	1	1	0	0
2	item 2	0	1	0	0	0
3	item 3	1	1	0	0	0
And with checkboxes the value is only passed back from the form if it's checked (ticked) if it's not ticked then it's not passed. With the way your doing it would be a nightmare to edit the record.

My way is easier as you just have to check if the value is 1 if it is set the checked property to checked and the item will be ticked when you edit the record.
Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Adding Gridview Column of Checkboxes and Retrieving Values nelper703 ASP.NET 3.5 Professionals 5 April 26th, 2009 09:44 PM
peoblem with checkboxes values shrisangeeta Classic ASP Basics 3 August 19th, 2006 02:03 AM
How 2 store checkboxes values in MySQL babaloui Beginning PHP 0 March 13th, 2006 04:05 AM
How to insert a column values in existing db (msac gilgalbiblewheel Classic ASP Databases 0 April 6th, 2005 06:53 PM
insert values into a MySQL DB from a drop down men crmpicco Classic ASP Basics 26 January 24th, 2005 08:10 AM



All times are GMT -4. The time now is 07:00 PM.


Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.