Wrox Programmer Forums
Go Back   Wrox Programmer Forums > PHP/MySQL > PHP Databases
|
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 software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old September 28th, 2011, 09:57 AM
Authorized User
 
Join Date: Apr 2006
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');");
 
Old September 29th, 2011, 05:06 AM
Friend of Wrox
 
Join Date: May 2011
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);
 
Old September 29th, 2011, 06:03 AM
Authorized User
 
Join Date: Apr 2006
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).
 
Old September 29th, 2011, 10:33 AM
Friend of Wrox
 
Join Date: May 2011
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.





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





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